Skip to content

Commit 3934e56

Browse files
DEPR: Deprecate ravel (#56053)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent cf78582 commit 3934e56

File tree

8 files changed

+26
-5
lines changed

8 files changed

+26
-5
lines changed

doc/source/whatsnew/v2.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ Other Deprecations
362362
- 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`)
363363
- 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`)
364364
- Deprecated :meth:`Index.format`, use ``index.astype(str)`` or ``index.map(formatter)`` instead (:issue:`55413`)
365+
- Deprecated :meth:`Series.ravel`, the underlying array is already 1D, so ravel is not necessary (:issue:`52511`)
365366
- Deprecated :meth:`Series.view`, use ``astype`` instead to change the dtype (:issue:`20251`)
366367
- Deprecated ``core.internals`` members ``Block``, ``ExtensionBlock``, and ``DatetimeTZBlock``, use public APIs instead (:issue:`55139`)
367368
- Deprecated ``year``, ``month``, ``quarter``, ``day``, ``hour``, ``minute``, and ``second`` keywords in the :class:`PeriodIndex` constructor, use :meth:`PeriodIndex.from_fields` instead (:issue:`55960`)

pandas/_testing/asserters.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ def assert_is_valid_plot_return_object(objs) -> None:
427427
from matplotlib.axes import Axes
428428

429429
if isinstance(objs, (Series, np.ndarray)):
430+
if isinstance(objs, Series):
431+
objs = objs._values
430432
for el in objs.ravel():
431433
msg = (
432434
"one of 'objs' is not a matplotlib Axes instance, "

pandas/core/dtypes/cast.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ def maybe_downcast_to_dtype(result: ArrayLike, dtype: str | np.dtype) -> ArrayLi
261261
try to cast to the specified dtype (e.g. convert back to bool/int
262262
or could be an astype of float64->float32
263263
"""
264+
if isinstance(result, ABCSeries):
265+
result = result._values
264266
do_round = False
265267

266268
if isinstance(dtype, str):

pandas/core/reshape/pivot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ def _add_margins(
309309

310310
row_names = result.index.names
311311
# check the result column and leave floats
312+
312313
for dtype in set(result.dtypes):
313314
if isinstance(dtype, ExtensionDtype):
314315
# Can hold NA already

pandas/core/series.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,11 @@ def ravel(self, order: str = "C") -> ArrayLike:
848848
"""
849849
Return the flattened underlying data as an ndarray or ExtensionArray.
850850
851+
.. deprecated:: 2.2.0
852+
Series.ravel is deprecated. The underlying array is already 1D, so
853+
ravel is not necessary. Use :meth:`to_numpy` for conversion to a numpy
854+
array instead.
855+
851856
Returns
852857
-------
853858
numpy.ndarray or ExtensionArray
@@ -860,9 +865,16 @@ def ravel(self, order: str = "C") -> ArrayLike:
860865
Examples
861866
--------
862867
>>> s = pd.Series([1, 2, 3])
863-
>>> s.ravel()
868+
>>> s.ravel() # doctest: +SKIP
864869
array([1, 2, 3])
865870
"""
871+
warnings.warn(
872+
"Series.ravel is deprecated. The underlying array is already 1D, so "
873+
"ravel is not necessary. Use `to_numpy()` for conversion to a numpy "
874+
"array instead.",
875+
FutureWarning,
876+
stacklevel=2,
877+
)
866878
arr = self._values.ravel(order=order)
867879
if isinstance(arr, np.ndarray) and using_copy_on_write():
868880
arr.flags.writeable = False

pandas/tests/copy_view/test_array.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ def test_series_to_numpy(using_copy_on_write):
116116
@pytest.mark.parametrize("order", ["F", "C"])
117117
def test_ravel_read_only(using_copy_on_write, order):
118118
ser = Series([1, 2, 3])
119-
arr = ser.ravel(order=order)
119+
with tm.assert_produces_warning(FutureWarning, match="is deprecated"):
120+
arr = ser.ravel(order=order)
120121
if using_copy_on_write:
121122
assert arr.flags.writeable is False
122123
assert np.shares_memory(get_array(ser), arr)

pandas/tests/dtypes/cast/test_downcast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def test_downcast_booleans():
5656
ser = Series([True, True, False])
5757
result = maybe_downcast_to_dtype(ser, np.dtype(np.float64))
5858

59-
expected = ser
60-
tm.assert_series_equal(result, expected)
59+
expected = ser.values
60+
tm.assert_numpy_array_equal(result, expected)
6161

6262

6363
def test_downcast_conversion_no_nan(any_real_numpy_dtype):

pandas/tests/series/test_api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ def test_ndarray_compat_like_func(self):
140140
def test_ndarray_compat_ravel(self):
141141
# ravel
142142
s = Series(np.random.default_rng(2).standard_normal(10))
143-
tm.assert_almost_equal(s.ravel(order="F"), s.values.ravel(order="F"))
143+
with tm.assert_produces_warning(FutureWarning, match="ravel is deprecated"):
144+
result = s.ravel(order="F")
145+
tm.assert_almost_equal(result, s.values.ravel(order="F"))
144146

145147
def test_empty_method(self):
146148
s_empty = Series(dtype=object)

0 commit comments

Comments
 (0)