Reputation: 59
I'm working on a small project to parse and create graphs based on DNC Primary data. It's all functional so far but I'm working on getting rid of the following SettingWithCopyWarning
:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
candidate['end_date'] = pd.to_datetime(candidate_state['end_date'])
I've tried changing the referenced line to:
candidate.loc['end_date'] = pd.to_datetime(candidate_state['end_date'])
But that throws another error about some kind of comparison.
Upvotes: 0
Views: 1311
Reputation: 353
It is hard to confirm without seeing the full code, but this warning is usually a result of the dataframe you are working on (candidate
in this case) being a "copy" of a filtered selection of a larger dataframe. In other words, when you created candidate
you did something like:
candidate = df_larger_dataset[df_larger_dataset['some_column'] == 'some_value']
The reason you get the warning is because when you do this, you don't actually create a new object, just a reference, which means when you start making changes to candidate
, you are also modifying df_larger_dataset
. This may or may not matter in your context, but to avoid the warning, when you create 'candidate' make it an explicit copy of df_larger_dataset
:
candidate = df_larger_dataset[df_larger_dataset['some_column'] == some_value].copy()
Upvotes: 2