Skip to content

BUG: KeyError in Series.__getitem__ with datetime index and mixed unit slice in pandas 3.0.0rc0 #63262

@TomAugspurger

Description

@TomAugspurger

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

This script succeeded in pandas 2.x, but raises a KeyError in pandas 3.0.0rc0:

import pandas as pd

df = pd.Series(1, index=pd.date_range(start="2000-01-01", freq="h", periods=8))
start = pd.Timestamp("2000-01-01 01:00:00")  # unit=us
stop = start + pd.Timedelta(1)               # unit=ns
df.loc[start:stop]

The mix of us-resolution datetime index with a ns-resolution slice seems relevant. Casting stop to us resolution with stop = (start + pd.Timedelta(1)).as_unit("us") avoids the error.

Issue Description

uv run --python=3.13 --with pandas==3.0.0rc0 python bug.py

fails with

Traceback (most recent call last):
  File "/Users/toaugspurger/gh/bug.py", line 6, in <module>
    df.loc[start:stop]
    ~~~~~~^^^^^^^^^^^^
  File "/Users/toaugspurger/.cache/uv/archive-v0/GlcV9lNBXMPGHt8xIqa_B/lib/python3.13/site-packages/pandas/core/indexing.py", line 1207, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/toaugspurger/.cache/uv/archive-v0/GlcV9lNBXMPGHt8xIqa_B/lib/python3.13/site-packages/pandas/core/indexing.py", line 1429, in _getitem_axis
    return self._get_slice_axis(key, axis=axis)
           ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/Users/toaugspurger/.cache/uv/archive-v0/GlcV9lNBXMPGHt8xIqa_B/lib/python3.13/site-packages/pandas/core/indexing.py", line 1461, in _get_slice_axis
    indexer = labels.slice_indexer(slice_obj.start, slice_obj.stop, slice_obj.step)
  File "/Users/toaugspurger/.cache/uv/archive-v0/GlcV9lNBXMPGHt8xIqa_B/lib/python3.13/site-packages/pandas/core/indexes/datetimes.py", line 1061, in slice_indexer
    return Index.slice_indexer(self, start, end, step)
           ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/toaugspurger/.cache/uv/archive-v0/GlcV9lNBXMPGHt8xIqa_B/lib/python3.13/site-packages/pandas/core/indexes/base.py", line 6690, in slice_indexer
    start_slice, end_slice = self.slice_locs(start, end, step=step)
                             ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/toaugspurger/.cache/uv/archive-v0/GlcV9lNBXMPGHt8xIqa_B/lib/python3.13/site-packages/pandas/core/indexes/base.py", line 6952, in slice_locs
    end_slice = self.get_slice_bound(end, "right")
  File "/Users/toaugspurger/.cache/uv/archive-v0/GlcV9lNBXMPGHt8xIqa_B/lib/python3.13/site-packages/pandas/core/indexes/base.py", line 6860, in get_slice_bound
    raise err from None
  File "/Users/toaugspurger/.cache/uv/archive-v0/GlcV9lNBXMPGHt8xIqa_B/lib/python3.13/site-packages/pandas/core/indexes/base.py", line 6854, in get_slice_bound
    slc = self.get_loc(label)
  File "/Users/toaugspurger/.cache/uv/archive-v0/GlcV9lNBXMPGHt8xIqa_B/lib/python3.13/site-packages/pandas/core/indexes/datetimes.py", line 987, in get_loc
    raise KeyError(orig_key) from err
KeyError: Timestamp('2000-01-01 01:00:00.000000001')

Expected Behavior

Return the matching rows:

2000-01-01 01:00:00    1
Freq: h, dtype: int64

Installed Versions

Replace this line with the output of pd.show_versions()

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNon-Nanodatetime64/timedelta64 with non-nanosecond resolution

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions