Skip to content

REF: share ExtensionIndex.insert-> Index.insert #44170

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 1 commit into from
Oct 24, 2021
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: 4 additions & 0 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
needs_i8_conversion,
)
from pandas.core.dtypes.dtypes import (
CategoricalDtype,
ExtensionDtype,
IntervalDtype,
PeriodDtype,
Expand Down Expand Up @@ -641,5 +642,8 @@ def is_valid_na_for_dtype(obj, dtype: DtypeObj) -> bool:
elif isinstance(dtype, IntervalDtype):
return lib.is_float(obj) or obj is None or obj is libmissing.NA

elif isinstance(dtype, CategoricalDtype):
return is_valid_na_for_dtype(obj, dtype.categories.dtype)

# fallback, default to allowing NaN, None, NA, NaT
return not isinstance(obj, (np.datetime64, np.timedelta64, Decimal))
15 changes: 11 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6432,14 +6432,21 @@ def insert(self, loc: int, item) -> Index:
if is_valid_na_for_dtype(item, self.dtype) and self.dtype != object:
item = self._na_value

arr = self._values

try:
item = self._validate_fill_value(item)
except TypeError:
if isinstance(arr, ExtensionArray):
res_values = arr.insert(loc, item)
return type(self)._simple_new(res_values, name=self.name)
else:
item = self._validate_fill_value(item)
except (TypeError, ValueError):
# e.g. trying to insert an integer into a DatetimeIndex
# We cannot keep the same dtype, so cast to the (often object)
# minimal shared dtype before doing the insert.
dtype = self._find_common_type_compat(item)
return self.astype(dtype).insert(loc, item)

arr = self._values

if arr.dtype != object or not isinstance(
item, (tuple, np.datetime64, np.timedelta64)
):
Expand Down
25 changes: 0 additions & 25 deletions pandas/core/indexes/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,31 +134,6 @@ class ExtensionIndex(Index):

# ---------------------------------------------------------------------

def insert(self, loc: int, item) -> Index:
"""
Make new Index inserting new item at location. Follows
Python list.append semantics for negative values.

Parameters
----------
loc : int
item : object

Returns
-------
new_index : Index
"""
try:
result = self._data.insert(loc, item)
except (ValueError, TypeError):
# e.g. trying to insert an integer into a DatetimeIndex
# We cannot keep the same dtype, so cast to the (often object)
# minimal shared dtype before doing the insert.
dtype = self._find_common_type_compat(item)
return self.astype(dtype).insert(loc, item)
else:
return type(self)._simple_new(result, name=self.name)

def _validate_fill_value(self, value):
"""
Convert value to be insertable to underlying array.
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/dtypes/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
is_scalar,
)
from pandas.core.dtypes.dtypes import (
CategoricalDtype,
DatetimeTZDtype,
IntervalDtype,
PeriodDtype,
Expand Down Expand Up @@ -739,3 +740,11 @@ def test_is_valid_na_for_dtype_interval(self):

dtype = IntervalDtype("datetime64[ns]", "both")
assert not is_valid_na_for_dtype(NaT, dtype)

def test_is_valid_na_for_dtype_categorical(self):
dtype = CategoricalDtype(categories=[0, 1, 2])
assert is_valid_na_for_dtype(np.nan, dtype)

assert not is_valid_na_for_dtype(NaT, dtype)
assert not is_valid_na_for_dtype(np.datetime64("NaT", "ns"), dtype)
assert not is_valid_na_for_dtype(np.timedelta64("NaT", "ns"), dtype)