|
41 | 41 |
|
42 | 42 | from pandas.core.dtypes.cast import ( |
43 | 43 | find_common_type, |
| 44 | + infer_dtype_from, |
44 | 45 | maybe_cast_to_integer_array, |
45 | | - maybe_promote, |
46 | 46 | validate_numeric_casting, |
47 | 47 | ) |
48 | 48 | from pandas.core.dtypes.common import ( |
|
87 | 87 | ABCTimedeltaIndex, |
88 | 88 | ) |
89 | 89 | from pandas.core.dtypes.inference import is_dict_like |
90 | | -from pandas.core.dtypes.missing import array_equivalent, isna |
| 90 | +from pandas.core.dtypes.missing import array_equivalent, is_valid_nat_for_dtype, isna |
91 | 91 |
|
92 | 92 | from pandas.core import missing, ops |
93 | 93 | from pandas.core.accessor import CachedAccessor |
@@ -5735,16 +5735,14 @@ def insert(self, loc: int, item): |
5735 | 5735 | # Note: this method is overridden by all ExtensionIndex subclasses, |
5736 | 5736 | # so self is never backed by an EA. |
5737 | 5737 | item = lib.item_from_zerodim(item) |
| 5738 | + if is_valid_nat_for_dtype(item, self.dtype) and self.dtype != object: |
| 5739 | + item = self._na_value |
5738 | 5740 |
|
5739 | 5741 | try: |
5740 | 5742 | item = self._validate_fill_value(item) |
5741 | 5743 | except TypeError: |
5742 | | - if is_scalar(item): |
5743 | | - dtype, item = maybe_promote(self.dtype, item) |
5744 | | - else: |
5745 | | - # maybe_promote would raise ValueError |
5746 | | - dtype = np.dtype(object) |
5747 | | - |
| 5744 | + inferred, _ = infer_dtype_from(item) |
| 5745 | + dtype = find_common_type([self.dtype, inferred]) |
5748 | 5746 | return self.astype(dtype).insert(loc, item) |
5749 | 5747 |
|
5750 | 5748 | arr = np.asarray(self) |
|
0 commit comments