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
BUG: non-monotonic DatetimeIndex does not raise KeyError for missing labels #7827
Comments
this is correct. It cannot tell its out of range because is not monotonic, so its effectively just a mask on the original. I suppose this could raise, what is the usecase? Scalars raise KeyErrors when not found
|
Honestly in this case it was mostly a mistake -- non-monotonic time indexes are not very useful. I think the right behavior is probably to raise a |
Please, don't, it is a legitimate use case to work with empty datasets, e.g. that happens to me when I fill a frame with incoming data over time. |
@immerrr I should clarify: by "indexing method" I guess I just really just meant Yes, it's absolutely a legitimate case to work with empty datasets -- but do you expect to be able to index them with scalar labels? |
Ah, ok, sure. I would indeed expect |
see here: https://github.com/pydata/pandas/blob/master/pandas/tseries/index.py#L1200 I remember putting this in for a monotonic index a while back, where you can easily figure out if its out of range. but didn't test (obviously) for non-monotonic. its easy to figure out if its not their, and I am leaning that it should raise (as all that said, I don't know if anything will (or should break). |
@shoyer PR for this? |
Nowadays the error is correctly reported index = pd.to_datetime(['2000-01-02', '2000-01-01'])
index.get_loc('1900-01-01')
File ~/git/mypandas/pandas/core/indexes/datetimes.py:681, in DatetimeIndex.get_loc(self, key, method, tolerance)
679 return Index.get_loc(self, key, method, tolerance)
680 except KeyError as err:
--> 681 raise KeyError(orig_key) from err
KeyError: '1900-01-01' So this issue is solved and should be closed |
see if we have a direct test for this (probably) if not would take a PR with one |
Indeed, it was highly probable pandas/pandas/tests/indexes/datetimes/test_indexing.py Lines 449 to 453 in aa720d4
|
The text was updated successfully, but these errors were encountered: