Skip to content

Commit

Permalink
Backport PR #53652: BUG: Indexing a timestamp ArrowDtype Index (#53755)
Browse files Browse the repository at this point in the history
* Backport PR #53652: BUG: Indexing a timestamp ArrowDtype Index

* Check for np.dtype only
  • Loading branch information
mroeschke committed Jun 21, 2023
1 parent 7c03432 commit 0e28257
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Bug fixes
- Bug in :func:`read_csv` when defining ``dtype`` with ``bool[pyarrow]`` for the ``"c"`` and ``"python"`` engines (:issue:`53390`)
- Bug in :meth:`Series.str.split` and :meth:`Series.str.rsplit` with ``expand=True`` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`53532`)
- 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`)
- Bug when indexing a :class:`DataFrame` or :class:`Series` with an :class:`Index` with a timestamp :class:`ArrowDtype` would raise an ``AttributeError`` (:issue:`53644`)

.. ---------------------------------------------------------------------------
.. _whatsnew_203.other:
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5880,7 +5880,9 @@ def _get_indexer_strict(self, key, axis_name: str_t) -> tuple[Index, np.ndarray]
if isinstance(key, Index):
# GH 42790 - Preserve name from an Index
keyarr.name = key.name
if keyarr.dtype.kind in ["m", "M"]:
if (
isinstance(keyarr.dtype, np.dtype) and keyarr.dtype.kind in ["m", "M"]
) or isinstance(keyarr.dtype, DatetimeTZDtype):
# DTI/TDI.take can infer a freq in some cases when we dont want one
if isinstance(key, list) or (
isinstance(key, type(self))
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,21 @@ def test_getitem_str_slice_millisecond_resolution(self, frame_or_series):
],
)
tm.assert_equal(result, expected)

def test_getitem_pyarrow_index(self, frame_or_series):
# GH 53644
pytest.importorskip("pyarrow")
obj = frame_or_series(
range(5),
index=date_range("2020", freq="D", periods=5).astype(
"timestamp[us][pyarrow]"
),
)
result = obj.loc[obj.index[:-3]]
expected = frame_or_series(
range(2),
index=date_range("2020", freq="D", periods=2).astype(
"timestamp[us][pyarrow]"
),
)
tm.assert_equal(result, expected)

0 comments on commit 0e28257

Please sign in to comment.