Skip to content

BUG: Categorical.view not accepting integer dtypes GH#25464 #45300

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 2 commits into from
Feb 5, 2022
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/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ Bug fixes

Categorical
^^^^^^^^^^^
- Bug in :meth:`Categorical.view` not accepting integer dtypes (:issue:`25464`)
- Bug in :meth:`CategoricalIndex.union` when the index's categories are integer-dtype and the index contains ``NaN`` values incorrectly raising instead of casting to ``float64`` (:issue:`45362`)
-

Expand Down
5 changes: 0 additions & 5 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1911,11 +1911,6 @@ def _values_for_rank(self):
)
return values

def view(self, dtype=None):
if dtype is not None:
raise NotImplementedError(dtype)
return self._from_backing_data(self._ndarray)

def to_dense(self) -> np.ndarray:
"""
Return my 'dense' representation
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/indexes/categorical/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,24 @@ def test_map_str(self):
class TestCategoricalIndex2:
# Tests that are not overriding a test in Base

def test_view_i8(self):
# GH#25464
ci = tm.makeCategoricalIndex(100)
msg = "When changing to a larger dtype, its size must be a divisor"
with pytest.raises(ValueError, match=msg):
ci.view("i8")
with pytest.raises(ValueError, match=msg):
ci._data.view("i8")

ci = ci[:-4] # length divisible by 8

res = ci.view("i8")
expected = ci._data.codes.view("i8")
tm.assert_numpy_array_equal(res, expected)

cat = ci._data
tm.assert_numpy_array_equal(cat.view("i8"), expected)

@pytest.mark.parametrize(
"dtype, engine_type",
[
Expand Down