GeezerVison is showing github.com — ENH: Add dtype-support for pandas' type-hinting · Issue #34248 · pandas-dev/pandas
ENH: Add dtype-support for pandas' type-hinting #34248DescriptionIt would be great if you could annotate a series, for example with -
def process_series(ser: pd.Series[int, str]) -> pd.Series[int, int]:
to indicate that the function should accept a string-valued series with an integer index, and output
int-valued series with an integer index.
It could be even better if you would build on that such that the series member's types could be inferred using TypeVars (for example, pd.Series[int,str].iloc would accept an integer and return a string), but that's not necessary - just a bonus or a later milestone.
To do that, you could add a new module (maybe "pandas.typing"?) that would contain these type-hints and would require minimal integration (if any) into the pandas' infrastructure. There's a similar package for "numpy" that's external to it called nptyping that could be used as a reference. Reacted by Yankee, Michał Krassowski, Robbie Clarken, Sri Datta Budaraju, Wei Qiu, Oliver Mannion, Alex Makelov, Jakob Kolb, Mitty, Rüdiger Busche and 34 more Activitysimonjayhawkins commented on May 19, 2020 Thanks @erezinman for the report. Adding this would certainly be easier with stubs, but we have not yet reached a decision on stubs, xref #28142.
To do this with type annotations in the code, we would need to use typing.Generic
Thanks for the link. I suspect we would follow NumPy conventions for the type parameters. https://github.com/numpy/numpy-stubs
This might be as simple as writing np.ndarray[np.float64], but will need a decision about appropriate syntax for shape typing to ensure that this is forwards compatible with typing shapes.
In pandas we would also need to allow Series to be backed by pandas Extension Arrays for the values and index, so I suspect the type parameters would be numpy/pandas extension dtypes and not Python types
so maybe something like pd.Series[pd.Int64Dtype(),pd.StringDtype()].
This would be cumbersome so having pd.Series[int, str]) represent the same thing would certainly be welcome from a user perspective.
Further investigation and PRs welcome. added this to the This milestone has been deleted milestone on May 19, 2020removed this from the This milestone has been deleted milestone on Oct 13, 2022simonjayhawkins commented on Feb 23, 2023 erezinman commented on Feb 23, 2023 Hi, @simonjayhawkins
Could you please elaborate how this commit solves the issue? I looked at the referred commit and couldn't see how.
Thanks. simonjayhawkins commented on Feb 23, 2023 Dr-Irv commented on Feb 23, 2023
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|