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

Hidden Chaining responsible for SettingWithCopyWarning #5

Open
paulelvers opened this issue Nov 11, 2020 · 0 comments
Open

Hidden Chaining responsible for SettingWithCopyWarning #5

paulelvers opened this issue Nov 11, 2020 · 0 comments

Comments

@paulelvers
Copy link

paulelvers commented Nov 11, 2020

Hidden chaining is another frequent issue responsible for SettingWithCopyWarning. It may occur the following way:

import pandas as pd
import numpy as np
d = {'col1': [1, 2, 3], 'col2': [3, 4, np.nan]}
df = pd.DataFrame(data=d)

# This will cause the warning
df_no_na = df.dropna()
df_no_na.loc[:, 'new_column'] = df_no_na['col1'] + df_no_na['col2']

# This won't cause the warning
df_no_na = df.dropna().copy()
df_no_na.loc[: ,'new_column'] = df_no_na['col1'] + df_no_na['col2']

The point is that it also occurs when using df.loc[:, 'new_column'] so using .loc[] does not always prevent this warning from happening. The reason for the warning is obviously the same as in the example in your notebook, however i think this issue of chaining a get and an assignment is less obvious, hence 'hidden' :-).

@paulelvers paulelvers changed the title Hidden Chaining Hidden Chaining responsible for SettingWithCopyWarning Nov 11, 2020
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

No branches or pull requests

1 participant