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

docs: Fix sign in pandas example of column assignment #19166

Closed
wants to merge 1 commit into from

Conversation

jennynz
Copy link

@jennynz jennynz commented Oct 9, 2024

In the docs on coming from pandas, under column assignment, the example says:

When the value in column c is equal to 2 then we replace the value in a with the value in b.

But the pandas example shown is:

df.assign(a=lambda df_: df_.a.where(df_.c != 2, df_.b))

This just changes the sign so it's actually when c is equal to 2, rather than not equal.

@jennynz jennynz changed the title docs: fix sign in pandas example of column assignment docs: Fix sign in pandas example of column assignment Oct 9, 2024
@github-actions github-actions bot added documentation Improvements or additions to documentation python Related to Python Polars rust Related to Rust Polars and removed title needs formatting labels Oct 9, 2024
@jennynz
Copy link
Author

jennynz commented Oct 9, 2024

Broken link is an existing one it seems:

ERROR: 1 dead links found!
[✖] https://matplotlib.org/ → Status: 403

@rodrigogiraoserrao
Copy link
Collaborator

rodrigogiraoserrao commented Oct 10, 2024

This looks correct to me 👇 the first five rows of df have the value 2 in column c and in the result those five rows had the values of a replaced by the values of b.
Isn't this what's described?

What may be causing the confusion is where replaces values where the condition is False (c.f. the pandas docs on Series.where.

>>> df = pd.DataFrame({
...     "a": list(range(10)),
...     "b": [99] * 10,
...     "c": [2] * 5 + [0] * 5,
... })
>>>
>>> df
   a   b  c
0  0  99  2
1  1  99  2
2  2  99  2
3  3  99  2
4  4  99  2
5  5  99  0
6  6  99  0
7  7  99  0
8  8  99  0
9  9  99  0
>>> df.assign(a=lambda df_: df_.a.where(df_.c != 2, df_.b))
    a   b  c
0  99  99  2
1  99  99  2
2  99  99  2
3  99  99  2
4  99  99  2
5   5  99  0
6   6  99  0
7   7  99  0
8   8  99  0
9   9  99  0

@jennynz jennynz deleted the docs/fix-pandas-example branch October 10, 2024 22:30
@jennynz
Copy link
Author

jennynz commented Oct 10, 2024

Ah apologies, you're right - I did get confused about how where works!

@rodrigogiraoserrao
Copy link
Collaborator

No worries. I triple checked everything before closing your PR because I was so confused as well. Double negations get me every time 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants