Skip to content
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

DEPR: make Categorical.ravel() return Categorical #27199

Merged
merged 7 commits into from
Jul 3, 2019
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ Other deprecations
- :meth:`Series.put` is deprecated. (:issue:`18262`)
- :meth:`Index.item` and :meth:`Series.item` is deprecated. (:issue:`18262`)
- :meth:`Index.contains` is deprecated. Use ``key in index`` (``__contains__``) instead (:issue:`17753`).
- :meth:`Categorical.ravel` will return a :class:`Categorical` instead of a ``np.ndarray`` (:issue:`27199`)

.. _whatsnew_0250.prior_deprecations:

Expand Down
15 changes: 15 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,21 @@ def _formatting_values(self) -> np.ndarray:
# Reshaping
# ------------------------------------------------------------------------

def ravel(self, order="C") -> ABCExtensionArray:
"""
Return a flattened view on this array.

Parameters
----------
order : {None, 'C', 'F', 'A', 'K'}, default 'C'

Notes
-----
- Because ExtensionArrays are 1D-only, this is a no-op.
- The "order" argument is ignored, is for compatibility with NumPy.
"""
return self

@classmethod
def _concat_same_type(
cls,
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,8 @@ def ravel(self, order='C'):
-------
numpy.array
"""
warn("Categorical.ravel will return a Categorical object instead "
"of an ndarray in a future version.", FutureWarning)
return np.array(self)

def view(self):
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/extension/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@


class BaseExtensionTests:
# Whether the EA being tested supports __setitem__
_supports_setitem = True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is ok, but in reality you should have a class which does this (future PR)


assert_equal = staticmethod(tm.assert_equal)
assert_series_equal = staticmethod(tm.assert_series_equal)
assert_frame_equal = staticmethod(tm.assert_frame_equal)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/extension/base/reshaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,13 @@ def test_unstack(self, data, index, obj):
result = result.astype(object)

self.assert_frame_equal(result, expected)

def test_ravel(self, data):
# as long as EA is 1D-only, ravel is a no-op
result = data.ravel()
assert type(result) == type(data)

if self._supports_setitem:
# Check that we have a view, not a copy
result[0] = result[1]
assert data[0] == data[1]
6 changes: 5 additions & 1 deletion pandas/tests/extension/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ class TestConstructors(base.BaseConstructorsTests):


class TestReshaping(base.BaseReshapingTests):
pass

def test_ravel(self, data):
# GH#27199 Categorical.ravel returns self until after deprecation cycle
with pytest.warns(FutureWarning, match="will return a Categorical"):
data.ravel()


class TestGetitem(base.BaseGetitemTests):
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def data_for_grouping(request):


class BaseSparseTests:
_supports_setitem = False

def _check_unsupported(self, data):
if data.dtype == SparseDtype(int, 0):
Expand Down