From 391a858ff3057905a7e8cd76b0310ccd6e738025 Mon Sep 17 00:00:00 2001 From: Yao Xiao <108576690+Charlie-XIAO@users.noreply.github.com> Date: Wed, 4 Oct 2023 16:25:14 +0800 Subject: [PATCH 1/6] BUG: avoid DeprecationWarning when the Series has index as list of Series --- doc/source/whatsnew/v2.1.2.rst | 1 + pandas/_libs/lib.pyx | 2 +- pandas/tests/series/test_constructors.py | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.2.rst b/doc/source/whatsnew/v2.1.2.rst index 38ef8c8455b9d..851fab4ce122b 100644 --- a/doc/source/whatsnew/v2.1.2.rst +++ b/doc/source/whatsnew/v2.1.2.rst @@ -22,6 +22,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ +- Fixed bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`) - Fixed bug in :meth:`Categorical.equals` if other has arrow backed string dtype (:issue:`55364`) - Fixed bug in :meth:`DataFrame.idxmin` and :meth:`DataFrame.idxmax` raising for arrow dtypes (:issue:`55368`) - Fixed bug in :meth:`DataFrame.interpolate` raising incorrect error message (:issue:`55347`) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 23c8066b973f8..d5853cbdf6fe9 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -866,7 +866,7 @@ def is_all_arraylike(obj: list) -> bool: for i in range(n): val = obj[i] if not (isinstance(val, list) or - util.is_array(val) or hasattr(val, "_data")): + util.is_array(val) or hasattr(type(val), "_data")): # 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 d45c655a4c0a2..6019d047c7da8 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -2224,3 +2224,9 @@ def test_series_with_complex_nan(input_list): result = Series(ser.array) assert ser.dtype == "complex128" tm.assert_series_equal(ser, result) + + +def test_series_with_multiindex_of_series(): + # GH#55228 + ind = [Series([1]), Series([2])] + Series([1.23], index=ind) From 912620ef2d07198da8f8f36856b684a9096438ba Mon Sep 17 00:00:00 2001 From: Yao Xiao <108576690+Charlie-XIAO@users.noreply.github.com> Date: Wed, 4 Oct 2023 20:14:58 +0800 Subject: [PATCH 2/6] hasattr(type(obj), '_data') not working as expected, for instance 'pytest pandas/tests/indexes/period/test_period.py::TestPeriodIndex::test_with_multi_index' --- pandas/_libs/lib.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index d5853cbdf6fe9..2be7b36534654 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -865,8 +865,8 @@ def is_all_arraylike(obj: list) -> bool: for i in range(n): val = obj[i] - if not (isinstance(val, list) or - util.is_array(val) or hasattr(type(val), "_data")): + if not (isinstance(val, list) or util.is_array(val) + or hasattr(val, "_mgr") or hasattr(val, "_data")): # TODO: EA? # exclude tuples, frozensets as they may be contained in an Index all_arrays = False From 8b75cfe5e5a4c1aae379e3ce9beb5e5bf54db317 Mon Sep 17 00:00:00 2001 From: Yao Xiao <108576690+Charlie-XIAO@users.noreply.github.com> Date: Mon, 9 Oct 2023 09:24:42 +0800 Subject: [PATCH 3/6] adopt jbrockmendel suggestion: runtime import ABC{Index, Series} --- doc/source/whatsnew/v2.1.2.rst | 1 - doc/source/whatsnew/v2.2.0.rst | 1 + pandas/_libs/lib.pyx | 9 +++++++-- pandas/tests/series/test_constructors.py | 24 ++++++++---------------- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/doc/source/whatsnew/v2.1.2.rst b/doc/source/whatsnew/v2.1.2.rst index 14916ba122027..ddd1f95c56aea 100644 --- a/doc/source/whatsnew/v2.1.2.rst +++ b/doc/source/whatsnew/v2.1.2.rst @@ -21,7 +21,6 @@ Fixed regressions Bug fixes ~~~~~~~~~ -- Fixed bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`) - Fixed bug in :meth:`Categorical.equals` if other has arrow backed string dtype (:issue:`55364`) - Fixed bug in :meth:`DataFrame.__setitem__` not inferring string dtype for zero-dimensional array with ``infer_string=True`` (:issue:`55366`) - Fixed bug in :meth:`DataFrame.idxmin` and :meth:`DataFrame.idxmax` raising for arrow dtypes (:issue:`55368`) diff --git a/doc/source/whatsnew/v2.2.0.rst b/doc/source/whatsnew/v2.2.0.rst index cfcc99bf5bda0..c58370d3c2f02 100644 --- a/doc/source/whatsnew/v2.2.0.rst +++ b/doc/source/whatsnew/v2.2.0.rst @@ -390,6 +390,7 @@ Styler Other ^^^^^ +- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`) - Bug in :func:`cut` incorrectly allowing cutting of timezone-aware datetimes with timezone-naive bins (:issue:`54964`) - Bug in :meth:`DataFrame.apply` where passing ``raw=True`` ignored ``args`` passed to the applied function (:issue:`55009`) - Bug in rendering a :class:`Series` with a :class:`MultiIndex` when one of the index level's names is 0 not having that name displayed (:issue:`55415`) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 2be7b36534654..0ef7aed09e811 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -863,10 +863,15 @@ def is_all_arraylike(obj: list) -> bool: object val bint all_arrays = True + from pandas.core.dtypes.generic import ( + ABCIndex, + ABCSeries, + ) + for i in range(n): val = obj[i] - if not (isinstance(val, list) or util.is_array(val) - or hasattr(val, "_mgr") or hasattr(val, "_data")): + if not (isinstance(val, (list, ABCSeries, ABCIndex)) + or util.is_array(val)): # 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 20cee090cf99f..7df7e781153c9 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -2134,16 +2134,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) @@ -2224,9 +2222,3 @@ def test_series_with_complex_nan(input_list): result = Series(ser.array) assert ser.dtype == "complex128" tm.assert_series_equal(ser, result) - - -def test_series_with_multiindex_of_series(): - # GH#55228 - ind = [Series([1]), Series([2])] - Series([1.23], index=ind) From 89807861a8f582b99c1b12b8af9a4d50f0299e28 Mon Sep 17 00:00:00 2001 From: Yao Xiao <108576690+Charlie-XIAO@users.noreply.github.com> Date: Fri, 27 Oct 2023 19:59:55 +0800 Subject: [PATCH 4/6] exclude ABCMultiIndex; move changelog to 2.1.3 --- doc/source/whatsnew/v2.1.3.rst | 2 +- doc/source/whatsnew/v2.2.0.rst | 1 - pandas/_libs/lib.pyx | 5 +++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v2.1.3.rst b/doc/source/whatsnew/v2.1.3.rst index 1359123ef153e..c630396d6884b 100644 --- a/doc/source/whatsnew/v2.1.3.rst +++ b/doc/source/whatsnew/v2.1.3.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/doc/source/whatsnew/v2.2.0.rst b/doc/source/whatsnew/v2.2.0.rst index d14825b90cd0a..ff19ef4eae179 100644 --- a/doc/source/whatsnew/v2.2.0.rst +++ b/doc/source/whatsnew/v2.2.0.rst @@ -438,7 +438,6 @@ Styler Other ^^^^^ -- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`) - Bug in :func:`cut` incorrectly allowing cutting of timezone-aware datetimes with timezone-naive bins (:issue:`54964`) - Bug in :func:`infer_freq` and :meth:`DatetimeIndex.inferred_freq` with weekly frequencies and non-nanosecond resolutions (:issue:`55609`) - Bug in :meth:`DataFrame.apply` where passing ``raw=True`` ignored ``args`` passed to the applied function (:issue:`55009`) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 1ae0dc4ed20da..aeb9d7c263485 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -865,13 +865,14 @@ def is_all_arraylike(obj: list) -> bool: from pandas.core.dtypes.generic import ( ABCIndex, + ABCMultiIndex, ABCSeries, ) for i in range(n): val = obj[i] - if not (isinstance(val, (list, ABCSeries, ABCIndex)) - or util.is_array(val)): + 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 From 2937e03b623587a3072ce66bc983eca220569500 Mon Sep 17 00:00:00 2001 From: Yao Xiao <108576690+Charlie-XIAO@users.noreply.github.com> Date: Tue, 7 Nov 2023 14:47:50 +0800 Subject: [PATCH 5/6] fix changelog order --- doc/source/whatsnew/v2.1.3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.3.rst b/doc/source/whatsnew/v2.1.3.rst index a9c3b271fc7db..7ccf9733d397e 100644 --- a/doc/source/whatsnew/v2.1.3.rst +++ b/doc/source/whatsnew/v2.1.3.rst @@ -21,9 +21,9 @@ Fixed regressions Bug fixes ~~~~~~~~~ +- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`) - Bug in :meth:`DatetimeIndex.diff` raising ``TypeError`` (:issue:`55080`) - Bug in :meth:`Index.isin` raising for Arrow backed string and ``None`` value (:issue:`55821`) -- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`) .. --------------------------------------------------------------------------- .. _whatsnew_213.other: From c5a4da149207a90ac3df0173fccfd317d0fec1c0 Mon Sep 17 00:00:00 2001 From: Yao Xiao <108576690+Charlie-XIAO@users.noreply.github.com> Date: Thu, 16 Nov 2023 10:35:08 +0800 Subject: [PATCH 6/6] moved changelog 2.1.3 -> 2.1.4 --- doc/source/whatsnew/v2.1.3.rst | 1 - doc/source/whatsnew/v2.1.4.rst | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.1.3.rst b/doc/source/whatsnew/v2.1.3.rst index 6548aaedf8e43..af626895a9e0e 100644 --- a/doc/source/whatsnew/v2.1.3.rst +++ b/doc/source/whatsnew/v2.1.3.rst @@ -20,7 +20,6 @@ Fixed regressions Bug fixes ~~~~~~~~~ -- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`) - Bug in :meth:`DatetimeIndex.diff` raising ``TypeError`` (:issue:`55080`) - Bug in :meth:`Index.isin` raising for Arrow backed string and ``None`` value (:issue:`55821`) - Fix :func:`read_parquet` and :func:`read_feather` for `CVE-2023-47248 `__ (:issue:`55894`) 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`) - .. ---------------------------------------------------------------------------