Reputation: 13
I have a data frame that has the month and date for 2015. I calculate the Year to Date value into a list. I assign this list to a new column in the data frame but get a warning SettingWithCopyWarning. How do I get around it and some explanation why is this happening. Thanking you all in advance.
print(dfabovemax.head())
print(dfabovemax.tail())
MaxTemp Data_Value
Mon-Date
01-02 114 113
01-10 142 126
04-10 213 203
04-15 246 228
05-03 203 195
MaxTemp Data_Value
Mon-Date
01-02 114 113
01-10 142 126
04-10 213 203
04-15 246 228
05-03 203 195
fmt = '%Y-%m-%d'
ytodt2 = []
for i in dfabovemax.index:
s='2005-{}'.format(i)
dt = datetime.datetime.strptime(s, fmt)
tt = dt.timetuple()
ytodt2.append(tt.tm_yday)
dfabovemax['YtoDt'] = list(ytodt2)
And I get a warning
/opt/conda/lib/python3.6/site-packages/ipykernel/main.py:14: 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/indexing.html#indexing-view-versus-copy
print(dfabovemax.head())
print(dfabovemax.tail())
MaxTemp Data_Value YtoDt
Mon-Date
01-02 114 113 2
01-10 142 126 10
04-10 213 203 100
04-15 246 228 105
05-03 203 195 123
MaxTemp Data_Value YtoDt
Mon-Date
12-25 140 135 359
12-26 152 130 360
12-27 138 118 361
12-28 134 124 362
12-30 134 128 364
Upvotes: 1
Views: 300
Reputation: 3184
You get this warning often when trying to set values on a copy of a dataframe. The only place in your code where this could happen is
dfabovemax['YtoDt'] = list(ytodt2)
This means that in all likelihood, dfabovemax is the result of another dataframe. In python, the dfabovemax is still referencing the original dataframe that it was made from. In order to rectify this use copy() when you made dfabovemax.
Upvotes: 0