Skip to content

Commit 1659fff

Browse files
jbrockmendeljreback
authored andcommitted
DEPR: make Categorical.ravel() return Categorical (#27199)
1 parent 47ffcd6 commit 1659fff

File tree

7 files changed

+39
-2
lines changed

7 files changed

+39
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ Other deprecations
862862
- The default value ``ordered=None`` in :class:`~pandas.api.types.CategoricalDtype` has been deprecated in favor of ``ordered=False``. When converting between categorical types ``ordered=True`` must be explicitly passed in order to be preserved. (:issue:`26336`)
863863
- :meth:`Index.contains` is deprecated. Use ``key in index`` (``__contains__``) instead (:issue:`17753`).
864864
- :meth:`DataFrame.get_dtype_counts` is deprecated. (:issue:`18262`)
865+
- :meth:`Categorical.ravel` will return a :class:`Categorical` instead of a ``np.ndarray`` (:issue:`27199`)
865866
866867
.. _whatsnew_0250.prior_deprecations:
867868

pandas/core/arrays/base.py

+15
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,21 @@ def _formatting_values(self) -> np.ndarray:
909909
# Reshaping
910910
# ------------------------------------------------------------------------
911911

912+
def ravel(self, order="C") -> ABCExtensionArray:
913+
"""
914+
Return a flattened view on this array.
915+
916+
Parameters
917+
----------
918+
order : {None, 'C', 'F', 'A', 'K'}, default 'C'
919+
920+
Notes
921+
-----
922+
- Because ExtensionArrays are 1D-only, this is a no-op.
923+
- The "order" argument is ignored, is for compatibility with NumPy.
924+
"""
925+
return self
926+
912927
@classmethod
913928
def _concat_same_type(
914929
cls,

pandas/core/arrays/categorical.py

+3
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,9 @@ def ravel(self, order='C'):
17131713
-------
17141714
numpy.array
17151715
"""
1716+
warn("Categorical.ravel will return a Categorical object instead "
1717+
"of an ndarray in a future version.",
1718+
FutureWarning, stacklevel=2)
17161719
return np.array(self)
17171720

17181721
def view(self):

pandas/tests/extension/base/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
class BaseExtensionTests:
5+
56
assert_equal = staticmethod(tm.assert_equal)
67
assert_series_equal = staticmethod(tm.assert_series_equal)
78
assert_frame_equal = staticmethod(tm.assert_frame_equal)

pandas/tests/extension/base/reshaping.py

+9
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,12 @@ def test_unstack(self, data, index, obj):
269269
result = result.astype(object)
270270

271271
self.assert_frame_equal(result, expected)
272+
273+
def test_ravel(self, data):
274+
# as long as EA is 1D-only, ravel is a no-op
275+
result = data.ravel()
276+
assert type(result) == type(data)
277+
278+
# Check that we have a view, not a copy
279+
result[0] = result[1]
280+
assert data[0] == data[1]

pandas/tests/extension/test_categorical.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from pandas import Categorical
2323
from pandas.api.types import CategoricalDtype
2424
from pandas.tests.extension import base
25+
import pandas.util.testing as tm
2526

2627

2728
def make_data():
@@ -94,7 +95,11 @@ class TestConstructors(base.BaseConstructorsTests):
9495

9596

9697
class TestReshaping(base.BaseReshapingTests):
97-
pass
98+
99+
def test_ravel(self, data):
100+
# GH#27199 Categorical.ravel returns self until after deprecation cycle
101+
with tm.assert_produces_warning(FutureWarning):
102+
data.ravel()
98103

99104

100105
class TestGetitem(base.BaseGetitemTests):

pandas/tests/extension/test_sparse.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,14 @@ def data_for_grouping(request):
8282

8383

8484
class BaseSparseTests:
85-
8685
def _check_unsupported(self, data):
8786
if data.dtype == SparseDtype(int, 0):
8887
pytest.skip("Can't store nan in int array.")
8988

89+
@pytest.mark.xfail(reason="SparseArray does not support setitem")
90+
def test_ravel(self, data):
91+
super().test_ravel(data)
92+
9093

9194
class TestDtype(BaseSparseTests, base.BaseDtypeTests):
9295

0 commit comments

Comments
 (0)