Skip to content

API: .fillna will now raise a NotImplementedError when passed a DataFrame (GH8377) #8378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`).
Expand Down
13 changes: 11 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down