Skip to content

BUG: get_loc with partial string index does not return error #46978

@MortGron

Description

@MortGron

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

INSTALLED VERSIONS

commit : 4bfe3d0
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.102.1-microsoft-standard-WSL2
Version : #1 SMP Wed Mar 2 00:30:59 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexingRelated to indexing on series/frames, not to indexes themselvesUsage Question

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions