Skip to content
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

Fix bug in Series.describe where the median is included any time the percentiles argument is not None #61158

Merged
merged 7 commits into from
Mar 21, 2025

Conversation

MartinBraquet
Copy link
Contributor

@MartinBraquet MartinBraquet commented Mar 21, 2025

This PR aims at fixing the bug mentioned in the issue referenced above. Multiple PRs (#61024, #61023 , #60986 , #60557) have already attempted to resolve it, yet all of them got closed or stale for more than 2 weeks. So I allowed myself to apply here all the comments in the issue and previous PRs.

One last thing left hanging pertains to backward incompatibility. The only case in which the result will change is when percentiles is a list that does not contain 0.5. Before the PR, it adds 0.5 to the result; after the PR, it does not include it.
If we add a warning message in such case, it would be considered as:

  • useful for the people who read the median even though they didn't include it in the list of percentiles
  • spamming / undesirable for the rest

So, what is pandas' policy regarding warning messages and its potentially large number of false alerts?

Comment on lines +1568 to +1570
if len(percentiles) == 0:
return []

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is backward-compatible as it is only extending the range of values that the input parameter can take.

Comment on lines 10821 to 10826
fall between 0 and 1. The default is
``[.25, .5, .75]``, which returns the 25th, 50th, and
75th percentiles.
fall between 0 and 1. Here are the options:
- A list-like of numbers : To include the percentiles listed. If
that list is empty, no percentiles will be returned.
- None (default) : To include the default percentiles, which are the
25th, 50th, and 75th ones.
Copy link
Member

Choose a reason for hiding this comment

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

I think you can just keep the old description but clarify The default, ``None``, will automatically return the 25th, 50th, and 75th percentiles.

**{f"{p:.0%}": df.a.quantile(p) for p in percentiles},
"max": df.a.max(),
},
).to_frame(name="a")
Copy link
Member

Choose a reason for hiding this comment

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

Can you just create expected = DataFrame(...) instead of using to_frame?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is the new formulation appropriate? I passed index=['a'] and had to transpose. Maybe there is a cleaner way such as to avoid the transpose?

Copy link
Member

Choose a reason for hiding this comment

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

You can do:

DataFrame(
    [len(df.a), df.a.mean(), ..., **[df.a.quantile(p) for p in percentiles], df.a.max()],
    index=pd.Index(["count", ..., **[f"{p:.0%}" for p in percentiles], ...]),
    column=["a"]
)

Copy link
Contributor Author

@MartinBraquet MartinBraquet Mar 21, 2025

Choose a reason for hiding this comment

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

Good point, although not sure it's really cleaner, as the list exhaustion is duplicated and this removes the clarity of the mapping from key to value. Is it fine as currently is, or would you rather prefer to have it as you mentioned just above?

Copy link
Member

Choose a reason for hiding this comment

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

I would prefer the suggested method to not exercise other extraneous pandas APIs in this test like transpose or to_frame

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes total sense; will update it right now.

@mroeschke mroeschke added the Series Series data structure label Mar 21, 2025
@MartinBraquet MartinBraquet requested a review from mroeschke March 21, 2025 16:18
@MartinBraquet
Copy link
Contributor Author

@mroeschke Thanks for the feedback! I applied your comments. Lmk if there's anything else to update.

@mroeschke mroeschke added this to the 3.0 milestone Mar 21, 2025
@mroeschke mroeschke merged commit dc8401a into pandas-dev:main Mar 21, 2025
42 checks passed
@mroeschke
Copy link
Member

Thanks @MartinBraquet

@MartinBraquet MartinBraquet deleted the describe branch March 22, 2025 05:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Series Series data structure
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: Passing a single value to .describe(percentiles = [0.25]) returns 25th- and 50th-percentile
2 participants