Skip to content

TYP: add CategoricalSeries type alias for Series[CategoricalDtype] (#1415)#1733

Draft
MukundaKatta wants to merge 1 commit intopandas-dev:mainfrom
MukundaKatta:feat/categorical-series-typing
Draft

TYP: add CategoricalSeries type alias for Series[CategoricalDtype] (#1415)#1733
MukundaKatta wants to merge 1 commit intopandas-dev:mainfrom
MukundaKatta:feat/categorical-series-typing

Conversation

@MukundaKatta
Copy link
Copy Markdown

Summary

Refs #1415. Adds the additive piece of "how should we type Series with categorical dtype?" — a CategoricalSeries: TypeAlias = Series[CategoricalDtype] declared under TYPE_CHECKING in pandas-stubs/_typing.pyi.

Design choice

The broader question — re-parameterizing Series by element type, or introducing a CategoricalItem[C1] generic mirroring Interval — was discussed in the issue thread but is genuinely open. Re-parameterizing would break every existing user that wrote Series[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 under TYPE_CHECKING (Series is not subscriptable at runtime; mirrors MaskType/IndexType pattern).
  • tests/series/categorical/test_dtypes.py — 3 new tests: assignment compatibility, cat accessor typing, copy-preserves-parameterization.

Test plan

  • ruff check, black --check, isort --check-only — all clean
  • pyright — 0 errors on changed files
  • pytest — 5/5 categorical tests pass

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant