Skip to content

BUG: FooIndex.insert casting datetimelike NAs incorrectly #36374

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
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
4 changes: 2 additions & 2 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
ABCPeriodIndex,
ABCSeries,
)
from pandas.core.dtypes.missing import isna, notna
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna, notna

from pandas.core.algorithms import take, value_counts
from pandas.core.arrays.base import ExtensionArray, _extension_array_shared_docs
Expand Down Expand Up @@ -883,7 +883,7 @@ def _validate_insert_value(self, value):
)
left_insert = value.left
right_insert = value.right
elif is_scalar(value) and isna(value):
elif is_valid_nat_for_dtype(value, self.left.dtype):
# GH#18295
left_insert = right_insert = value
else:
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,9 @@ def is_valid_nat_for_dtype(obj, dtype: DtypeObj) -> bool:
return not isinstance(obj, np.timedelta64)
if dtype.kind == "m":
return not isinstance(obj, np.datetime64)
if dtype.kind in ["i", "u", "f", "c"]:
# Numeric
return obj is not NaT and not isinstance(obj, (np.datetime64, np.timedelta64))

# must be PeriodDType
return not isinstance(obj, (np.datetime64, np.timedelta64))
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
ABCSeries,
ABCUInt64Index,
)
from pandas.core.dtypes.missing import isna
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna

from pandas.core import algorithms
import pandas.core.common as com
Expand Down Expand Up @@ -164,7 +164,12 @@ def is_all_dates(self) -> bool:
def insert(self, loc: int, item):
# treat NA values as nans:
if is_scalar(item) and isna(item):
item = self._na_value
if is_valid_nat_for_dtype(item, self.dtype):
item = self._na_value
else:
# NaT, np.datetime64("NaT"), np.timedelta64("NaT")
return self.astype(object).insert(loc, item)

return super().insert(loc, item)

def _union(self, other, sort):
Expand Down
11 changes: 10 additions & 1 deletion pandas/tests/indexes/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,20 @@ def test_insert(self, data):

# GH 18295 (test missing)
na_idx = IntervalIndex([np.nan], closed=data.closed)
for na in (np.nan, pd.NaT, None):
for na in [np.nan, None, pd.NA]:
expected = data[:1].append(na_idx).append(data[1:])
result = data.insert(1, na)
tm.assert_index_equal(result, expected)

if data.left.dtype.kind not in ["m", "M"]:
# trying to insert pd.NaT into a numeric-dtyped Index should cast/raise
msg = "can only insert Interval objects and NA into an IntervalIndex"
with pytest.raises(ValueError, match=msg):
result = data.insert(1, pd.NaT)
else:
result = data.insert(1, pd.NaT)
tm.assert_index_equal(result, expected)

def test_is_unique_interval(self, closed):
"""
Interval specific tests for is_unique in addition to base class tests
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,14 @@ def test_insert(self):

# GH 18295 (test missing)
expected = Float64Index([0, np.nan, 1, 2, 3, 4])
for na in (np.nan, pd.NaT, None):
for na in [np.nan, None, pd.NA]:
Copy link
Contributor

Choose a reason for hiding this comment

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

could eventually parameterize on the null_fixture

result = RangeIndex(5).insert(1, na)
tm.assert_index_equal(result, expected)

result = RangeIndex(5).insert(1, pd.NaT)
expected = pd.Index([0, pd.NaT, 1, 2, 3, 4], dtype=object)
tm.assert_index_equal(result, expected)

def test_delete(self):

idx = RangeIndex(5, name="Foo")
Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/indexes/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@ def test_index_groupby(self):
expected = {ex_keys[0]: idx[[0, 5]], ex_keys[1]: idx[[1, 4]]}
tm.assert_dict_equal(idx.groupby(to_groupby), expected)

def test_insert(self, nulls_fixture):
def test_insert_na(self, nulls_fixture):
# GH 18295 (test missing)
index = self.create_index()
expected = Float64Index([index[0], np.nan] + list(index[1:]))

if nulls_fixture is pd.NaT:
expected = Index([index[0], pd.NaT] + list(index[1:]), dtype=object)
else:
expected = Float64Index([index[0], np.nan] + list(index[1:]))
result = index.insert(1, nulls_fixture)
tm.assert_index_equal(result, expected)

Expand Down