From ba4e647db8806b3e9579c6d9a96b7f6ce34840bb Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 3 Oct 2020 00:02:45 +0100 Subject: [PATCH 1/2] Backport PR #36675: REGR: Series.loc with a MultiIndex containing Timestamp raises InvalidIndexError --- doc/source/whatsnew/v1.1.3.rst | 1 + pandas/core/indexing.py | 2 +- pandas/tests/indexing/multiindex/test_loc.py | 12 ++++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.3.rst b/doc/source/whatsnew/v1.1.3.rst index 15777abcb8084..acf1dafc59885 100644 --- a/doc/source/whatsnew/v1.1.3.rst +++ b/doc/source/whatsnew/v1.1.3.rst @@ -37,6 +37,7 @@ Fixed regressions - Fixed regression in modulo of :class:`Index`, :class:`Series` and :class:`DataFrame` using ``numexpr`` using C not Python semantics (:issue:`36047`, :issue:`36526`) - Fixed regression in :meth:`read_excel` with ``engine="odf"`` caused ``UnboundLocalError`` in some cases where cells had nested child nodes (:issue:`36122`, :issue:`35802`) - Fixed regression in :meth:`DataFrame.replace` inconsistent replace when using a float in the replace method (:issue:`35376`) +- Fixed regression in :meth:`Series.loc` on a :class:`Series` with a :class:`MultiIndex` containing :class:`Timestamp` raising ``InvalidIndexError`` (:issue:`35858`) - Fixed regression in :class:`DataFrame` and :class:`Series` comparisons between numeric arrays and strings (:issue:`35700`, :issue:`36377`) - Fixed regression in :meth:`DataFrame.apply` with ``raw=True`` and user-function returning string (:issue:`35940`) - Fixed regression when setting empty :class:`DataFrame` column to a :class:`Series` in preserving name of index in frame (:issue:`36527`) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 04d1dbceb3342..5a24addf46d93 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1064,7 +1064,7 @@ def _handle_lowerdim_multi_index_axis0(self, tup: Tuple): try: # fast path for series or for tup devoid of slices return self._get_label(tup, axis=axis) - except TypeError: + except (TypeError, InvalidIndexError): # slices are unhashable pass except KeyError as ek: diff --git a/pandas/tests/indexing/multiindex/test_loc.py b/pandas/tests/indexing/multiindex/test_loc.py index 63983f45d7832..1b659bec0e9e8 100644 --- a/pandas/tests/indexing/multiindex/test_loc.py +++ b/pandas/tests/indexing/multiindex/test_loc.py @@ -493,6 +493,18 @@ def test_loc_datetime_mask_slicing(): tm.assert_series_equal(result, expected) +def test_loc_datetime_series_tuple_slicing(): + # https://github.com/pandas-dev/pandas/issues/35858 + date = pd.Timestamp("2000") + ser = pd.Series( + 1, + index=pd.MultiIndex.from_tuples([("a", date)], names=["a", "b"]), + name="c", + ) + result = ser.loc[:, [date]] + tm.assert_series_equal(result, ser) + + def test_loc_with_mi_indexer(): # https://github.com/pandas-dev/pandas/issues/35351 df = DataFrame( From 531f4908573c3722d6f2f6d745ac75823b5ad264 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 3 Oct 2020 11:27:01 +0100 Subject: [PATCH 2/2] format with old black --- pandas/tests/indexing/multiindex/test_loc.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/tests/indexing/multiindex/test_loc.py b/pandas/tests/indexing/multiindex/test_loc.py index 1b659bec0e9e8..95a23a9bcf63b 100644 --- a/pandas/tests/indexing/multiindex/test_loc.py +++ b/pandas/tests/indexing/multiindex/test_loc.py @@ -497,9 +497,7 @@ def test_loc_datetime_series_tuple_slicing(): # https://github.com/pandas-dev/pandas/issues/35858 date = pd.Timestamp("2000") ser = pd.Series( - 1, - index=pd.MultiIndex.from_tuples([("a", date)], names=["a", "b"]), - name="c", + 1, index=pd.MultiIndex.from_tuples([("a", date)], names=["a", "b"]), name="c", ) result = ser.loc[:, [date]] tm.assert_series_equal(result, ser)