Skip to content

Commit 0e92697

Browse files
authored
REGR: fillna on f32 column raising for f64 (#43455)
1 parent 59a06fd commit 0e92697

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

doc/source/whatsnew/v1.3.3.rst

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Fixed regressions
2626
- Fixed regression in :func:`is_list_like` where objects with ``__iter__`` set to ``None`` would be identified as iterable (:issue:`43373`)
2727
- Fixed regression in :meth:`.Resampler.aggregate` when used after column selection would raise if ``func`` is a list of aggregation functions (:issue:`42905`)
2828
- Fixed regression in :meth:`DataFrame.corr` where Kendall correlation would produce incorrect results for columns with repeated values (:issue:`43401`)
29+
- Fixed regression in :meth:`Series.fillna` raising ``TypeError`` when filling ``float`` ``Series`` with list-like fill value having a dtype which couldn't cast lostlessly (like ``float32`` filled with ``float64``) (:issue:`43424`)
30+
-
2931

3032
.. ---------------------------------------------------------------------------
3133

pandas/core/array_algos/putmask.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ def putmask_inplace(values: ArrayLike, mask: np.ndarray, value: Any) -> None:
4141
if lib.is_scalar(value) and isinstance(values, np.ndarray):
4242
value = convert_scalar_for_putitemlike(value, values.dtype)
4343

44-
if not isinstance(values, np.ndarray) or (
45-
values.dtype == object and not lib.is_scalar(value)
44+
if (
45+
not isinstance(values, np.ndarray)
46+
or (values.dtype == object and not lib.is_scalar(value))
47+
# GH#43424: np.putmask raises TypeError if we cannot cast between types with
48+
# rule = "safe", a stricter guarantee we may not have here
49+
or (
50+
isinstance(value, np.ndarray) and not np.can_cast(value.dtype, values.dtype)
51+
)
4652
):
4753
# GH#19266 using np.putmask gives unexpected results with listlike value
4854
if is_list_like(value) and len(value) == len(values):

pandas/tests/series/methods/test_fillna.py

+17
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,23 @@ def test_fillna_categorical_raises(self):
702702
with pytest.raises(TypeError, match=msg):
703703
ser.fillna(DataFrame({1: ["a"], 3: ["b"]}))
704704

705+
@pytest.mark.parametrize("dtype", [float, "float32", "float64"])
706+
@pytest.mark.parametrize("fill_type", tm.ALL_REAL_NUMPY_DTYPES)
707+
def test_fillna_float_casting(self, dtype, fill_type):
708+
# GH-43424
709+
ser = Series([np.nan, 1.2], dtype=dtype)
710+
fill_values = Series([2, 2], dtype=fill_type)
711+
result = ser.fillna(fill_values)
712+
expected = Series([2.0, 1.2], dtype=dtype)
713+
tm.assert_series_equal(result, expected)
714+
715+
def test_fillna_f32_upcast_with_dict(self):
716+
# GH-43424
717+
ser = Series([np.nan, 1.2], dtype=np.float32)
718+
result = ser.fillna({0: 1})
719+
expected = Series([1.0, 1.2], dtype=np.float32)
720+
tm.assert_series_equal(result, expected)
721+
705722
# ---------------------------------------------------------------
706723
# Invalid Usages
707724

0 commit comments

Comments
 (0)