From d691954715f3a11e89f029bd48da245c1fcb9c34 Mon Sep 17 00:00:00 2001 From: Albert Villanova del Moral Date: Wed, 1 Feb 2017 07:13:04 +0100 Subject: [PATCH 1/3] BUG: Fix downcast argument for DataFrame.fillna() closes #15277 Author: Albert Villanova del Moral --- pandas/core/generic.py | 2 +- pandas/tests/frame/test_missing.py | 13 +++++++++++++ pandas/tests/series/test_missing.py | 13 +++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 8074b167ff176..bb2664a5b8d28 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3347,7 +3347,7 @@ def fillna(self, value=None, method=None, axis=None, inplace=False, if k not in result: continue obj = result[k] - obj.fillna(v, limit=limit, inplace=True) + obj.fillna(v, limit=limit, inplace=True, downcast=downcast) return result elif not is_list_like(value): new_data = self._data.fillna(value=value, limit=limit, diff --git a/pandas/tests/frame/test_missing.py b/pandas/tests/frame/test_missing.py index c4f037e85edf6..1cce65774262d 100644 --- a/pandas/tests/frame/test_missing.py +++ b/pandas/tests/frame/test_missing.py @@ -252,6 +252,19 @@ def test_fillna(self): result = df.fillna(value={'Date': df['Date2']}) assert_frame_equal(result, expected) + def test_fillna_downcast(self): + # infer int64 from float64 + df = pd.DataFrame({'a': [1., np.nan]}) + result = df.fillna(0, downcast='infer') + expected = pd.DataFrame({'a': [1, 0]}) + assert_frame_equal(result, expected) + + # infer int64 from float64 when fillna value is a dict + df = pd.DataFrame({'a': [1., np.nan]}) + result = df.fillna({'a': 0}, downcast='infer') + expected = pd.DataFrame({'a': [1, 0]}) + assert_frame_equal(result, expected) + def test_fillna_dtype_conversion(self): # make sure that fillna on an empty frame works df = DataFrame(index=["A", "B", "C"], columns=[1, 2, 3, 4, 5]) diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index 3c82e4ed82969..89caba5ab193f 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -268,6 +268,19 @@ def test_datetime64tz_fillna_round_issue(self): assert_series_equal(filled, expected) + def test_fillna_downcast(self): + # infer int64 from float64 + s = pd.Series([1., np.nan]) + result = s.fillna(0, downcast='infer') + expected = pd.Series([1, 0]) + assert_series_equal(result, expected) + + # infer int64 from float64 when fillna value is a dict + s = pd.Series([1., np.nan]) + result = s.fillna({1: 0}, downcast='infer') + expected = pd.Series([1, 0]) + assert_series_equal(result, expected) + def test_fillna_int(self): s = Series(np.random.randint(-100, 100, 50)) s.fillna(method='ffill', inplace=True) From 631a2dc901fcfe0abf09ff4cf08ad45fc14dbb5b Mon Sep 17 00:00:00 2001 From: Albert Villanova del Moral Date: Fri, 3 Feb 2017 19:44:25 +0100 Subject: [PATCH 2/3] Add whatsnew note --- doc/source/whatsnew/v0.20.0.txt | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index d76a78c68fb73..99f1bb30a0f26 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -505,11 +505,7 @@ Bug Fixes - Bug in ``DataFrame.to_stata()`` and ``StataWriter`` which produces incorrectly formatted files to be produced for some locales (:issue:`13856`) - - - - - - - Bug in ``DataFrame.boxplot`` where ``fontsize`` was not applied to the tick labels on both axes (:issue:`15108`) - Bug in ``Series.replace`` and ``DataFrame.replace`` which failed on empty replacement dicts (:issue:`15289`) + +- Bug in ``DataFrame.fillna()`` where the argument ``downcast`` was ignored when fillna value was of type dict instead of a scalar (:issue:`15277`) From 1b594a950fb180c5a5b3fee3b2b2228b7d187cd0 Mon Sep 17 00:00:00 2001 From: Albert Villanova del Moral Date: Mon, 6 Feb 2017 00:06:49 +0100 Subject: [PATCH 3/3] Fix tab indentation --- pandas/tests/frame/test_missing.py | 22 +++++++++++----------- pandas/tests/series/test_missing.py | 8 ++++---- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pandas/tests/frame/test_missing.py b/pandas/tests/frame/test_missing.py index 1cce65774262d..aeaaa0d8a8e2b 100644 --- a/pandas/tests/frame/test_missing.py +++ b/pandas/tests/frame/test_missing.py @@ -253,17 +253,17 @@ def test_fillna(self): assert_frame_equal(result, expected) def test_fillna_downcast(self): - # infer int64 from float64 - df = pd.DataFrame({'a': [1., np.nan]}) - result = df.fillna(0, downcast='infer') - expected = pd.DataFrame({'a': [1, 0]}) - assert_frame_equal(result, expected) - - # infer int64 from float64 when fillna value is a dict - df = pd.DataFrame({'a': [1., np.nan]}) - result = df.fillna({'a': 0}, downcast='infer') - expected = pd.DataFrame({'a': [1, 0]}) - assert_frame_equal(result, expected) + # infer int64 from float64 + df = pd.DataFrame({'a': [1., np.nan]}) + result = df.fillna(0, downcast='infer') + expected = pd.DataFrame({'a': [1, 0]}) + assert_frame_equal(result, expected) + + # infer int64 from float64 when fillna value is a dict + df = pd.DataFrame({'a': [1., np.nan]}) + result = df.fillna({'a': 0}, downcast='infer') + expected = pd.DataFrame({'a': [1, 0]}) + assert_frame_equal(result, expected) def test_fillna_dtype_conversion(self): # make sure that fillna on an empty frame works diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index 89caba5ab193f..dccde42f78797 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -270,10 +270,10 @@ def test_datetime64tz_fillna_round_issue(self): def test_fillna_downcast(self): # infer int64 from float64 - s = pd.Series([1., np.nan]) - result = s.fillna(0, downcast='infer') - expected = pd.Series([1, 0]) - assert_series_equal(result, expected) + s = pd.Series([1., np.nan]) + result = s.fillna(0, downcast='infer') + expected = pd.Series([1, 0]) + assert_series_equal(result, expected) # infer int64 from float64 when fillna value is a dict s = pd.Series([1., np.nan])