From 9352b201e022ab42c1f4a72f49cf8870d7191296 Mon Sep 17 00:00:00 2001 From: Yuanhao Geng <41546976+GYHHAHA@users.noreply.github.com> Date: Wed, 13 Jul 2022 23:04:43 -0500 Subject: [PATCH 1/7] Update generic.py --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index e392802bdb5ea..4325f0eb04a9c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6679,7 +6679,7 @@ def fillna( return result if not inplace else None elif not is_list_like(value): - if not self._mgr.is_single_block and axis == 1: + if axis == 1: result = self.T.fillna(value=value, limit=limit).T From 9a8cf3bf192b18ebfa45f1936bfcd0d875ca695e Mon Sep 17 00:00:00 2001 From: Yuanhao Geng <41546976+GYHHAHA@users.noreply.github.com> Date: Wed, 13 Jul 2022 23:07:00 -0500 Subject: [PATCH 2/7] Update test_fillna.py --- pandas/tests/frame/methods/test_fillna.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pandas/tests/frame/methods/test_fillna.py b/pandas/tests/frame/methods/test_fillna.py index f5c9dd65e4760..4834af962eaeb 100644 --- a/pandas/tests/frame/methods/test_fillna.py +++ b/pandas/tests/frame/methods/test_fillna.py @@ -685,6 +685,29 @@ def test_inplace_dict_update_view(self, val): tm.assert_frame_equal(df, expected) tm.assert_frame_equal(result_view, expected) + def test_single_block_df_with_horizontal_axis(self): + # GH 47713 + df = DataFrame( + { + 'col1': [5, 0, np.nan, 10, np.nan], + 'col2': [7, np.nan, np.nan, 5, 3], + 'col3': [12, np.nan, 1, 2, 0], + 'col4': [np.nan, 1, 1, np.nan, 18], + } + ) + result = df.fillna(50, limit=1, axis=1) + expected = DataFrame( + [ + [ 5., 7., 12., 50.], + [ 0., 50., np.nan, 1.], + [50., np.nan, 1., 1.], + [10., 5., 2., 50.], + [50., 3., 0., 18.], + ], + columns=["col1", "col2", "col3", "col4"], + ) + tm.assert_frame_equal(result, expected) + def test_fillna_nonconsolidated_frame(): # https://github.com/pandas-dev/pandas/issues/36495 From d3c0d2205971ba22c2755dd34ad47bd1a38baada Mon Sep 17 00:00:00 2001 From: Yuanhao Geng <41546976+GYHHAHA@users.noreply.github.com> Date: Wed, 13 Jul 2022 23:10:04 -0500 Subject: [PATCH 3/7] Update v1.5.0.rst --- doc/source/whatsnew/v1.5.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index a6408b940119d..e3740d04cd065 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -914,6 +914,7 @@ Missing - Bug in :meth:`Series.fillna` and :meth:`DataFrame.fillna` with :class:`IntervalDtype` and incompatible value raising instead of casting to a common (usually object) dtype (:issue:`45796`) - Bug in :meth:`DataFrame.interpolate` with object-dtype column not returning a copy with ``inplace=False`` (:issue:`45791`) - Bug in :meth:`DataFrame.dropna` allows to set both ``how`` and ``thresh`` incompatible arguments (:issue:`46575`) +- Bug in :meth:`DataFrame.fillna` on single block :class:`DataFrame` with ``axis=1`` (:issue:`47713`) MultiIndex ^^^^^^^^^^ From 81c8e9bcb042d69a7ae3932386befbde07b70d69 Mon Sep 17 00:00:00 2001 From: Yuanhao Geng <41546976+GYHHAHA@users.noreply.github.com> Date: Wed, 13 Jul 2022 23:13:46 -0500 Subject: [PATCH 4/7] fix array format --- pandas/tests/frame/methods/test_fillna.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/frame/methods/test_fillna.py b/pandas/tests/frame/methods/test_fillna.py index 4834af962eaeb..4880a80ea43f2 100644 --- a/pandas/tests/frame/methods/test_fillna.py +++ b/pandas/tests/frame/methods/test_fillna.py @@ -698,11 +698,11 @@ def test_single_block_df_with_horizontal_axis(self): result = df.fillna(50, limit=1, axis=1) expected = DataFrame( [ - [ 5., 7., 12., 50.], - [ 0., 50., np.nan, 1.], - [50., np.nan, 1., 1.], - [10., 5., 2., 50.], - [50., 3., 0., 18.], + [5., 7., 12., 50.], + [0., 50., np.nan, 1.], + [50., np.nan, 1., 1.], + [10., 5., 2., 50.], + [50., 3., 0., 18.], ], columns=["col1", "col2", "col3", "col4"], ) From 23b2c4901a4def59c3f700d264c2ac671b1bcf47 Mon Sep 17 00:00:00 2001 From: Yuanhao Geng <41546976+GYHHAHA@users.noreply.github.com> Date: Wed, 13 Jul 2022 23:16:05 -0500 Subject: [PATCH 5/7] fix what's new description --- doc/source/whatsnew/v1.5.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index e3740d04cd065..741520322fa72 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -914,7 +914,7 @@ Missing - Bug in :meth:`Series.fillna` and :meth:`DataFrame.fillna` with :class:`IntervalDtype` and incompatible value raising instead of casting to a common (usually object) dtype (:issue:`45796`) - Bug in :meth:`DataFrame.interpolate` with object-dtype column not returning a copy with ``inplace=False`` (:issue:`45791`) - Bug in :meth:`DataFrame.dropna` allows to set both ``how`` and ``thresh`` incompatible arguments (:issue:`46575`) -- Bug in :meth:`DataFrame.fillna` on single block :class:`DataFrame` with ``axis=1`` (:issue:`47713`) +- Bug in :meth:`DataFrame.fillna` ignored `axis` when :class:`DataFrame` is single block (:issue:`47713`) MultiIndex ^^^^^^^^^^ From 9d6fe602c63ec2bfff3477461af762c0aa8abc29 Mon Sep 17 00:00:00 2001 From: Yuanhao Geng <41546976+GYHHAHA@users.noreply.github.com> Date: Wed, 13 Jul 2022 23:16:57 -0500 Subject: [PATCH 6/7] fix format --- doc/source/whatsnew/v1.5.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 741520322fa72..9651269963803 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -914,7 +914,7 @@ Missing - Bug in :meth:`Series.fillna` and :meth:`DataFrame.fillna` with :class:`IntervalDtype` and incompatible value raising instead of casting to a common (usually object) dtype (:issue:`45796`) - Bug in :meth:`DataFrame.interpolate` with object-dtype column not returning a copy with ``inplace=False`` (:issue:`45791`) - Bug in :meth:`DataFrame.dropna` allows to set both ``how`` and ``thresh`` incompatible arguments (:issue:`46575`) -- Bug in :meth:`DataFrame.fillna` ignored `axis` when :class:`DataFrame` is single block (:issue:`47713`) +- Bug in :meth:`DataFrame.fillna` ignored ``axis`` when :class:`DataFrame` is single block (:issue:`47713`) MultiIndex ^^^^^^^^^^ From 506d8b5a4b0448d434ba7ab7bedb2cca84005116 Mon Sep 17 00:00:00 2001 From: Yuanhao Geng <41546976+GYHHAHA@users.noreply.github.com> Date: Wed, 13 Jul 2022 23:27:30 -0500 Subject: [PATCH 7/7] Update test_fillna.py --- pandas/tests/frame/methods/test_fillna.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/tests/frame/methods/test_fillna.py b/pandas/tests/frame/methods/test_fillna.py index 4880a80ea43f2..d86c1b2aedcac 100644 --- a/pandas/tests/frame/methods/test_fillna.py +++ b/pandas/tests/frame/methods/test_fillna.py @@ -689,20 +689,20 @@ def test_single_block_df_with_horizontal_axis(self): # GH 47713 df = DataFrame( { - 'col1': [5, 0, np.nan, 10, np.nan], - 'col2': [7, np.nan, np.nan, 5, 3], - 'col3': [12, np.nan, 1, 2, 0], - 'col4': [np.nan, 1, 1, np.nan, 18], + "col1": [5, 0, np.nan, 10, np.nan], + "col2": [7, np.nan, np.nan, 5, 3], + "col3": [12, np.nan, 1, 2, 0], + "col4": [np.nan, 1, 1, np.nan, 18], } ) result = df.fillna(50, limit=1, axis=1) expected = DataFrame( [ - [5., 7., 12., 50.], - [0., 50., np.nan, 1.], - [50., np.nan, 1., 1.], - [10., 5., 2., 50.], - [50., 3., 0., 18.], + [5.0, 7.0, 12.0, 50.0], + [0.0, 50.0, np.nan, 1.0], + [50.0, np.nan, 1.0, 1.0], + [10.0, 5.0, 2.0, 50.0], + [50.0, 3.0, 0.0, 18.0], ], columns=["col1", "col2", "col3", "col4"], )