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
6 changes: 6 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=q, columns=[])
return self._constructor_sliced([], index=[], name=q)

result = data._data.quantile(
qs=q, axis=1, interpolation=interpolation, transposed=is_transposed
)
Expand Down
13 changes: 12 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,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):
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))
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

tm.assert_series_equal(result, expected)

result = df.quantile([0.5])
expected = pd.DataFrame([], index=[0.5], columns=[])
tm.assert_frame_equal(result, expected)