I have a dataframe column period that has values by Quarters(Q1,Q2,Q3,Q4) that I want to convert into associated month (see dict). My code below works however wondering why I'm getting this warning.
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
quarter = {"Q1":"Mar","Q2":"Jun","Q3":"Sep","Q4":"Dec"}
df['period'] = df['period'].astype(str).map(quarter)
A value is trying to be set on a copy of a slice from a DataFrame. One approach that can be used to suppress SettingWithCopyWarning is to perform the chained operations into just a single loc operation. This will ensure that the assignment happens on the original DataFrame instead of a copy.
Generally, to avoid a SettingWithCopyWarning in Pandas, you should do the following: Avoid chained assignments that combine two or more indexing operations like df["z"][mask] = 0 and df. loc[mask]["z"] = 0 . Apply single assignments with just one indexing operation like df.
"A value is trying to be set on a copy of a slice from a DataFrame" is a warning. SO contains many posts on this subject.
df.assign
was added in Pandas 0.16 and is a good way to avoid this warning.
quarter = {"Q1": "Mar", "Q2": "Jun", "Q3": "Sep", "Q4": "Dec"}
df = pd.DataFrame({'period': ['Q1', 'Q2', 'Q3', 'Q4', 'Q5'], 'qtr': [1, 2, 3, 4, 5]})
df
period qtr
0 Q1 1
1 Q2 2
2 Q3 3
3 Q4 4
4 Q5 5
df = df.assign(period=[quarter.get(q, q) for q in df.period])
# Unmapped values unchanged.
>>> df
period qtr
0 Mar 1
1 Jun 2
2 Sep 3
3 Dec 4
4 Q5 5
df = pd.DataFrame({'period': ['Q1', 'Q2', 'Q3', 'Q4', 'Q5'], 'qtr': [1, 2, 3, 4, 5]})
df = df.assign(period=df.period.map(quarter))
# Unmapped values get `NaN`.
>>> df
period qtr
0 Mar 1
1 Jun 2
2 Sep 3
3 Dec 4
4 NaN 5
Assign new columns to a DataFrame, returning a new object (a copy) with all the original columns in addition to the new ones.
.. versionadded:: 0.16.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With