TYP: add CategoricalSeries type alias for Series[CategoricalDtype] (#1415)#1733
Draft
MukundaKatta wants to merge 1 commit intopandas-dev:mainfrom
Draft
TYP: add CategoricalSeries type alias for Series[CategoricalDtype] (#1415)#1733MukundaKatta wants to merge 1 commit intopandas-dev:mainfrom
MukundaKatta wants to merge 1 commit intopandas-dev:mainfrom
Conversation
…ype] Adds a CategoricalSeries: TypeAlias = Series[CategoricalDtype] alias to pandas-stubs/_typing.pyi guarded under TYPE_CHECKING (Series is not subscriptable at runtime). This gives users a short, discoverable spelling for typing categorical Series without changing the existing Series[CategoricalDtype] parameterization, so it is fully backward compatible. Adds tests under tests/series/categorical/test_dtypes.py exercising the alias, the .cat accessor (codes, categories, ordered), Series.array narrowing to Categorical, and Series.copy preserving the categorical parameterization. The broader design question raised in GH1415 (whether Series[Categorical] should be parameterized by element type instead of CategoricalDtype, or modeled like Interval with a CategoricalItem[C1] generic) is left open; this change is the safer, non-breaking subset.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Refs #1415. Adds the additive piece of "how should we type Series with categorical dtype?" — a
CategoricalSeries: TypeAlias = Series[CategoricalDtype]declared underTYPE_CHECKINGinpandas-stubs/_typing.pyi.Design choice
The broader question — re-parameterizing
Seriesby element type, or introducing aCategoricalItem[C1]generic mirroringInterval— was discussed in the issue thread but is genuinely open. Re-parameterizing would break every existing user that wroteSeries[CategoricalDtype]. This PR ships only the additive subset: a short, discoverable spelling. Maintainers can layer the generic design on top later without breaking anything here.Files changed
pandas-stubs/_typing.pyi— alias underTYPE_CHECKING(Series is not subscriptable at runtime; mirrorsMaskType/IndexTypepattern).tests/series/categorical/test_dtypes.py— 3 new tests: assignment compatibility, cat accessor typing, copy-preserves-parameterization.Test plan