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

Equivalent to df[:n] for non-negative values too #57305

Closed
snoyes opened this issue Feb 8, 2024 · 4 comments · Fixed by #57518
Closed

Equivalent to df[:n] for non-negative values too #57305

snoyes opened this issue Feb 8, 2024 · 4 comments · Fixed by #57518
Labels
Docs Filters e.g. head, tail, nth

Comments

@snoyes
Copy link

snoyes commented Feb 8, 2024

The documentation for pandas.DataFrame.head mentions that negative values of n are equivalent to df[:n]. This is true for all values of n.

pandas/pandas/core/generic.py

Lines 5617 to 5618 in 2110b74

For negative values of `n`, this function returns all rows except
the last `|n|` rows, equivalent to ``df[:n]``.

@rhshadrach rhshadrach added Docs Filters e.g. head, tail, nth labels Feb 8, 2024
@rhshadrach
Copy link
Member

Thanks for the report. I believe the intention here is to specifically highlight the behavior for negative values, which users might not consider, rather than to suggest this isn't the behavior for positive values.

Do you find this confusing?

@snoyes
Copy link
Author

snoyes commented Feb 8, 2024

It made me pause to wonder why it was only mentioned in the "negative values" section, worried that implies it does not behave that way for positive values and 0. I think the df[:n] bit should appear in a separate paragraph, so it's clearly true for all n.

@jmarintur
Copy link
Contributor

Hi @snoyes and @rhshadrach, I think the examples are quite self-explanatory, what about changing the first part to something like this:

This function exhibits the same behavior as df[:n], returning the first n rows based on position. It is useful for quickly testing if your object has the right type of data in it.

When n is positive, it returns the first n rows. For n equal to 0, it returns an empty object. For negative values of n, it returns all rows except the last |n| rows, mirroring the behavior of df[:n].

If n is larger...

@jmarintur
Copy link
Contributor

Hi @rhshadrach, I've created this morning a PR to improve the definition. However, I see there are some unrelated issues that are preventing the branch to pass the checks. Is there anything I can do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Filters e.g. head, tail, nth
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants