You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I guess this is similar to indexing with an index with duplicate values (which is probably a separate issue). It's nice to be able to rety on invariants, like the size of the result matching the number of matching values in the index.
KeyError makes sense for indexes without duplicates, because the alternative is returning a scalar, which isn't possible if there isn't a match.
IMO it's unexpected for line 4 to return a 0 sized array
The explanation here is that the string '2018-01-02' is to be considered as a slice, because the resolution of the string is higher than the resolution of the index:
In [31]: s.index.resolution
Out[31]: 'minute'
In [32]: _, _, resolution = parsing.parse_time_string('2018-01-02', freq=None)
In [33]: resolution
Out[33]: 'day'
So if the strings '2018-01-01', '2018-02-02' etc are considered as slices, why not '2018-03-03' ?
The only difference is that it is "out of range" for the index. But with normal slicing, out of range slice bounds return an empty object, and don't raise an error:
In [34]: s.iloc[0:2]
Out[34]:
2018-01-01 00:00:00 1
2018-02-02 01:01:00 2
dtype: int64
In [35]: s.iloc[10:12]
Out[35]: Series([], dtype: int64)
So given that, I agree with @shoyer that it would be more consistent (and reliable) to return an empty object here instead of raising an error.
Although, Stephan, note that it would still depend on the resolution of the passed string (so it would still depend to a certain extent on the value of the key, and you can't be sure that whathever string will not raise an error, but at least for datetime strings of the same resolution, it wouldn't depend any more on the exact value).
With pandas 0.24:
(side note: this is quite the traceback!)
Bizarrely, whether indexing with a string raises a KeyError or returns an array of size 0 depends upon the value.
But more generally, does it ever make sense to raise an error? It's arguably more consistent to only return size 0 arrays.
xref #7827 and pydata/xarray#2825
The text was updated successfully, but these errors were encountered: