Skip to content

BUG(?): Allow setitem to category/sparse of the same underlying dtype?  #59627

Open
@mroeschke

Description

@mroeschke

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

In [7]: df = pd.DataFrame(range(2))

In [8]: df.iloc[:, 0] = df.iloc[:, 0].astype("category")

TypeError: Invalid value '0    0
1    1
Name: 0, dtype: category
Categories (2, int64): [0, 1]' for dtype 'int64'

Issue Description

In the PDEP 6 discussions (#39584 #50424), I can't find any discussion really of whether setting values with a different "representation" (e.g. to category or sparse) of the dtype should be disallowed. Technically casting to e.g. category, sparse, ArrowDtype of the same underlying type doesn't upcast, so should that be allowed?

cc @MarcoGorelli

Expected Behavior

Setitem with category/sparse/ArrowDtype that doesn't change the underlying type should be allowed

Installed Versions

Replace this line with the output of pd.show_versions()

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugPDEP6-relatedrelated to PDEP6 (not upcasting during setitem-like Series operations)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions