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

Update nsfg.py to fix FutureWarning #217

Merged
merged 1 commit into from
Jan 23, 2025

Conversation

debnath-d
Copy link
Contributor

Fix warning:

ThinkStats2/code/nsfg.py:66: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method. The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.

For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.

Fix warning:

ThinkStats2/code/nsfg.py:66: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.

For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.
@AllenDowney
Copy link
Owner

Thanks for this PR!

It looks like this combines multiple calls to replace into a single call, but it still uses inplace=True, so doesn't it generate the same warning?

@debnath-d
Copy link
Contributor Author

No it does not generate the warning. In fact, the warning gives the hint that this will work, and the way it was done before won't work in Pandas 3.0.

Please read the full warning.

@AllenDowney
Copy link
Owner

Sorry, I was thinking of a different issue, the deprecation of inplace in anticipation of Pandas 2. But we don't have to deal with that now.

@AllenDowney
Copy link
Owner

Looks like there's a failing test that's unrelated to this PR, so let's get this merged and then I'll deal with the other issue.

@AllenDowney AllenDowney merged commit bc965c2 into AllenDowney:master Jan 23, 2025
0 of 2 checks passed
@debnath-d
Copy link
Contributor Author

Also, I wanted to say, thank you for all your excellent free books and code!

I'm going through the code notebooks of your book. I'll keep making pull requests whenever I find more such minor updates needed to make the code compatible with the latest versions of the used libraries.

@debnath-d debnath-d deleted the patch-1 branch January 23, 2025 16:58
@AllenDowney
Copy link
Owner

I appreciate that, but the 3rd edition is almost done. The online version is substantially complete at https://allendowney.github.io/ThinkStats/, and the print edition is going into production now. So if it is not too late, you might want to switch!

@debnath-d
Copy link
Contributor Author

Oh that's lovely! Thank you for pointing me to this! I just started, so perfect time for me to switch to your latest work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants