diff --git a/doc/source/whatsnew/v2.1.4.rst b/doc/source/whatsnew/v2.1.4.rst index 04bbb0f806cbd..25afcbb3bb532 100644 --- a/doc/source/whatsnew/v2.1.4.rst +++ b/doc/source/whatsnew/v2.1.4.rst @@ -21,7 +21,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ -- +- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`) - .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index e8a69891c6093..2940a5956faff 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -865,10 +865,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 195c50969ffae..57cc674754cc7 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -2144,16 +2144,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)