Search code examples
pythonpandasdataframepandas-settingwithcopy-warning

Python: change column month to separate quarter columns; SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame


I would like to add to my DF 4 new columns with boolean type representing quarters of the year. I have column with month numbers and would like to get this result:

month  Q1  Q2  Q3  Q4
    6   0   1   0   0
    7   0   0   1   0
    8   0   0   1   0
    9   0   0   1   0
   10   0   0   0   1
   11   0   0   0   1
   12   0   0   0   1
    1   1   0   0   0

At the moment I have tried this code:

print("Quarters")
quarters = {'Q1': [1, 2, 3], 'Q2': [4, 5, 6], 'Q3': [7, 8, 9], 'Q4': [10, 11, 12]}
for quarter, value in quarters.items():
    df_analysis[quarter] = (df_analysis["month"].isin(value))*1

Which works however I get following error: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame I have tried also:

df_analysis.loc[:, quarter] = (df_analysis.loc[:, "month"].isin(value))*1

However warning is still showing up. Could you please help how to write it correctly?


Solution

  • Do not loop, you should rather map and get_dummies:

    quarters = {'Q1': [1, 2, 3], 'Q2': [4, 5, 6], 'Q3': [7, 8, 9], 'Q4': [10, 11, 12]}
    # compute a better mapping format
    d = {k:v for v,l in quarters.items() for k in l}
    # {1: 'Q1', 2: 'Q1', 3: 'Q1', 4: 'Q2'...}
    
    df.join(pd.get_dummies(df['month'].map(d)))
    

    output:

       month  Q1  Q2  Q3  Q4
    0      6   0   1   0   0
    1      7   0   0   1   0
    2      8   0   0   1   0
    3      9   0   0   1   0
    4     10   0   0   0   1
    5     11   0   0   0   1
    6     12   0   0   0   1
    7      1   1   0   0   0