Skip to content

Commit

Permalink
[backport 2.3.x] BUG (string dtype): let fillna with invalid value up…
Browse files Browse the repository at this point in the history
…cast to object dtype (#60296) (#60316)

BUG (string dtype): let fillna with invalid value upcast to object dtype (#60296)

* BUG (string dtype): let fillna with invalid value upcast to object dtype

* fix fillna limit case + update tests for no longer raising

(cherry picked from commit 34c39e9)
  • Loading branch information
jorisvandenbossche authored Nov 14, 2024
1 parent 54b47df commit c875a53
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 16 deletions.
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

0 comments on commit c875a53

Please sign in to comment.