Skip to content

BUG: KeyError on slicing with datetime, when datetime contains microseconds  #33589

Closed
@PaulVoigt

Description

@PaulVoigt

Problem description

Slicing a DataFrame with a datetime index by datetime results in a KeyError when the string contains microseconds.

df['2017-10-25T16:25:04.252':'2017-10-25T16:50:05.237']

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-20-482a9c5e8c58> in <module>
----> 1 df['2017-10-25T16:25:04.252':'2017-10-25T16:50:05.237']

/opt/conda/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2777 
   2778         # Do we have a slicer (on rows)?
-> 2779         indexer = convert_to_index_sliceable(self, key)
   2780         if indexer is not None:
   2781             # either we have a slice or we have a string that can be converted

/opt/conda/lib/python3.7/site-packages/pandas/core/indexing.py in convert_to_index_sliceable(obj, key)
   2265     idx = obj.index
   2266     if isinstance(key, slice):
-> 2267         return idx._convert_slice_indexer(key, kind="getitem")
   2268 
   2269     elif isinstance(key, str):

/opt/conda/lib/python3.7/site-packages/pandas/core/indexes/base.py in _convert_slice_indexer(self, key, kind)
   2960             indexer = key
   2961         else:
-> 2962             indexer = self.slice_indexer(start, stop, step, kind=kind)
   2963 
   2964         return indexer

/opt/conda/lib/python3.7/site-packages/pandas/core/indexes/datetimes.py in slice_indexer(self, start, end, step, kind)
    823                 mask = True
    824                 if start is not None:
--> 825                     start_casted = self._maybe_cast_slice_bound(start, "left", kind)
    826                     mask = start_casted <= self
    827 

/opt/conda/lib/python3.7/site-packages/pandas/core/indexes/datetimes.py in _maybe_cast_slice_bound(self, label, side, kind)
    761             freq = getattr(self, "freqstr", getattr(self, "inferred_freq", None))
    762             _, parsed, reso = parsing.parse_time_string(label, freq)
--> 763             lower, upper = self._parsed_string_to_bounds(reso, parsed)
    764             # lower, upper form the half-open interval:
    765             #   [parsed, parsed + 1 freq)

/opt/conda/lib/python3.7/site-packages/pandas/core/indexes/datetimes.py in _parsed_string_to_bounds(self, reso, parsed)
    517         }
    518         if reso not in valid_resos:
--> 519             raise KeyError
    520         if reso == "year":
    521             start = Timestamp(parsed.year, 1, 1)

KeyError: 

I think that the function .parse_time_string in pandas._libs.tslibs.parsing.pyx is not supporting microseconds and returns None

Output of pd.show_versions()

pandas : 1.0.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions