Skip to content

Commit 899a6bf

Browse files
authored
BUG: Series[Interval].fillna(mismatched) coerce instead of raise (#45796)
1 parent c7da9ea commit 899a6bf

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ Indexing
282282
Missing
283283
^^^^^^^
284284
- Bug in :meth:`Series.fillna` and :meth:`DataFrame.fillna` with ``downcast`` keyword not being respected in some cases where there are no NA values present (:issue:`45423`)
285+
- Bug in :meth:`Series.fillna` and :meth:`DataFrame.fillna` with :class:`IntervalDtype` and incompatible value raising instead of casting to a common (usually object) dtype (:issue:`??`)
285286
- Bug in :meth:`DataFrame.interpolate` with object-dtype column not returning a copy with ``inplace=False`` (:issue:`45791`)
286287
-
287288

pandas/core/internals/blocks.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,11 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> list[Blo
11391139
fill_value = self._standardize_fill_value(fill_value)
11401140

11411141
try:
1142-
casted = np_can_hold_element(self.dtype, fill_value)
1142+
# error: Argument 1 to "np_can_hold_element" has incompatible type
1143+
# "Union[dtype[Any], ExtensionDtype]"; expected "dtype[Any]"
1144+
casted = np_can_hold_element(
1145+
self.dtype, fill_value # type: ignore[arg-type]
1146+
)
11431147
except LossySetitemError:
11441148
nb = self.coerce_to_target_dtype(fill_value)
11451149
return nb.shift(periods, axis=axis, fill_value=fill_value)
@@ -1465,11 +1469,6 @@ def fillna(
14651469
# Discussion about what we want to support in the general
14661470
# case GH#39584
14671471
blk = self.coerce_to_target_dtype(value)
1468-
if blk.dtype == _dtype_obj:
1469-
# For now at least, only support casting e.g.
1470-
# Interval[int64]->Interval[float64],
1471-
raise
1472-
# Never actually reached, but *could* be possible pending GH#45412
14731472
return blk.fillna(value, limit, inplace, downcast)
14741473

14751474
elif isinstance(self, NDArrayBackedExtensionBlock):

pandas/tests/indexing/test_coercion.py

+24
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,30 @@ def test_fillna_datetime64tz(self, index_or_series, fill_val, fill_dtype):
709709
with tm.assert_produces_warning(warn, match="mismatched timezone"):
710710
self._assert_fillna_conversion(obj, fill_val, exp, fill_dtype)
711711

712+
@pytest.mark.parametrize(
713+
"fill_val",
714+
[
715+
1,
716+
1.1,
717+
1 + 1j,
718+
True,
719+
pd.Interval(1, 2, closed="left"),
720+
pd.Timestamp("2012-01-01", tz="US/Eastern"),
721+
pd.Timestamp("2012-01-01"),
722+
pd.Timedelta(days=1),
723+
pd.Period("2016-01-01", "D"),
724+
],
725+
)
726+
def test_fillna_interval(self, index_or_series, fill_val):
727+
ii = pd.interval_range(1.0, 5.0, closed="right").insert(1, np.nan)
728+
assert isinstance(ii.dtype, pd.IntervalDtype)
729+
obj = index_or_series(ii)
730+
731+
exp = index_or_series([ii[0], fill_val, ii[2], ii[3], ii[4]], dtype=object)
732+
733+
fill_dtype = object
734+
self._assert_fillna_conversion(obj, fill_val, exp, fill_dtype)
735+
712736
@pytest.mark.xfail(reason="Test not implemented")
713737
def test_fillna_series_int64(self):
714738
raise NotImplementedError

0 commit comments

Comments
 (0)