From ae2834b9ea7daf6c23c01e170866aa03c62571cb Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 17 Jul 2019 11:16:11 -0700 Subject: [PATCH 1/5] BUG: fix+test quantile with empty DataFrame --- pandas/core/frame.py | 6 ++++++ pandas/tests/frame/test_quantile.py | 13 ++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f45a13249b16c..6cbb5a16341d7 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8196,6 +8196,12 @@ def quantile(self, q=0.5, axis=0, numeric_only=True, interpolation="linear"): if is_transposed: data = data.T + if len(data.columns) == 0: + # GH#23925 _get_numeric_data may have dropped all columns + if is_list_like(q): + return self._constructor([], index=[], columns=q) + return self._constructor_sliced([], index=[], name=q) + result = data._data.quantile( qs=q, axis=1, interpolation=interpolation, transposed=is_transposed ) diff --git a/pandas/tests/frame/test_quantile.py b/pandas/tests/frame/test_quantile.py index 236cadf67735d..801df43f4d86f 100644 --- a/pandas/tests/frame/test_quantile.py +++ b/pandas/tests/frame/test_quantile.py @@ -439,7 +439,7 @@ def test_quantile_nat(self): ) tm.assert_frame_equal(res, exp) - def test_quantile_empty(self): + def test_quantile_empty_no_rows(self): # floats df = DataFrame(columns=["a", "b"], dtype="float64") @@ -467,3 +467,14 @@ def test_quantile_empty(self): # FIXME (gives NaNs instead of NaT in 0.18.1 or 0.19.0) # res = df.quantile(0.5, numeric_only=False) + + def test_quantile_empty_no_columns(self): + # GH#23925 _get_numeric_data may drop all columns + df = pd.DataFrame(pd.date_range("1/1/18", periods=5)) + result = df.quantile(0.5) + expected = pd.Series([], index=[], name=0.5) + tm.assert_series_equal(result, expected) + + result = df.quantile([0.5]) + expected = pd.DataFrame([], index=[], columns=[0.5]) + tm.assert_frame_equal(result, expected) From c9ba3e6bc93cdd0dcf4e30cc069a9dcf01ce3673 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 17 Jul 2019 14:28:25 -0700 Subject: [PATCH 2/5] flip index/columns --- pandas/core/frame.py | 2 +- pandas/tests/frame/test_quantile.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6cbb5a16341d7..005bd1bc53021 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8199,7 +8199,7 @@ def quantile(self, q=0.5, axis=0, numeric_only=True, interpolation="linear"): if len(data.columns) == 0: # GH#23925 _get_numeric_data may have dropped all columns if is_list_like(q): - return self._constructor([], index=[], columns=q) + return self._constructor([], index=q, columns=[]) return self._constructor_sliced([], index=[], name=q) result = data._data.quantile( diff --git a/pandas/tests/frame/test_quantile.py b/pandas/tests/frame/test_quantile.py index 801df43f4d86f..e3fcc4730fbbf 100644 --- a/pandas/tests/frame/test_quantile.py +++ b/pandas/tests/frame/test_quantile.py @@ -476,5 +476,5 @@ def test_quantile_empty_no_columns(self): tm.assert_series_equal(result, expected) result = df.quantile([0.5]) - expected = pd.DataFrame([], index=[], columns=[0.5]) + expected = pd.DataFrame([], index=[0.5], columns=[]) tm.assert_frame_equal(result, expected) From 2be56dc04ae2622bb0d09a63cd41cdbf0ef0b574 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 20 Jul 2019 13:31:31 -0700 Subject: [PATCH 3/5] ensure name is retained --- pandas/core/frame.py | 5 +++-- pandas/tests/frame/test_quantile.py | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 78ee033a91bf3..20d58f90fd4fd 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8259,9 +8259,10 @@ def quantile(self, q=0.5, axis=0, numeric_only=True, interpolation="linear"): if len(data.columns) == 0: # GH#23925 _get_numeric_data may have dropped all columns + cols = Index([], name=self.columns.name) if is_list_like(q): - return self._constructor([], index=q, columns=[]) - return self._constructor_sliced([], index=[], name=q) + return self._constructor([], index=q, columns=cols) + return self._constructor_sliced([], index=cols, name=q) result = data._data.quantile( qs=q, axis=1, interpolation=interpolation, transposed=is_transposed diff --git a/pandas/tests/frame/test_quantile.py b/pandas/tests/frame/test_quantile.py index e3fcc4730fbbf..e5e881dece34a 100644 --- a/pandas/tests/frame/test_quantile.py +++ b/pandas/tests/frame/test_quantile.py @@ -471,10 +471,13 @@ def test_quantile_empty_no_rows(self): def test_quantile_empty_no_columns(self): # GH#23925 _get_numeric_data may drop all columns df = pd.DataFrame(pd.date_range("1/1/18", periods=5)) + df.columns.name = "captain tightpants" result = df.quantile(0.5) expected = pd.Series([], index=[], name=0.5) + expected.index.name = "captain tightpants" tm.assert_series_equal(result, expected) result = df.quantile([0.5]) expected = pd.DataFrame([], index=[0.5], columns=[]) + expected.columns.name = "captain tightpants" tm.assert_frame_equal(result, expected) From c83255cb2de779e9ce5c0b54676c125ec0f43fed Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 20 Jul 2019 16:51:56 -0700 Subject: [PATCH 4/5] whatsnew --- doc/source/whatsnew/v1.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 9caf127553e05..4f8f7752042dd 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -193,6 +193,7 @@ ExtensionArray Other ^^^^^ +- Bug in :meth:`DataFrame.quantile` with zero-column :class:`DataFrame` incorrectly raising (:issue:`23925`) .. _whatsnew_1000.contributors: From 9705eb5ba9e839db6b99baed072dfda29213f45e Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 20 Jul 2019 18:22:10 -0700 Subject: [PATCH 5/5] move to numeric section --- doc/source/whatsnew/v1.0.0.rst | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 4f8f7752042dd..99fb25a98f7e3 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -107,7 +107,7 @@ Timezones Numeric ^^^^^^^ - +- Bug in :meth:`DataFrame.quantile` with zero-column :class:`DataFrame` incorrectly raising (:issue:`23925`) - - @@ -190,11 +190,6 @@ ExtensionArray - -Other -^^^^^ - -- Bug in :meth:`DataFrame.quantile` with zero-column :class:`DataFrame` incorrectly raising (:issue:`23925`) - .. _whatsnew_1000.contributors: Contributors