diff --git a/doc/source/v0.15.0.txt b/doc/source/v0.15.0.txt index 1e52d7e20046e..92eb1d54ae676 100644 --- a/doc/source/v0.15.0.txt +++ b/doc/source/v0.15.0.txt @@ -278,6 +278,8 @@ API changes - ``DataFrame.info()`` now ends its output with a newline character (:issue:`8114`) - add ``copy=True`` argument to ``pd.concat`` to enable pass thrue of complete blocks (:issue:`8252`) +- ``.fillna`` will now raise a ``NotImplementedError`` when passed a ``DataFrame`` (:issue:`8377`) + .. _whatsnew_0150.dt: .dt accessor @@ -956,7 +958,6 @@ Bug Fixes - Bug with stacked barplots and NaNs (:issue:`8175`). - - Bug in interpolation methods with the ``limit`` keyword when no values needed interpolating (:issue:`7173`). - Bug where ``col_space`` was ignored in ``DataFrame.to_string()`` when ``header=False`` (:issue:`8230`). - Bug with ``DatetimeIndex.asof`` incorrectly matching partial strings and returning the wrong date (:issue:`8245`). diff --git a/pandas/core/generic.py b/pandas/core/generic.py index c9f83bfe07116..30f2d6bf093f2 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2266,6 +2266,7 @@ def fillna(self, value=None, method=None, axis=0, inplace=False, axis = self._get_axis_number(axis) method = com._clean_fill_method(method) + from pandas import DataFrame if value is None: if method is None: raise ValueError('must specify a fill method or value') @@ -2308,10 +2309,14 @@ def fillna(self, value=None, method=None, axis=0, inplace=False, if len(self._get_axis(axis)) == 0: return self - if self.ndim == 1 and value is not None: + if self.ndim == 1: if isinstance(value, (dict, com.ABCSeries)): from pandas import Series value = Series(value) + elif not com.is_list_like(value): + pass + else: + raise ValueError("invalid fill value with a %s" % type(value)) new_data = self._data.fillna(value=value, limit=limit, @@ -2331,11 +2336,15 @@ def fillna(self, value=None, method=None, axis=0, inplace=False, obj = result[k] obj.fillna(v, limit=limit, inplace=True) return result - else: + elif not com.is_list_like(value): new_data = self._data.fillna(value=value, limit=limit, inplace=inplace, downcast=downcast) + elif isinstance(value, DataFrame) and self.ndim == 2: + raise NotImplementedError("can't use fillna with a DataFrame, use .where instead") + else: + raise ValueError("invalid fill value with a %s" % type(value)) if inplace: self._update_inplace(new_data) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 18de6ac35bf9c..03dbfbbf1fd6c 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -7650,6 +7650,10 @@ def test_fillna_invalid_value(self): self.assertRaises(TypeError, self.frame.fillna, [1, 2]) # tuple self.assertRaises(TypeError, self.frame.fillna, (1, 2)) + # frame + self.assertRaises(NotImplementedError, self.frame.fillna, self.frame) + # frame with series + self.assertRaises(ValueError, self.frame.iloc[:,0].fillna, self.frame) def test_replace_inplace(self): self.tsframe['A'][:5] = nan