-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
Closed
Labels
IndexingRelated to indexing on series/frames, not to indexes themselvesRelated to indexing on series/frames, not to indexes themselvesUsage Question
Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
>>> import pandas as pd
>>> pd.date_range(start="2022-2-1", end="2022-3-1", periods=4).get_loc("2022-2-4")
slice(1, 1, None)Issue Description
get_loc should give an error for non-exact matches according to the documentation when method=None. Even interpreting "2022-2-4" as a partial string index slice, there is no data that date.
Expected Behavior
Changing the resolution of the index gives the expected result, so it is probably related to https://pandas.pydata.org/docs/user_guide/timeseries.html#slice-vs-exact-match
>>> pd.date_range(start="2022-2-1", end="2022-3-1", periods=4).get_loc("2022-2-4")
KeyError Traceback (most recent call last)
File ~/.cache/pypoetry/virtualenvs/indsl-aATwQON6-py3.8/lib/python3.8/site-packages/pandas/_libs/index.pyx:545, in pandas._libs.index.DatetimeEngine.get_loc()
File pandas/_libs/hashtable_class_helper.pxi:2131, in pandas._libs.hashtable.Int64HashTable.get_item()
File pandas/_libs/hashtable_class_helper.pxi:2140, in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 1648771200000000000
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
File ~/.cache/pypoetry/virtualenvs/indsl-aATwQON6-py3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py:3621, in Index.get_loc(self, key, method, tolerance)
3620 try:
-> 3621 return self._engine.get_loc(casted_key)
3622 except KeyError as err:
File ~/.cache/pypoetry/virtualenvs/indsl-aATwQON6-py3.8/lib/python3.8/site-packages/pandas/_libs/index.pyx:513, in pandas._libs.index.DatetimeEngine.get_loc()
File ~/.cache/pypoetry/virtualenvs/indsl-aATwQON6-py3.8/lib/python3.8/site-packages/pandas/_libs/index.pyx:547, in pandas._libs.index.DatetimeEngine.get_loc()
KeyError: Timestamp('2022-04-01 00:00:00')
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
File ~/.cache/pypoetry/virtualenvs/indsl-aATwQON6-py3.8/lib/python3.8/site-packages/pandas/core/indexes/datetimes.py:679, in DatetimeIndex.get_loc(self, key, method, tolerance)
678 try:
--> 679 return Index.get_loc(self, key, method, tolerance)
680 except KeyError as err:
File ~/.cache/pypoetry/virtualenvs/indsl-aATwQON6-py3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py:3623, in Index.get_loc(self, key, method, tolerance)
3622 except KeyError as err:
-> 3623 raise KeyError(key) from err
3624 except TypeError:
3625 # If we have a listlike key, _check_indexing_error will raise
3626 # InvalidIndexError. Otherwise we fall through and re-raise
3627 # the TypeError.
KeyError: Timestamp('2022-04-01 00:00:00')
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
Input In [117], in <cell line: 1>()
----> 1 pd.date_range(start="2022-2-1", end="2022-3-1", periods=3).get_loc("2022-4-1")
File ~/.cache/pypoetry/virtualenvs/indsl-aATwQON6-py3.8/lib/python3.8/site-packages/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: '2022-4-1'
3
pd.date_range(start="2022-2-1", end="2022-3-1", periods=3).get_loc("2022-2-4")
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
File ~/.cache/pypoetry/virtualenvs/indsl-aATwQON6-py3.8/lib/python3.8/site-packages/pandas/_libs/index.pyx:545, in pandas._libs.index.DatetimeEngine.get_loc()
File pandas/_libs/hashtable_class_helper.pxi:2131, in pandas._libs.hashtable.Int64HashTable.get_item()
File pandas/_libs/hashtable_class_helper.pxi:2140, in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 1643932800000000000
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
File ~/.cache/pypoetry/virtualenvs/indsl-aATwQON6-py3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py:3621, in Index.get_loc(self, key, method, tolerance)
3620 try:
-> 3621 return self._engine.get_loc(casted_key)
3622 except KeyError as err:
File ~/.cache/pypoetry/virtualenvs/indsl-aATwQON6-py3.8/lib/python3.8/site-packages/pandas/_libs/index.pyx:513, in pandas._libs.index.DatetimeEngine.get_loc()
File ~/.cache/pypoetry/virtualenvs/indsl-aATwQON6-py3.8/lib/python3.8/site-packages/pandas/_libs/index.pyx:547, in pandas._libs.index.DatetimeEngine.get_loc()
KeyError: Timestamp('2022-02-04 00:00:00')
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
File ~/.cache/pypoetry/virtualenvs/indsl-aATwQON6-py3.8/lib/python3.8/site-packages/pandas/core/indexes/datetimes.py:679, in DatetimeIndex.get_loc(self, key, method, tolerance)
678 try:
--> 679 return Index.get_loc(self, key, method, tolerance)
680 except KeyError as err:
File ~/.cache/pypoetry/virtualenvs/indsl-aATwQON6-py3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py:3623, in Index.get_loc(self, key, method, tolerance)
3622 except KeyError as err:
-> 3623 raise KeyError(key) from err
3624 except TypeError:
3625 # If we have a listlike key, _check_indexing_error will raise
3626 # InvalidIndexError. Otherwise we fall through and re-raise
3627 # the TypeError.
KeyError: Timestamp('2022-02-04 00:00:00')
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
Input In [118], in <cell line: 1>()
----> 1 pd.date_range(start="2022-2-1", end="2022-3-1", periods=3).get_loc("2022-2-4")
File ~/.cache/pypoetry/virtualenvs/indsl-aATwQON6-py3.8/lib/python3.8/site-packages/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: '2022-2-4'
Installed Versions
Metadata
Metadata
Assignees
Labels
IndexingRelated to indexing on series/frames, not to indexes themselvesRelated to indexing on series/frames, not to indexes themselvesUsage Question