Skip to content

BUG: astype('category') on dataframe backed by non-writeable arrays raises ValueError #53678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ Bug fixes

Categorical
^^^^^^^^^^^
- Bug in :meth:`Series.astype` with ``dtype="category"`` for nullable arrays with read-only null value masks (:issue:`53658`)
- Bug in :meth:`Series.map` , where the value of the ``na_action`` parameter was not used if the series held a :class:`Categorical` (:issue:`22527`).
-

Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/hashtable_class_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ cdef class {{name}}HashTable(HashTable):
UInt8Vector result_mask
UInt8VectorData *rmd
bint use_na_value, use_mask, seen_na = False
uint8_t[:] mask_values
const uint8_t[:] mask_values

if return_inverse:
labels = np.empty(n, dtype=np.intp)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/extension/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,12 @@ def test_repr_2d(self, data):

res = repr(data.reshape(-1, 1))
assert res.count("\nCategories") == 1


def test_astype_category_readonly_mask_values():
# GH 53658
df = pd.DataFrame([0, 1, 2], dtype="Int64")
df._mgr.arrays[0]._mask.flags["WRITEABLE"] = False
result = df.astype("category")
expected = pd.DataFrame([0, 1, 2], dtype="Int64").astype("category")
tm.assert_frame_equal(result, expected)