Skip to content

BUG: corner cases in DTI.get_value, Float64Index.get_value #31163

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 3 commits into from
Jan 23, 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
6 changes: 3 additions & 3 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from pandas.core.dtypes.common import _NS_DTYPE, is_float, is_integer, is_scalar
from pandas.core.dtypes.dtypes import DatetimeTZDtype
from pandas.core.dtypes.missing import isna
from pandas.core.dtypes.missing import is_valid_nat_for_dtype

from pandas.core.accessor import delegate_names
from pandas.core.arrays.datetimes import (
Expand Down Expand Up @@ -677,8 +677,8 @@ def get_loc(self, key, method=None, tolerance=None):
-------
loc : int
"""
if is_scalar(key) and isna(key):
key = NaT # FIXME: do this systematically
if is_valid_nat_for_dtype(key, self.dtype):
key = NaT

if tolerance is not None:
# try converting tolerance now, so errors don't get swallowed by
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,7 @@ def get_value(self, series: "Series", key):
raise InvalidIndexError

loc = self.get_loc(key)
if not is_scalar(loc):
return series.iloc[loc]

new_values = series._values[loc]
return new_values
return self._get_values_for_loc(series, loc)

def equals(self, other) -> bool:
"""
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,3 +773,14 @@ def test_get_loc_nat(self):
# GH#20464
index = DatetimeIndex(["1/3/2000", "NaT"])
assert index.get_loc(pd.NaT) == 1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe better to parmateize over the accepted nulls (do we have a fixture for date time nulls?) and then have an errors version

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ill put that on my list of follow-ups, we're about 2/3 of the way towards having get_value be internally consistent.

Thoughts on the Series.at outlier?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what’s the Series.at outlier?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ill put that on my list of follow-ups, we're about 2/3 of the way towards having get_value be internally consistent.

sure

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what’s the Series.at outlier?

in the test here ser.loc[4] behaves like ser.loc[4.0], while ser.at[4] raises

assert index.get_loc(None) == 1

assert index.get_loc(np.nan) == 1

assert index.get_loc(pd.NA) == 1

assert index.get_loc(np.datetime64("NaT")) == 1

with pytest.raises(KeyError, match="NaT"):
index.get_loc(np.timedelta64("NaT"))
40 changes: 40 additions & 0 deletions pandas/tests/indexes/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,46 @@ def test_get_loc_missing_nan(self):
# listlike/non-hashable raises TypeError
idx.get_loc([np.nan])

@pytest.mark.parametrize(
"vals",
[
pd.date_range("2016-01-01", periods=3),
pd.timedelta_range("1 Day", periods=3),
],
)
def test_lookups_datetimelike_values(self, vals):
# If we have datetime64 or timedelta64 values, make sure they are
# wrappped correctly
ser = pd.Series(vals, index=range(3, 6))
ser.index = ser.index.astype("float64")

expected = vals[1]

result = ser.index.get_value(ser, 4.0)
assert isinstance(result, type(expected)) and result == expected
result = ser.index.get_value(ser, 4)
assert isinstance(result, type(expected)) and result == expected

result = ser[4.0]
assert isinstance(result, type(expected)) and result == expected
result = ser[4]
assert isinstance(result, type(expected)) and result == expected

result = ser.loc[4.0]
assert isinstance(result, type(expected)) and result == expected
result = ser.loc[4]
assert isinstance(result, type(expected)) and result == expected

result = ser.at[4.0]
assert isinstance(result, type(expected)) and result == expected
# Note: ser.at[4] raises ValueError; TODO: should we make this match loc?

result = ser.iloc[1]
assert isinstance(result, type(expected)) and result == expected

result = ser.iat[1]
assert isinstance(result, type(expected)) and result == expected

def test_contains_nans(self):
i = Float64Index([1.0, 2.0, np.nan])
assert np.nan in i
Expand Down