Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A value is trying to be set on a copy of a slice from a DataFrame

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)
like image 311
obabs Avatar asked Apr 29 '16 02:04

obabs


People also ask

How do you suppress a value is trying to be set on a copy of a slice from a DataFrame?

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.

How do you deal with SettingWithCopyWarning?

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.


1 Answers

"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

like image 55
Alexander Avatar answered Sep 27 '22 19:09

Alexander