Skip to content

Commit 2ff8646

Browse files
committed
API: .fillna will now raise a NotImplementedError when passed a DataFrame (GH8377)
1 parent 41b1f65 commit 2ff8646

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

doc/source/v0.15.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ API changes
278278
- ``DataFrame.info()`` now ends its output with a newline character (:issue:`8114`)
279279
- add ``copy=True`` argument to ``pd.concat`` to enable pass thrue of complete blocks (:issue:`8252`)
280280

281+
- ``.fillna`` will now raise a ``NotImplementedError`` when passed a ``DataFrame`` (:issue:`8377`)
282+
281283
.. _whatsnew_0150.dt:
282284

283285
.dt accessor
@@ -956,7 +958,6 @@ Bug Fixes
956958
- Bug with stacked barplots and NaNs (:issue:`8175`).
957959

958960

959-
960961
- Bug in interpolation methods with the ``limit`` keyword when no values needed interpolating (:issue:`7173`).
961962
- Bug where ``col_space`` was ignored in ``DataFrame.to_string()`` when ``header=False`` (:issue:`8230`).
962963
- Bug with ``DatetimeIndex.asof`` incorrectly matching partial strings and returning the wrong date (:issue:`8245`).

pandas/core/generic.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -2266,6 +2266,7 @@ def fillna(self, value=None, method=None, axis=0, inplace=False,
22662266
axis = self._get_axis_number(axis)
22672267
method = com._clean_fill_method(method)
22682268

2269+
from pandas import DataFrame
22692270
if value is None:
22702271
if method is None:
22712272
raise ValueError('must specify a fill method or value')
@@ -2308,10 +2309,14 @@ def fillna(self, value=None, method=None, axis=0, inplace=False,
23082309
if len(self._get_axis(axis)) == 0:
23092310
return self
23102311

2311-
if self.ndim == 1 and value is not None:
2312+
if self.ndim == 1:
23122313
if isinstance(value, (dict, com.ABCSeries)):
23132314
from pandas import Series
23142315
value = Series(value)
2316+
elif not com.is_list_like(value):
2317+
pass
2318+
else:
2319+
raise ValueError("invalid fill value with a %s" % type(value))
23152320

23162321
new_data = self._data.fillna(value=value,
23172322
limit=limit,
@@ -2331,11 +2336,15 @@ def fillna(self, value=None, method=None, axis=0, inplace=False,
23312336
obj = result[k]
23322337
obj.fillna(v, limit=limit, inplace=True)
23332338
return result
2334-
else:
2339+
elif not com.is_list_like(value):
23352340
new_data = self._data.fillna(value=value,
23362341
limit=limit,
23372342
inplace=inplace,
23382343
downcast=downcast)
2344+
elif isinstance(value, DataFrame) and self.ndim == 2:
2345+
raise NotImplementedError("can't use fillna with a DataFrame, use .where instead")
2346+
else:
2347+
raise ValueError("invalid fill value with a %s" % type(value))
23392348

23402349
if inplace:
23412350
self._update_inplace(new_data)

pandas/tests/test_frame.py

+4
Original file line numberDiff line numberDiff line change
@@ -7650,6 +7650,10 @@ def test_fillna_invalid_value(self):
76507650
self.assertRaises(TypeError, self.frame.fillna, [1, 2])
76517651
# tuple
76527652
self.assertRaises(TypeError, self.frame.fillna, (1, 2))
7653+
# frame
7654+
self.assertRaises(NotImplementedError, self.frame.fillna, self.frame)
7655+
# frame with series
7656+
self.assertRaises(ValueError, self.frame.iloc[:,0].fillna, self.frame)
76537657

76547658
def test_replace_inplace(self):
76557659
self.tsframe['A'][:5] = nan

0 commit comments

Comments
 (0)