Skip to content

Commit 34c39e9

Browse files
BUG (string dtype): let fillna with invalid value upcast to object dtype (pandas-dev#60296)
* BUG (string dtype): let fillna with invalid value upcast to object dtype * fix fillna limit case + update tests for no longer raising
1 parent 9bee2f0 commit 34c39e9

File tree

3 files changed

+6
-16
lines changed

3 files changed

+6
-16
lines changed

pandas/core/internals/blocks.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
PeriodArray,
109109
TimedeltaArray,
110110
)
111+
from pandas.core.arrays.string_ import StringDtype
111112
from pandas.core.base import PandasObject
112113
import pandas.core.common as com
113114
from pandas.core.computation import expressions
@@ -1336,7 +1337,7 @@ def fillna(
13361337
return [self.copy(deep=False)]
13371338

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

13411342
if inplace:
13421343
nbs = self.putmask(mask.T, value)
@@ -1684,7 +1685,7 @@ def where(self, other, cond) -> list[Block]:
16841685
res_values = arr._where(cond, other).T
16851686
except (ValueError, TypeError):
16861687
if self.ndim == 1 or self.shape[0] == 1:
1687-
if isinstance(self.dtype, IntervalDtype):
1688+
if isinstance(self.dtype, (IntervalDtype, StringDtype)):
16881689
# TestSetitemFloatIntervalWithIntIntervalValues
16891690
blk = self.coerce_to_target_dtype(orig_other, raise_on_upcast=False)
16901691
return blk.where(orig_other, orig_cond)
@@ -1854,9 +1855,9 @@ def fillna(
18541855
limit: int | None = None,
18551856
inplace: bool = False,
18561857
) -> list[Block]:
1857-
if isinstance(self.dtype, IntervalDtype):
1858+
if isinstance(self.dtype, (IntervalDtype, StringDtype)):
18581859
# Block.fillna handles coercion (test_fillna_interval)
1859-
if limit is not None:
1860+
if isinstance(self.dtype, IntervalDtype) and limit is not None:
18601861
raise ValueError("limit must be None")
18611862
return super().fillna(
18621863
value=value,

pandas/tests/frame/indexing/test_where.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -1025,15 +1025,9 @@ def test_where_producing_ea_cond_for_np_dtype():
10251025
@pytest.mark.parametrize(
10261026
"replacement", [0.001, True, "snake", None, datetime(2022, 5, 4)]
10271027
)
1028-
def test_where_int_overflow(replacement, using_infer_string):
1028+
def test_where_int_overflow(replacement):
10291029
# GH 31687
10301030
df = DataFrame([[1.0, 2e25, "nine"], [np.nan, 0.1, None]])
1031-
if using_infer_string and replacement not in (None, "snake"):
1032-
with pytest.raises(
1033-
TypeError, match=f"Invalid value '{replacement}' for dtype 'str'"
1034-
):
1035-
df.where(pd.notnull(df), replacement)
1036-
return
10371031
result = df.where(pd.notnull(df), replacement)
10381032
expected = DataFrame([[1.0, 2e25, "nine"], [replacement, 0.1, replacement]])
10391033

pandas/tests/series/indexing/test_setitem.py

-5
Original file line numberDiff line numberDiff line change
@@ -839,11 +839,6 @@ def test_series_where(self, obj, key, expected, raises, val, is_inplace):
839839
obj = obj.copy()
840840
arr = obj._values
841841

842-
if raises and obj.dtype == "string":
843-
with pytest.raises(TypeError, match="Invalid value"):
844-
obj.where(~mask, val)
845-
return
846-
847842
res = obj.where(~mask, val)
848843

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

0 commit comments

Comments
 (0)