Skip to content

DEPR: Deprecate ravel #56053

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 15 commits into from
Nov 27, 2023
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ Other Deprecations
- Deprecated :func:`read_gbq` and :meth:`DataFrame.to_gbq`. Use ``pandas_gbq.read_gbq`` and ``pandas_gbq.to_gbq`` instead https://pandas-gbq.readthedocs.io/en/latest/api.html (:issue:`55525`)
- Deprecated :meth:`.DataFrameGroupBy.fillna` and :meth:`.SeriesGroupBy.fillna`; use :meth:`.DataFrameGroupBy.ffill`, :meth:`.DataFrameGroupBy.bfill` for forward and backward filling or :meth:`.DataFrame.fillna` to fill with a single value (or the Series equivalents) (:issue:`55718`)
- Deprecated :meth:`Index.format`, use ``index.astype(str)`` or ``index.map(formatter)`` instead (:issue:`55413`)
- Deprecated :meth:`Series.ravel`, the underlying array is already 1D, so ravel is not necessary (:issue:`52511`)
- Deprecated ``core.internals`` members ``Block``, ``ExtensionBlock``, and ``DatetimeTZBlock``, use public APIs instead (:issue:`55139`)
- Deprecated ``year``, ``month``, ``quarter``, ``day``, ``hour``, ``minute``, and ``second`` keywords in the :class:`PeriodIndex` constructor, use :meth:`PeriodIndex.from_fields` instead (:issue:`55960`)
- Deprecated allowing non-integer ``periods`` argument in :func:`date_range`, :func:`timedelta_range`, :func:`period_range`, and :func:`interval_range` (:issue:`56036`)
Expand Down
2 changes: 2 additions & 0 deletions pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ def assert_is_valid_plot_return_object(objs) -> None:
from matplotlib.axes import Axes

if isinstance(objs, (Series, np.ndarray)):
if isinstance(objs, Series):
objs = objs._values
for el in objs.ravel():
msg = (
"one of 'objs' is not a matplotlib Axes instance, "
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ def maybe_downcast_to_dtype(result: ArrayLike, dtype: str | np.dtype) -> ArrayLi
try to cast to the specified dtype (e.g. convert back to bool/int
or could be an astype of float64->float32
"""
if isinstance(result, ABCSeries):
result = result._values
do_round = False

if isinstance(dtype, str):
Expand Down
1 change: 1 addition & 0 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ def _add_margins(

row_names = result.index.names
# check the result column and leave floats

for dtype in set(result.dtypes):
if isinstance(dtype, ExtensionDtype):
# Can hold NA already
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,8 @@ def ravel(self, order: str = "C") -> ArrayLike:
"""
Return the flattened underlying data as an ndarray or ExtensionArray.

.. deprecated:: 2.2.0
Copy link
Member

Choose a reason for hiding this comment

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

Can you add the content of the warning message here as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

added


Returns
-------
numpy.ndarray or ExtensionArray
Expand All @@ -858,9 +860,15 @@ def ravel(self, order: str = "C") -> ArrayLike:
Examples
--------
>>> s = pd.Series([1, 2, 3])
>>> s.ravel()
>>> s.ravel() # doctest: +SKIP
array([1, 2, 3])
"""
warnings.warn(
"Series.ravel is deprecated. The underlying array is already 1D, so "
"ravel is not necessary.",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"ravel is not necessary.",
"ravel is not necessary. Use `to_numpy()` for conversion to a numpy array instead.",

Copy link
Member Author

Choose a reason for hiding this comment

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

added

FutureWarning,
stacklevel=2,
)
arr = self._values.ravel(order=order)
if isinstance(arr, np.ndarray) and using_copy_on_write():
arr.flags.writeable = False
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/copy_view/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def test_series_to_numpy(using_copy_on_write):
@pytest.mark.parametrize("order", ["F", "C"])
def test_ravel_read_only(using_copy_on_write, order):
ser = Series([1, 2, 3])
arr = ser.ravel(order=order)
with tm.assert_produces_warning(FutureWarning, match="is deprecated"):
arr = ser.ravel(order=order)
if using_copy_on_write:
assert arr.flags.writeable is False
assert np.shares_memory(get_array(ser), arr)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/dtypes/cast/test_downcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def test_downcast_booleans():
ser = Series([True, True, False])
result = maybe_downcast_to_dtype(ser, np.dtype(np.float64))

expected = ser
tm.assert_series_equal(result, expected)
expected = ser.values
tm.assert_numpy_array_equal(result, expected)


def test_downcast_conversion_no_nan(any_real_numpy_dtype):
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def test_ndarray_compat_like_func(self):
def test_ndarray_compat_ravel(self):
# ravel
s = Series(np.random.default_rng(2).standard_normal(10))
tm.assert_almost_equal(s.ravel(order="F"), s.values.ravel(order="F"))
with tm.assert_produces_warning(FutureWarning, match="ravel is deprecated"):
result = s.ravel(order="F")
tm.assert_almost_equal(result, s.values.ravel(order="F"))

def test_empty_method(self):
s_empty = Series(dtype=object)
Expand Down