Skip to content

Commit 0e28257

Browse files
authored
Backport PR #53652: BUG: Indexing a timestamp ArrowDtype Index (#53755)
* Backport PR #53652: BUG: Indexing a timestamp ArrowDtype Index * Check for np.dtype only
1 parent 7c03432 commit 0e28257

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

doc/source/whatsnew/v2.0.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Bug fixes
2626
- Bug in :func:`read_csv` when defining ``dtype`` with ``bool[pyarrow]`` for the ``"c"`` and ``"python"`` engines (:issue:`53390`)
2727
- Bug in :meth:`Series.str.split` and :meth:`Series.str.rsplit` with ``expand=True`` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`53532`)
2828
- Bug in indexing methods (e.g. :meth:`DataFrame.__getitem__`) where taking the entire :class:`DataFrame`/:class:`Series` would raise an ``OverflowError`` when Copy on Write was enabled and the length of the array was over the maximum size a 32-bit integer can hold (:issue:`53616`)
29+
- Bug when indexing a :class:`DataFrame` or :class:`Series` with an :class:`Index` with a timestamp :class:`ArrowDtype` would raise an ``AttributeError`` (:issue:`53644`)
2930

3031
.. ---------------------------------------------------------------------------
3132
.. _whatsnew_203.other:

pandas/core/indexes/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5880,7 +5880,9 @@ def _get_indexer_strict(self, key, axis_name: str_t) -> tuple[Index, np.ndarray]
58805880
if isinstance(key, Index):
58815881
# GH 42790 - Preserve name from an Index
58825882
keyarr.name = key.name
5883-
if keyarr.dtype.kind in ["m", "M"]:
5883+
if (
5884+
isinstance(keyarr.dtype, np.dtype) and keyarr.dtype.kind in ["m", "M"]
5885+
) or isinstance(keyarr.dtype, DatetimeTZDtype):
58845886
# DTI/TDI.take can infer a freq in some cases when we dont want one
58855887
if isinstance(key, list) or (
58865888
isinstance(key, type(self))

pandas/tests/indexing/test_datetime.py

+18
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,21 @@ def test_getitem_str_slice_millisecond_resolution(self, frame_or_series):
168168
],
169169
)
170170
tm.assert_equal(result, expected)
171+
172+
def test_getitem_pyarrow_index(self, frame_or_series):
173+
# GH 53644
174+
pytest.importorskip("pyarrow")
175+
obj = frame_or_series(
176+
range(5),
177+
index=date_range("2020", freq="D", periods=5).astype(
178+
"timestamp[us][pyarrow]"
179+
),
180+
)
181+
result = obj.loc[obj.index[:-3]]
182+
expected = frame_or_series(
183+
range(2),
184+
index=date_range("2020", freq="D", periods=2).astype(
185+
"timestamp[us][pyarrow]"
186+
),
187+
)
188+
tm.assert_equal(result, expected)

0 commit comments

Comments
 (0)