Skip to content

Commit 171cbcd

Browse files
authored
BUG: avoid DeprecationWarning when the Series has index as list of Series (pandas-dev#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
1 parent 7e89905 commit 171cbcd

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

doc/source/whatsnew/v2.1.4.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Fixed regressions
2121

2222
Bug fixes
2323
~~~~~~~~~
24-
-
24+
- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`)
2525
-
2626

2727
.. ---------------------------------------------------------------------------

pandas/_libs/lib.pyx

+8-2
Original file line numberDiff line numberDiff line change
@@ -862,10 +862,16 @@ def is_all_arraylike(obj: list) -> bool:
862862
object val
863863
bint all_arrays = True
864864

865+
from pandas.core.dtypes.generic import (
866+
ABCIndex,
867+
ABCMultiIndex,
868+
ABCSeries,
869+
)
870+
865871
for i in range(n):
866872
val = obj[i]
867-
if not (isinstance(val, list) or
868-
util.is_array(val) or hasattr(val, "_data")):
873+
if (not (isinstance(val, (list, ABCSeries, ABCIndex)) or util.is_array(val))
874+
or isinstance(val, ABCMultiIndex)):
869875
# TODO: EA?
870876
# exclude tuples, frozensets as they may be contained in an Index
871877
all_arrays = False

pandas/tests/series/test_constructors.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -2144,16 +2144,14 @@ def test_series_constructor_datetimelike_index_coercion(self):
21442144
# to DatetimeIndex GH#39307, GH#23598
21452145
assert not isinstance(ser.index, DatetimeIndex)
21462146

2147-
def test_series_constructor_infer_multiindex(self):
2148-
index_lists = [["a", "a", "b", "b"], ["x", "y", "x", "y"]]
2149-
2150-
multi = Series(1.0, index=[np.array(x) for x in index_lists])
2151-
assert isinstance(multi.index, MultiIndex)
2152-
2153-
multi = Series(1.0, index=index_lists)
2154-
assert isinstance(multi.index, MultiIndex)
2155-
2156-
multi = Series(range(4), index=index_lists)
2147+
@pytest.mark.parametrize("container", [None, np.array, Series, Index])
2148+
@pytest.mark.parametrize("data", [1.0, range(4)])
2149+
def test_series_constructor_infer_multiindex(self, container, data):
2150+
indexes = [["a", "a", "b", "b"], ["x", "y", "x", "y"]]
2151+
if container is not None:
2152+
indexes = [container(ind) for ind in indexes]
2153+
2154+
multi = Series(data, index=indexes)
21572155
assert isinstance(multi.index, MultiIndex)
21582156

21592157

0 commit comments

Comments
 (0)