Skip to content

REF: Index.insert maybe_promote -> find_common_type #39437

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 28, 2021
Merged
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
14 changes: 6 additions & 8 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@

from pandas.core.dtypes.cast import (
find_common_type,
infer_dtype_from,
maybe_cast_to_integer_array,
maybe_promote,
validate_numeric_casting,
)
from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -87,7 +87,7 @@
ABCTimedeltaIndex,
)
from pandas.core.dtypes.inference import is_dict_like
from pandas.core.dtypes.missing import array_equivalent, isna
from pandas.core.dtypes.missing import array_equivalent, is_valid_nat_for_dtype, isna

from pandas.core import missing, ops
from pandas.core.accessor import CachedAccessor
Expand Down Expand Up @@ -5735,16 +5735,14 @@ def insert(self, loc: int, item):
# Note: this method is overridden by all ExtensionIndex subclasses,
# so self is never backed by an EA.
item = lib.item_from_zerodim(item)
if is_valid_nat_for_dtype(item, self.dtype) and self.dtype != object:
item = self._na_value

try:
item = self._validate_fill_value(item)
except TypeError:
if is_scalar(item):
dtype, item = maybe_promote(self.dtype, item)
else:
# maybe_promote would raise ValueError
dtype = np.dtype(object)

inferred, _ = infer_dtype_from(item)
dtype = find_common_type([self.dtype, inferred])
return self.astype(dtype).insert(loc, item)

arr = np.asarray(self)
Expand Down