diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index eed48f9e46897..bccb6f991d646 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -108,7 +108,7 @@ Timezones Numeric ^^^^^^^ - +- Bug in :meth:`DataFrame.quantile` with zero-column :class:`DataFrame` incorrectly raising (:issue:`23925`) - - @@ -191,10 +191,6 @@ ExtensionArray - -Other -^^^^^ - - .. _whatsnew_1000.contributors: Contributors diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4beaf301988b4..46dc9204b86f5 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8245,6 +8245,13 @@ 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 + cols = Index([], name=self.columns.name) + if is_list_like(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 236cadf67735d..e5e881dece34a 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,17 @@ 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)) + 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)