79639642

Date: 2025-05-26 23:05:49
Score: 1
Natty:
Report link

What you have is pretty close to the best you can get, simply swap out each of the type hints for a pandas.core.series.Series to get the correct type hints. Here is an example implementation modeled after your example.

import pandas as pd
import pandas.core.series
import io


class TypedDataFrame(pd.DataFrame):
    day: pd.core.series.Series
    country: pd.core.series.Series
    weather: pd.core.series.Series


def _main():
    df: TypedDataFrame = TypedDataFrame(pd.read_csv(io.StringIO(
        'day,country,weather\n'
        '1,"CAN","sunny"\n'
        '2,"GBR","cloudy"\n'
        '4,"IND","rain"'
    )))

    print(type(df.weather))
    print(df.country[0])


if __name__ == '__main__':
    _main()

And here are what the type hints looks like in Pycharm at least, I would expect VSCode or others to be similar:

Showing type hints in Pycharm for field

Showing type hints in Pycharm for field attributes

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): What you have is
  • Low reputation (0.5):
Posted by: BitsAreNumbersToo