From 8ca197d9626d571818d591201a9a4a7196dae628 Mon Sep 17 00:00:00 2001 From: Yao Xiao <108576690+Charlie-XIAO@users.noreply.github.com> Date: Fri, 17 Nov 2023 01:43:59 +0800 Subject: [PATCH] BUG: avoid DeprecationWarning when the Series has index as list of Series (#55395) * BUG: avoid DeprecationWarning when the Series has index as list of Series * hasattr(type(obj), '_data') not working as expected, for instance 'pytest pandas/tests/indexes/period/test_period.py::TestPeriodIndex::test_with_multi_index' * adopt jbrockmendel suggestion: runtime import ABC{Index, Series} * exclude ABCMultiIndex; move changelog to 2.1.3 * fix changelog order * moved changelog 2.1.3 -> 2.1.4 (cherry picked from commit 171cbcd3a6e79a5b845f39d2304d664bec10b9db) --- doc/source/whatsnew/v2.1.4.rst | 2 ++ pandas/_libs/lib.pyx | 10 ++++++++-- pandas/tests/series/test_constructors.py | 18 ++++++++---------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/doc/source/whatsnew/v2.1.4.rst b/doc/source/whatsnew/v2.1.4.rst index 3a72c0864d29c..890c4f6c09e45 100644 --- a/doc/source/whatsnew/v2.1.4.rst +++ b/doc/source/whatsnew/v2.1.4.rst @@ -21,6 +21,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ +- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`) - Bug in :meth:`DataFrame.apply` where passing ``raw=True`` ignored ``args`` passed to the applied function (:issue:`55753`) - Bug in :meth:`Index.__getitem__` returning wrong result for Arrow dtypes and negative stepsize (:issue:`55832`) - Fixed bug in :func:`to_numeric` converting to extension dtype for ``string[pyarrow_numpy]`` dtype (:issue:`56179`) @@ -28,6 +29,7 @@ Bug fixes - Fixed bug in :meth:`DataFrame.to_hdf` raising when columns have ``StringDtype`` (:issue:`55088`) - Fixed bug in :meth:`Index.insert` casting object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`) - Fixed bug in :meth:`Series.str.translate` losing object dtype when string option is set (:issue:`56152`) +- .. --------------------------------------------------------------------------- .. _whatsnew_214.other: diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 704a5f12204c4..2fb3da5c65d14 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -840,10 +840,16 @@ def is_all_arraylike(obj: list) -> bool: object val bint all_arrays = True + from pandas.core.dtypes.generic import ( + ABCIndex, + ABCMultiIndex, + ABCSeries, + ) + for i in range(n): val = obj[i] - if not (isinstance(val, list) or - util.is_array(val) or hasattr(val, "_data")): + if (not (isinstance(val, (list, ABCSeries, ABCIndex)) or util.is_array(val)) + or isinstance(val, ABCMultiIndex)): # TODO: EA? # exclude tuples, frozensets as they may be contained in an Index all_arrays = False diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index b74ee5cf8f2bc..d585a56665901 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -2150,16 +2150,14 @@ def test_series_constructor_datetimelike_index_coercion(self): # to DatetimeIndex GH#39307, GH#23598 assert not isinstance(ser.index, DatetimeIndex) - def test_series_constructor_infer_multiindex(self): - index_lists = [["a", "a", "b", "b"], ["x", "y", "x", "y"]] - - multi = Series(1.0, index=[np.array(x) for x in index_lists]) - assert isinstance(multi.index, MultiIndex) - - multi = Series(1.0, index=index_lists) - assert isinstance(multi.index, MultiIndex) - - multi = Series(range(4), index=index_lists) + @pytest.mark.parametrize("container", [None, np.array, Series, Index]) + @pytest.mark.parametrize("data", [1.0, range(4)]) + def test_series_constructor_infer_multiindex(self, container, data): + indexes = [["a", "a", "b", "b"], ["x", "y", "x", "y"]] + if container is not None: + indexes = [container(ind) for ind in indexes] + + multi = Series(data, index=indexes) assert isinstance(multi.index, MultiIndex)