diff --git a/doc/source/whatsnew/v0.16.1.txt b/doc/source/whatsnew/v0.16.1.txt index 4ccaf7a4719c9..2efa19e79fff0 100755 --- a/doc/source/whatsnew/v0.16.1.txt +++ b/doc/source/whatsnew/v0.16.1.txt @@ -372,3 +372,4 @@ Bug Fixes - Updated BigQuery connector to no longer use deprecated ``oauth2client.tools.run()`` (:issue:`8327`) - Bug in subclassed ``DataFrame``. It may not return the correct class, when slicing or subsetting it. (:issue:`9632`) - Bug in ``.median()`` where non-float null values are not handled correctly (:issue:`10040`) +- Bug in Series.fillna() where it raises if a numerically convertible string is given (:issue:`10092`) diff --git a/pandas/core/internals.py b/pandas/core/internals.py index 564484a19e873..3395ea360165e 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -4017,7 +4017,8 @@ def _putmask_smart(v, m, n): try: nn = n[m] nn_at = nn.astype(v.dtype) - if (nn == nn_at).all(): + comp = (nn == nn_at) + if is_list_like(comp) and comp.all(): nv = v.copy() nv[m] = nn_at return nv diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index f4880fdbb5de4..22f8aee1e0a4e 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -3654,6 +3654,16 @@ def test_fillna(self): expected = Series([999,999,np.nan],index=[0,1,2]) assert_series_equal(result,expected) + # GH 9043 + # make sure a string representation of int/float values can be filled + # correctly without raising errors or being converted + vals = ['0', '1.5', '-0.3'] + for val in vals: + s = Series([0, 1, np.nan, np.nan, 4], dtype='float64') + result = s.fillna(val) + expected = Series([0, 1, val, val, 4], dtype='object') + assert_series_equal(result, expected) + def test_fillna_bug(self): x = Series([nan, 1., nan, 3., nan], ['z', 'a', 'b', 'c', 'd']) filled = x.fillna(method='ffill')