Skip to content

[backport 2.3.x] BUG (string dtype): let fillna with invalid value upcast to object dtype (#60296) #60316

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 1 commit into from
Nov 14, 2024
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
6 changes: 3 additions & 3 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,7 @@ def fillna(
return nbs

if limit is not None:
mask[mask.cumsum(self.ndim - 1) > limit] = False
mask[mask.cumsum(self.values.ndim - 1) > limit] = False

if inplace:
nbs = self.putmask(
Expand Down Expand Up @@ -2136,7 +2136,7 @@ def where(
res_values = arr._where(cond, other).T
except (ValueError, TypeError):
if self.ndim == 1 or self.shape[0] == 1:
if isinstance(self.dtype, IntervalDtype):
if isinstance(self.dtype, (IntervalDtype, StringDtype)):
# TestSetitemFloatIntervalWithIntIntervalValues
blk = self.coerce_to_target_dtype(orig_other)
nbs = blk.where(orig_other, orig_cond, using_cow=using_cow)
Expand Down Expand Up @@ -2338,7 +2338,7 @@ def fillna(
using_cow: bool = False,
already_warned=None,
) -> list[Block]:
if isinstance(self.dtype, IntervalDtype):
if isinstance(self.dtype, (IntervalDtype, StringDtype)):
# Block.fillna handles coercion (test_fillna_interval)
return super().fillna(
value=value,
Expand Down
8 changes: 1 addition & 7 deletions pandas/tests/frame/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,15 +1086,9 @@ def test_where_producing_ea_cond_for_np_dtype():
@pytest.mark.parametrize(
"replacement", [0.001, True, "snake", None, datetime(2022, 5, 4)]
)
def test_where_int_overflow(replacement, using_infer_string):
def test_where_int_overflow(replacement):
# GH 31687
df = DataFrame([[1.0, 2e25, "nine"], [np.nan, 0.1, None]])
if using_infer_string and replacement not in (None, "snake"):
with pytest.raises(
TypeError, match=f"Invalid value '{replacement}' for dtype 'str'"
):
df.where(pd.notnull(df), replacement)
return
result = df.where(pd.notnull(df), replacement)
expected = DataFrame([[1.0, 2e25, "nine"], [replacement, 0.1, replacement]])

Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
concat,
date_range,
interval_range,
isna,
period_range,
timedelta_range,
)
Expand Down Expand Up @@ -865,11 +864,6 @@ def test_series_where(self, obj, key, expected, warn, val, is_inplace):
obj = obj.copy()
arr = obj._values

if obj.dtype == "string" and not (isinstance(val, str) or isna(val)):
with pytest.raises(TypeError, match="Invalid value"):
obj.where(~mask, val)
return

res = obj.where(~mask, val)

if val is NA and res.dtype == object:
Expand Down
Loading