Skip to content

Fixed describe.py #61024

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

Closed
wants to merge 14 commits into from
5 changes: 2 additions & 3 deletions pandas/core/methods/describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,8 @@ def _refine_percentiles(
# get them all to be in [0, 1]
validate_percentile(percentiles)

# median should always be included
if 0.5 not in percentiles:
percentiles.append(0.5)
if percentiles == []:
percentiles.append(0.5) # By default, if percentiles is empty then append 50th percentile.
Copy link
Member

Choose a reason for hiding this comment

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

As mentioned in the linked issue, I think pandas should not be adding in 0.5 when the user specifies percentiles=[].

Copy link
Author

@Abhibhav2003 Abhibhav2003 Mar 1, 2025

Choose a reason for hiding this comment

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

As mentioned in the linked issue, I think pandas should not be adding in 0.5 when the user specifies percentiles=[].

I have removed the automatic addition of 0.5 when percentiles=[], as per the feedback.

Should percentiles=[] return an empty result, or should it raise an error?

Copy link
Member

Choose a reason for hiding this comment

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

percentiles=[] should produce a result without any percentiles in it. But the result itself will not be empty.

Copy link
Author

Choose a reason for hiding this comment

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

Done!
Added Tests as well!

import numpy as np

import pandas as pd

frame = pd.DataFrame(np.array([1, 2, 3, 4, 5, 100]))

print(frame.describe(percentiles=[]))

Here's the required Output :
image

print(frame.describe(percentiles=[0.25]))

Output :

image


percentiles = np.asarray(percentiles)

Expand Down
Loading