Skip to content

Commit

Permalink
BUG: avoid DeprecationWarning when the Series has index as list of Se…
Browse files Browse the repository at this point in the history
…ries (#55395) (#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]>
  • Loading branch information
mroeschke and Charlie-XIAO authored Dec 4, 2023
1 parent e9ce3ea commit 0e9ffbe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -29,6 +30,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:
Expand Down
10 changes: 8 additions & 2 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 8 additions & 10 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down

0 comments on commit 0e9ffbe

Please sign in to comment.