Skip to content

BUG: Series[Interval].fillna(mismatched) coerce instead of raise #45796

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 2 commits into from
Feb 4, 2022
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ Indexing
Missing
^^^^^^^
- 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`)
- 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:`??`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?? -> 45796 (follow up reminder)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

included in #45940

- Bug in :meth:`DataFrame.interpolate` with object-dtype column not returning a copy with ``inplace=False`` (:issue:`45791`)
-

Expand Down
11 changes: 5 additions & 6 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,11 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> list[Blo
fill_value = self._standardize_fill_value(fill_value)

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

elif isinstance(self, NDArrayBackedExtensionBlock):
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/indexing/test_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,30 @@ def test_fillna_datetime64tz(self, index_or_series, fill_val, fill_dtype):
with tm.assert_produces_warning(warn, match="mismatched timezone"):
self._assert_fillna_conversion(obj, fill_val, exp, fill_dtype)

@pytest.mark.parametrize(
"fill_val",
[
1,
1.1,
1 + 1j,
True,
pd.Interval(1, 2, closed="left"),
pd.Timestamp("2012-01-01", tz="US/Eastern"),
pd.Timestamp("2012-01-01"),
pd.Timedelta(days=1),
pd.Period("2016-01-01", "D"),
],
)
def test_fillna_interval(self, index_or_series, fill_val):
ii = pd.interval_range(1.0, 5.0, closed="right").insert(1, np.nan)
assert isinstance(ii.dtype, pd.IntervalDtype)
obj = index_or_series(ii)

exp = index_or_series([ii[0], fill_val, ii[2], ii[3], ii[4]], dtype=object)

fill_dtype = object
self._assert_fillna_conversion(obj, fill_val, exp, fill_dtype)

@pytest.mark.xfail(reason="Test not implemented")
def test_fillna_series_int64(self):
raise NotImplementedError
Expand Down