Skip to content

API: Categorical constructor with dtype raise instead of casting to NA #40996

Open
@jbrockmendel

Description

@jbrockmendel
dti = pd.date_range("2016-01-01", periods=3)
ci = pd.Categorical(dti._values)

>>> pd.Categorical(["foo"], dtype=ci.dtype)
[NaT]
Categories (3, datetime64[ns]): [2016-01-01, 2016-01-02, 2016-01-03]

Casting "foo" to pd.NaT here makes very little sense to me, and does not match the behavior of any of our other constructors. I'm pretty sure this is why we have to do so much special-casing for Categorical in dtypes.cast and groupby ops.

Suggestion: we change/deprecate the behavior to raise instead of cast.

Metadata

Metadata

Assignees

No one assigned

    Labels

    API - ConsistencyInternal Consistency of API/BehaviorCategoricalCategorical Data TypeConstructorsSeries/DataFrame/Index/pd.array ConstructorsError ReportingIncorrect or improved errors from pandas

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions