Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
PaulVoigt opened this issue Apr 16, 2020 · 0 comments · Fixed by #39378
Closed

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

PaulVoigt opened this issue Apr 16, 2020 · 0 comments · Fixed by #39378
Labels
Bug Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@PaulVoigt
Copy link

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

@PaulVoigt PaulVoigt added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 16, 2020
@jbrockmendel jbrockmendel added Indexing Related to indexing on series/frames, not to indexes themselves and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2020
@jreback jreback added this to the 1.3 milestone Jan 25, 2021
@jreback jreback added the Datetime Datetime data dtype label Jan 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants