-3

I add values to a dataframe entry by entry as followed:

refined_cme_quandl_list['typical_daily_volume']= np.nan
for index, row in refined_cme_quandl_list.iterrows():
    refined_cme_quandl_list['typical_daily_volume'][index] = typical_volume[row['Quandl_download_symbol']]

I still get what i want, but i get this warning:

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

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

Does it matter?

Parfait
  • 104,375
  • 17
  • 94
  • 125
Vacassal Alsk
  • 391
  • 1
  • 6
  • 19

1 Answers1

1

Yes, using boolean indexing directly to assign to slices is not recommended. Use df.loc instead:

refined_cme_quandl_list.loc[index, 'typical_daily_volume'] = \
                              typical_volume[row['Quandl_download_symbol']]

It is quite possible that future releases of pandas might disable this behaviour (direct indexing), so you don't want your code breaking in the future.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • Exactly. The behaviour could even vary depending on the release of `numpy` or `python` (or other `pandas` dependencies) that you're using. Because the documentation says about code that throws a `SettingWithCopyWarning`: "Outside of simple cases, it’s very hard to predict whether it will return a view or a copy (it depends on the memory layout of the array, about which pandas makes no guarantees)" – Xukrao Jul 26 '17 at 20:54