Skip to content

Commit f742a66

Browse files
Albert Villanova del Moraljreback
Albert Villanova del Moral
authored andcommitted
BUG: Fix downcast argument for DataFrame.fillna()
closes pandas-dev#15277 Author: Albert Villanova del Moral <[email protected]> Closes pandas-dev#15278 from albertvillanova/fix-15277 and squashes the following commits: 1b594a9 [Albert Villanova del Moral] Fix tab indentation 631a2dc [Albert Villanova del Moral] Add whatsnew note d691954 [Albert Villanova del Moral] BUG: Fix downcast argument for DataFrame.fillna()
1 parent 7d6afc4 commit f742a66

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ Bug Fixes
496496
- Bug in ``pd.read_csv()`` for the C engine where ``usecols`` were being indexed incorrectly with ``parse_dates`` (:issue:`14792`)
497497
- Incorrect dtyped ``Series`` was returned by comparison methods (e.g., ``lt``, ``gt``, ...) against a constant for an empty ``DataFrame`` (:issue:`15077`)
498498
- Bug in ``Series.dt.round`` inconsistent behaviour on NAT's with different arguments (:issue:`14940`)
499+
- Bug in ``DataFrame.fillna()`` where the argument ``downcast`` was ignored when fillna value was of type ``dict`` (:issue:`15277`)
499500

500501

501502
- Bug in ``.read_json()`` for Python 2 where ``lines=True`` and contents contain non-ascii unicode characters (:issue:`15132`)
@@ -509,7 +510,5 @@ Bug Fixes
509510

510511

511512

512-
513-
514513
- Bug in ``DataFrame.boxplot`` where ``fontsize`` was not applied to the tick labels on both axes (:issue:`15108`)
515514
- Bug in ``Series.replace`` and ``DataFrame.replace`` which failed on empty replacement dicts (:issue:`15289`)

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3347,7 +3347,7 @@ def fillna(self, value=None, method=None, axis=None, inplace=False,
33473347
if k not in result:
33483348
continue
33493349
obj = result[k]
3350-
obj.fillna(v, limit=limit, inplace=True)
3350+
obj.fillna(v, limit=limit, inplace=True, downcast=downcast)
33513351
return result
33523352
elif not is_list_like(value):
33533353
new_data = self._data.fillna(value=value, limit=limit,

pandas/tests/frame/test_missing.py

+14
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,20 @@ def test_fillna(self):
252252
result = df.fillna(value={'Date': df['Date2']})
253253
assert_frame_equal(result, expected)
254254

255+
def test_fillna_downcast(self):
256+
# GH 15277
257+
# infer int64 from float64
258+
df = pd.DataFrame({'a': [1., np.nan]})
259+
result = df.fillna(0, downcast='infer')
260+
expected = pd.DataFrame({'a': [1, 0]})
261+
assert_frame_equal(result, expected)
262+
263+
# infer int64 from float64 when fillna value is a dict
264+
df = pd.DataFrame({'a': [1., np.nan]})
265+
result = df.fillna({'a': 0}, downcast='infer')
266+
expected = pd.DataFrame({'a': [1, 0]})
267+
assert_frame_equal(result, expected)
268+
255269
def test_fillna_dtype_conversion(self):
256270
# make sure that fillna on an empty frame works
257271
df = DataFrame(index=["A", "B", "C"], columns=[1, 2, 3, 4, 5])

pandas/tests/series/test_missing.py

+14
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,20 @@ def test_datetime64tz_fillna_round_issue(self):
273273

274274
assert_series_equal(filled, expected)
275275

276+
def test_fillna_downcast(self):
277+
# GH 15277
278+
# infer int64 from float64
279+
s = pd.Series([1., np.nan])
280+
result = s.fillna(0, downcast='infer')
281+
expected = pd.Series([1, 0])
282+
assert_series_equal(result, expected)
283+
284+
# infer int64 from float64 when fillna value is a dict
285+
s = pd.Series([1., np.nan])
286+
result = s.fillna({1: 0}, downcast='infer')
287+
expected = pd.Series([1, 0])
288+
assert_series_equal(result, expected)
289+
276290
def test_fillna_int(self):
277291
s = Series(np.random.randint(-100, 100, 50))
278292
s.fillna(method='ffill', inplace=True)

0 commit comments

Comments
 (0)