Skip to content

Commit 9abb4f9

Browse files
committed
update to use can_hold_element instead of naive exact dtype matching
1 parent 76d0305 commit 9abb4f9

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

pandas/core/generic.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
)
110110

111111
from pandas.core.dtypes.astype import astype_is_view
112+
from pandas.core.dtypes.cast import can_hold_element
112113
from pandas.core.dtypes.common import (
113114
ensure_object,
114115
ensure_platform_int,
@@ -7120,17 +7121,19 @@ def fillna(
71207121
result = self if inplace else self.copy(deep=False)
71217122
if axis == 1:
71227123
# Check that all columns in result have the same dtype
7123-
# otherwise don't bother with ffill and losing accurate dtypes
7124-
dtypes = [result[col].dtype for col in result.columns]
7125-
if len(set(dtypes)) > 1:
7124+
# otherwise don't bother with fillna and losing accurate dtypes
7125+
unique_dtypes = algos.unique(self._mgr.get_dtypes())
7126+
if len(unique_dtypes) > 1:
71267127
raise ValueError(
71277128
"All columns must have the same dtype, but got dtypes: "
7128-
f"{dict(zip(result.columns, dtypes))}"
7129+
f"{list(unique_dtypes)}"
71297130
)
7130-
if (value_dtype := np.asarray(value).dtype) != dtypes[0]:
7131+
# Use the first column, which we have already validated has the
7132+
# same dtypes as the other columns.
7133+
if not can_hold_element(result.iloc[:, 0], value):
7134+
frame_dtype = unique_dtypes.item()
71317135
raise ValueError(
7132-
"Dtype mismatch for value "
7133-
f"(value.dtype={value_dtype} vs {dtypes[0]})"
7136+
f"{value} not a suitable type to fill into {frame_dtype}"
71347137
)
71357138
result = result.T.fillna(value=value).T
71367139
else:

pandas/tests/frame/methods/test_fillna.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def test_fillna_dict_series_axis_1_value_mismatch_with_cols(self):
500500
"c": [np.nan, 1, 2, 3, 4],
501501
}
502502
)
503-
with pytest.raises(ValueError, match="Dtype mismatch for value"):
503+
with pytest.raises(ValueError, match=".* not a suitable type to fill into .*"):
504504
df.fillna(Series({"a": "abc", "b": "def", "c": "hij"}), axis=1)
505505

506506
def test_fillna_dataframe(self):

0 commit comments

Comments
 (0)