Skip to content

Commit 0e9ffbe

Browse files
BUG: avoid DeprecationWarning when the Series has index as list of Series (pandas-dev#55395) (pandas-dev#56325)
* 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 171cbcd) Co-authored-by: Yao Xiao <[email protected]>
1 parent e9ce3ea commit 0e9ffbe

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

doc/source/whatsnew/v2.1.4.rst

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

2222
Bug fixes
2323
~~~~~~~~~
24+
- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`)
2425
- Bug in :meth:`DataFrame.apply` where passing ``raw=True`` ignored ``args`` passed to the applied function (:issue:`55753`)
2526
- Bug in :meth:`Index.__getitem__` returning wrong result for Arrow dtypes and negative stepsize (:issue:`55832`)
2627
- Fixed bug in :func:`to_numeric` converting to extension dtype for ``string[pyarrow_numpy]`` dtype (:issue:`56179`)
@@ -29,6 +30,7 @@ Bug fixes
2930
- Fixed bug in :meth:`DataFrame.to_hdf` raising when columns have ``StringDtype`` (:issue:`55088`)
3031
- Fixed bug in :meth:`Index.insert` casting object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`)
3132
- Fixed bug in :meth:`Series.str.translate` losing object dtype when string option is set (:issue:`56152`)
33+
-
3234

3335
.. ---------------------------------------------------------------------------
3436
.. _whatsnew_214.other:

pandas/_libs/lib.pyx

+8-2
Original file line numberDiff line numberDiff line change
@@ -840,10 +840,16 @@ def is_all_arraylike(obj: list) -> bool:
840840
object val
841841
bint all_arrays = True
842842

843+
from pandas.core.dtypes.generic import (
844+
ABCIndex,
845+
ABCMultiIndex,
846+
ABCSeries,
847+
)
848+
843849
for i in range(n):
844850
val = obj[i]
845-
if not (isinstance(val, list) or
846-
util.is_array(val) or hasattr(val, "_data")):
851+
if (not (isinstance(val, (list, ABCSeries, ABCIndex)) or util.is_array(val))
852+
or isinstance(val, ABCMultiIndex)):
847853
# TODO: EA?
848854
# exclude tuples, frozensets as they may be contained in an Index
849855
all_arrays = False

pandas/tests/series/test_constructors.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -2150,16 +2150,14 @@ def test_series_constructor_datetimelike_index_coercion(self):
21502150
# to DatetimeIndex GH#39307, GH#23598
21512151
assert not isinstance(ser.index, DatetimeIndex)
21522152

2153-
def test_series_constructor_infer_multiindex(self):
2154-
index_lists = [["a", "a", "b", "b"], ["x", "y", "x", "y"]]
2155-
2156-
multi = Series(1.0, index=[np.array(x) for x in index_lists])
2157-
assert isinstance(multi.index, MultiIndex)
2158-
2159-
multi = Series(1.0, index=index_lists)
2160-
assert isinstance(multi.index, MultiIndex)
2161-
2162-
multi = Series(range(4), index=index_lists)
2153+
@pytest.mark.parametrize("container", [None, np.array, Series, Index])
2154+
@pytest.mark.parametrize("data", [1.0, range(4)])
2155+
def test_series_constructor_infer_multiindex(self, container, data):
2156+
indexes = [["a", "a", "b", "b"], ["x", "y", "x", "y"]]
2157+
if container is not None:
2158+
indexes = [container(ind) for ind in indexes]
2159+
2160+
multi = Series(data, index=indexes)
21632161
assert isinstance(multi.index, MultiIndex)
21642162

21652163

0 commit comments

Comments
 (0)