Skip to content

Commit 56476d1

Browse files
authored
BUG: Slicing DatetimeIndex with strings containing microseconds raising KeyError (#39378)
1 parent 7d9ad04 commit 56476d1

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ Indexing
290290
- Bug in :meth:`DataFrame.iloc.__setitem__` and :meth:`DataFrame.loc.__setitem__` with mixed dtypes when setting with a dictionary value (:issue:`38335`)
291291
- Bug in :meth:`DataFrame.__setitem__` not raising ``ValueError`` when right hand side is a :class:`DataFrame` with wrong number of columns (:issue:`38604`)
292292
- Bug in :meth:`DataFrame.loc` dropping levels of :class:`MultiIndex` when :class:`DataFrame` used as input has only one row (:issue:`10521`)
293+
- Bug in :meth:`DataFrame.__getitem__` and :meth:`Series.__getitem__` always raising ``KeyError`` when slicing with existing strings an :class:`Index` with milliseconds (:issue:`33589`)
293294
- Bug in setting ``timedelta64`` values into numeric :class:`Series` failing to cast to object dtype (:issue:`39086`)
294295
- Bug in setting :class:`Interval` values into a :class:`Series` or :class:`DataFrame` with mismatched :class:`IntervalDtype` incorrectly casting the new values to the existing dtype (:issue:`39120`)
295296
- Bug in incorrectly raising in :meth:`Index.insert`, when setting a new column that cannot be held in the existing ``frame.columns``, or in :meth:`Series.reset_index` or :meth:`DataFrame.reset_index` instead of casting to a compatible dtype (:issue:`39068`)

pandas/core/indexes/datetimes.py

+1
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ def _parsed_string_to_bounds(self, reso: Resolution, parsed: datetime):
565565
"second",
566566
"minute",
567567
"second",
568+
"millisecond",
568569
"microsecond",
569570
}
570571
if reso.attrname not in valid_resos:

pandas/tests/indexing/test_datetime.py

+21
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,24 @@ def test_loc_setitem_with_existing_dst(self):
231231
dtype=object,
232232
)
233233
tm.assert_frame_equal(result, expected)
234+
235+
def test_getitem_millisecond_resolution(self, frame_or_series):
236+
# GH#33589
237+
obj = frame_or_series(
238+
[1, 2, 3, 4],
239+
index=[
240+
Timestamp("2017-10-25T16:25:04.151"),
241+
Timestamp("2017-10-25T16:25:04.252"),
242+
Timestamp("2017-10-25T16:50:05.237"),
243+
Timestamp("2017-10-25T16:50:05.238"),
244+
],
245+
)
246+
result = obj["2017-10-25T16:25:04.252":"2017-10-25T16:50:05.237"]
247+
expected = frame_or_series(
248+
[2, 3],
249+
index=[
250+
Timestamp("2017-10-25T16:25:04.252"),
251+
Timestamp("2017-10-25T16:50:05.237"),
252+
],
253+
)
254+
tm.assert_equal(result, expected)

0 commit comments

Comments
 (0)