diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 0aed08d46657e..b0f9f8ac8b2fd 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4047,9 +4047,10 @@ def _to_safe_for_reshape(self): """ return self - def _convert_for_op(self, value): + def _validate_fill_value(self, value): """ - Convert value to be insertable to ndarray. + Check if the value can be inserted into our array, and convert + it to an appropriate native type if necessary. """ return value @@ -4228,7 +4229,8 @@ def putmask(self, mask, value): """ values = self.values.copy() try: - np.putmask(values, mask, self._convert_for_op(value)) + converted = self._validate_fill_value(value) + np.putmask(values, mask, converted) if is_period_dtype(self.dtype): # .values cast to object, so we need to cast back values = type(self)(values)._data diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index f269495f6011a..2d166773dda2c 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -325,13 +325,11 @@ def __reduce__(self): d.update(self._get_attributes_dict()) return _new_DatetimeIndex, (type(self), d), None - def _convert_for_op(self, value): + def _validate_fill_value(self, value): """ Convert value to be insertable to ndarray. """ - if self._has_same_tz(value): - return Timestamp(value).asm8 - raise ValueError("Passed item and index have different timezone") + return self._data._validate_setitem_value(value) def _is_comparable_dtype(self, dtype: DtypeObj) -> bool: """ diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index e8b7efeee8852..f6859cbc4c0a2 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -116,7 +116,7 @@ def _shallow_copy(self, values=None, name: Label = lib.no_default): return Float64Index._simple_new(values, name=name) return super()._shallow_copy(values=values, name=name) - def _convert_for_op(self, value): + def _validate_fill_value(self, value): """ Convert value to be insertable to ndarray. """