Skip to content

BUG: DatetimeIndex.get_loc/get_value raise InvalidIndexError #31257

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

Merged
merged 2 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from pandas.core.groupby import ops
from pandas.core.groupby.categorical import recode_for_groupby, recode_from_groupby
from pandas.core.indexes.api import CategoricalIndex, Index, MultiIndex
from pandas.core.indexes.base import InvalidIndexError
from pandas.core.series import Series

from pandas.io.formats.printing import pprint_thing
Expand Down Expand Up @@ -565,7 +566,7 @@ def is_in_axis(key) -> bool:
items = obj._data.items
try:
items.get_loc(key)
except (KeyError, TypeError):
except (KeyError, TypeError, InvalidIndexError):
# TypeError shows up here if we pass e.g. Int64Index
return False

Expand Down
7 changes: 6 additions & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
validate_tz_from_dtype,
)
import pandas.core.common as com
from pandas.core.indexes.base import Index, maybe_extract_name
from pandas.core.indexes.base import Index, InvalidIndexError, maybe_extract_name
from pandas.core.indexes.datetimelike import (
DatetimelikeDelegateMixin,
DatetimeTimedeltaMixin,
Expand Down Expand Up @@ -641,6 +641,8 @@ def get_value(self, series, key):
Fast lookup of value from 1-dimensional ndarray. Only use this if you
know what you're doing
"""
if not is_scalar(key):
raise InvalidIndexError(key)

if isinstance(key, (datetime, np.datetime64)):
return self.get_value_maybe_box(series, key)
Expand Down Expand Up @@ -677,6 +679,9 @@ def get_loc(self, key, method=None, tolerance=None):
-------
loc : int
"""
if not is_scalar(key):
raise InvalidIndexError(key)

if is_valid_nat_for_dtype(key, self.dtype):
key = NaT

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2778,7 +2778,7 @@ def maybe_mi_droplevels(indexer, levels, drop_level: bool):
indexer = self._get_level_indexer(key, level=level)
new_index = maybe_mi_droplevels(indexer, [0], drop_level)
return indexer, new_index
except TypeError:
except (TypeError, InvalidIndexError):
pass

if not any(isinstance(k, slice) for k in key):
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,9 @@ def __setitem__(self, key, value):
self[:] = value
else:
self.loc[key] = value
except InvalidIndexError:
# e.g. slice
self._set_with(key, value)

except TypeError as e:
if isinstance(key, tuple) and not isinstance(self.index, MultiIndex):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pandas as pd
from pandas import DatetimeIndex, Index, Timestamp, date_range, notna
import pandas._testing as tm
from pandas.core.indexes.base import InvalidIndexError

from pandas.tseries.offsets import BDay, CDay

Expand Down Expand Up @@ -697,7 +698,7 @@ def test_get_loc(self):

with pytest.raises(KeyError, match="'foobar'"):
idx.get_loc("foobar")
with pytest.raises(TypeError):
with pytest.raises(InvalidIndexError, match=r"slice\(None, 2, None\)"):
idx.get_loc(slice(2))

idx = pd.to_datetime(["2000-01-01", "2000-01-04"])
Expand Down