Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ Other deprecations
- 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`)
- :meth:`Index.contains` is deprecated. Use ``key in index`` (``__contains__``) instead (:issue:`17753`).
- :meth:`DataFrame.get_dtype_counts` is deprecated. (:issue:`18262`)
- :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
3 changes: 3 additions & 0 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,9 @@ def ravel(self, order='C'):
-------
numpy.array
"""
warn("Categorical.ravel will return a Categorical object instead "
"of an ndarray in a future version.",
FutureWarning, stacklevel=2)
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]
7 changes: 6 additions & 1 deletion pandas/tests/extension/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from pandas import Categorical
from pandas.api.types import CategoricalDtype
from pandas.tests.extension import base
import pandas.util.testing as tm


def make_data():
Expand Down Expand Up @@ -94,7 +95,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 tm.assert_produces_warning(FutureWarning):
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