Skip to content

Commit c875a53

Browse files
[backport 2.3.x] BUG (string dtype): let fillna with invalid value upcast 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)
1 parent 54b47df commit c875a53

File tree

3 files changed

+4
-16
lines changed

3 files changed

+4
-16
lines changed

pandas/core/internals/blocks.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ def fillna(
17101710
return nbs
17111711

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

17151715
if inplace:
17161716
nbs = self.putmask(
@@ -2136,7 +2136,7 @@ def where(
21362136
res_values = arr._where(cond, other).T
21372137
except (ValueError, TypeError):
21382138
if self.ndim == 1 or self.shape[0] == 1:
2139-
if isinstance(self.dtype, IntervalDtype):
2139+
if isinstance(self.dtype, (IntervalDtype, StringDtype)):
21402140
# TestSetitemFloatIntervalWithIntIntervalValues
21412141
blk = self.coerce_to_target_dtype(orig_other)
21422142
nbs = blk.where(orig_other, orig_cond, using_cow=using_cow)
@@ -2338,7 +2338,7 @@ def fillna(
23382338
using_cow: bool = False,
23392339
already_warned=None,
23402340
) -> list[Block]:
2341-
if isinstance(self.dtype, IntervalDtype):
2341+
if isinstance(self.dtype, (IntervalDtype, StringDtype)):
23422342
# Block.fillna handles coercion (test_fillna_interval)
23432343
return super().fillna(
23442344
value=value,

pandas/tests/frame/indexing/test_where.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -1086,15 +1086,9 @@ def test_where_producing_ea_cond_for_np_dtype():
10861086
@pytest.mark.parametrize(
10871087
"replacement", [0.001, True, "snake", None, datetime(2022, 5, 4)]
10881088
)
1089-
def test_where_int_overflow(replacement, using_infer_string):
1089+
def test_where_int_overflow(replacement):
10901090
# GH 31687
10911091
df = DataFrame([[1.0, 2e25, "nine"], [np.nan, 0.1, None]])
1092-
if using_infer_string and replacement not in (None, "snake"):
1093-
with pytest.raises(
1094-
TypeError, match=f"Invalid value '{replacement}' for dtype 'str'"
1095-
):
1096-
df.where(pd.notnull(df), replacement)
1097-
return
10981092
result = df.where(pd.notnull(df), replacement)
10991093
expected = DataFrame([[1.0, 2e25, "nine"], [replacement, 0.1, replacement]])
11001094

pandas/tests/series/indexing/test_setitem.py

-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
concat,
3535
date_range,
3636
interval_range,
37-
isna,
3837
period_range,
3938
timedelta_range,
4039
)
@@ -865,11 +864,6 @@ def test_series_where(self, obj, key, expected, warn, val, is_inplace):
865864
obj = obj.copy()
866865
arr = obj._values
867866

868-
if obj.dtype == "string" and not (isinstance(val, str) or isna(val)):
869-
with pytest.raises(TypeError, match="Invalid value"):
870-
obj.where(~mask, val)
871-
return
872-
873867
res = obj.where(~mask, val)
874868

875869
if val is NA and res.dtype == object:

0 commit comments

Comments
 (0)