From acdfc48e4bb3b8b377ba6c13e592469f9b69f5fd Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 18 Jul 2025 10:40:43 -0700 Subject: [PATCH] DEPR: maybe_infer_ndim --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/_libs/internals.pyx | 4 ++-- pandas/core/internals/api.py | 16 ++++++++++++++-- pandas/tests/internals/test_api.py | 9 +++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 1383202154f04..a096e977e3bf3 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -457,6 +457,7 @@ Other Deprecations - Deprecated :meth:`.DataFrameGroupby.corrwith` (:issue:`57158`) - Deprecated :meth:`Timestamp.utcfromtimestamp`, use ``Timestamp.fromtimestamp(ts, "UTC")`` instead (:issue:`56680`) - Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`) +- Deprecated ``pd.core.internals.api.maybe_infer_ndim`` (:issue:`40226`) - Deprecated allowing non-keyword arguments in :meth:`DataFrame.all`, :meth:`DataFrame.min`, :meth:`DataFrame.max`, :meth:`DataFrame.sum`, :meth:`DataFrame.prod`, :meth:`DataFrame.mean`, :meth:`DataFrame.median`, :meth:`DataFrame.sem`, :meth:`DataFrame.var`, :meth:`DataFrame.std`, :meth:`DataFrame.skew`, :meth:`DataFrame.kurt`, :meth:`Series.all`, :meth:`Series.min`, :meth:`Series.max`, :meth:`Series.sum`, :meth:`Series.prod`, :meth:`Series.mean`, :meth:`Series.median`, :meth:`Series.sem`, :meth:`Series.var`, :meth:`Series.std`, :meth:`Series.skew`, and :meth:`Series.kurt`. (:issue:`57087`) - Deprecated allowing non-keyword arguments in :meth:`Series.to_markdown` except ``buf``. (:issue:`57280`) - Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`) diff --git a/pandas/_libs/internals.pyx b/pandas/_libs/internals.pyx index 8caf7e5fb99b2..4fb24c9ad1538 100644 --- a/pandas/_libs/internals.pyx +++ b/pandas/_libs/internals.pyx @@ -709,9 +709,9 @@ cdef class Block: self.ndim = state[2] else: # older pickle - from pandas.core.internals.api import maybe_infer_ndim + from pandas.core.internals.api import _maybe_infer_ndim - ndim = maybe_infer_ndim(self.values, self.mgr_locs) + ndim = _maybe_infer_ndim(self.values, self.mgr_locs) self.ndim = ndim cpdef Block slice_block_rows(self, slice slicer): diff --git a/pandas/core/internals/api.py b/pandas/core/internals/api.py index c5d6a2fe7a6a6..fe2af4e142b55 100644 --- a/pandas/core/internals/api.py +++ b/pandas/core/internals/api.py @@ -136,7 +136,7 @@ def make_block( if not isinstance(placement, BlockPlacement): placement = BlockPlacement(placement) - ndim = maybe_infer_ndim(values, placement, ndim) + ndim = _maybe_infer_ndim(values, placement, ndim) if isinstance(values.dtype, (PeriodDtype, DatetimeTZDtype)): # GH#41168 ensure we can pass 1D dt64tz values # More generally, any EA dtype that isn't is_1d_only_ea_dtype @@ -148,7 +148,7 @@ def make_block( return klass(values, ndim=ndim, placement=placement) -def maybe_infer_ndim(values, placement: BlockPlacement, ndim: int | None) -> int: +def _maybe_infer_ndim(values, placement: BlockPlacement, ndim: int | None) -> int: """ If `ndim` is not provided, infer it from placement and values. """ @@ -162,3 +162,15 @@ def maybe_infer_ndim(values, placement: BlockPlacement, ndim: int | None) -> int else: ndim = values.ndim return ndim + + +def maybe_infer_ndim(values, placement: BlockPlacement, ndim: int | None) -> int: + """ + If `ndim` is not provided, infer it from placement and values. + """ + warnings.warn( + "maybe_infer_ndim is deprecated and will be removed in a future version.", + DeprecationWarning, + stacklevel=2, + ) + return _maybe_infer_ndim(values, placement, ndim) diff --git a/pandas/tests/internals/test_api.py b/pandas/tests/internals/test_api.py index fc222f6987466..f38bed42a3aa2 100644 --- a/pandas/tests/internals/test_api.py +++ b/pandas/tests/internals/test_api.py @@ -79,6 +79,15 @@ def test_create_block_manager_from_blocks_deprecated(): internals.create_block_manager_from_blocks +def test_maybe_infer_ndim_deprecated(): + # GH#40226 + msg = "maybe_infer_ndim is deprecated and will be removed in a future version." + arr = np.arange(5) + bp = pd._libs.internals.BlockPlacement([1]) + with tm.assert_produces_warning(DeprecationWarning, match=msg): + internals.api.maybe_infer_ndim(arr, bp, 1) + + def test_create_dataframe_from_blocks(float_frame): block = float_frame._mgr.blocks[0] index = float_frame.index.copy()