Skip to content

BUG: fix+test quantile with empty DataFrame, closes #23925 #27436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Timezones

Numeric
^^^^^^^

- Bug in :meth:`DataFrame.quantile` with zero-column :class:`DataFrame` incorrectly raising (:issue:`23925`)
-
-

Expand Down Expand Up @@ -191,10 +191,6 @@ ExtensionArray
-


Other
^^^^^


.. _whatsnew_1000.contributors:

Contributors
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
16 changes: 15 additions & 1 deletion pandas/tests/frame/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be worth testing this with timedelta & period data as well (just to confirm that they work), but can do followup.

# 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a name to the index of the frame which I think should propagate here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. i think it should be df.columns.name that propagates.

It looks like DataFrame_get_numeric_data isn't retaining the columns names, will open a separate issue

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also I pushed the 1.0 whatsnew if you rebase

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)