Skip to content

Commit bb9ac7f

Browse files
jbrockmendeljreback
authored andcommitted
BUG: Categorical.view not accepting integer dtypes GH#25464 (pandas-dev#45300)
Co-authored-by: Jeff Reback <[email protected]>
1 parent 006173c commit bb9ac7f

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ Bug fixes
217217

218218
Categorical
219219
^^^^^^^^^^^
220+
- Bug in :meth:`Categorical.view` not accepting integer dtypes (:issue:`25464`)
220221
- 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`)
221222
-
222223

pandas/core/arrays/categorical.py

-5
Original file line numberDiff line numberDiff line change
@@ -1911,11 +1911,6 @@ def _values_for_rank(self):
19111911
)
19121912
return values
19131913

1914-
def view(self, dtype=None):
1915-
if dtype is not None:
1916-
raise NotImplementedError(dtype)
1917-
return self._from_backing_data(self._ndarray)
1918-
19191914
def to_dense(self) -> np.ndarray:
19201915
"""
19211916
Return my 'dense' representation

pandas/tests/indexes/categorical/test_category.py

+18
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,24 @@ def test_map_str(self):
290290
class TestCategoricalIndex2:
291291
# Tests that are not overriding a test in Base
292292

293+
def test_view_i8(self):
294+
# GH#25464
295+
ci = tm.makeCategoricalIndex(100)
296+
msg = "When changing to a larger dtype, its size must be a divisor"
297+
with pytest.raises(ValueError, match=msg):
298+
ci.view("i8")
299+
with pytest.raises(ValueError, match=msg):
300+
ci._data.view("i8")
301+
302+
ci = ci[:-4] # length divisible by 8
303+
304+
res = ci.view("i8")
305+
expected = ci._data.codes.view("i8")
306+
tm.assert_numpy_array_equal(res, expected)
307+
308+
cat = ci._data
309+
tm.assert_numpy_array_equal(cat.view("i8"), expected)
310+
293311
@pytest.mark.parametrize(
294312
"dtype, engine_type",
295313
[

0 commit comments

Comments
 (0)