0

When mapping pandas series s to dataframe x, I get a SettingWithCopy warning:

s.head()
84     2013-06-05 10:20:29
87     2013-06-05 15:27:56
469    2013-06-28 03:20:05
4670   2013-11-15 11:10:48
4673   2013-11-15 14:08:01
Name: timestamp, dtype: datetime64[ns]

y.head()
min max
0   2013-06-02 09:10:51 2013-06-02 10:27:44
1   2013-06-12 03:08:35 2013-06-12 03:08:35
2   2013-08-03 09:11:35 2021-01-26 23:05:17

x.head()
timestamp
4   2013-06-01 04:12:34
5   2013-06-01 04:19:08
6   2013-06-01 05:18:35
7   2013-06-01 06:00:19
8   2013-06-01 09:16:13

dfo = x.assign(a=1).merge(y.assign(a=1), on='a') 
s = df.loc[(dfo['min']<=dfo['timestamp']) & (dfo['max']>=dfo['timestamp']), 'timestamp']

x['count']=x['timestamp'].map(s.value_counts()).fillna(0).astype(int)

<ipython-input-205-91489a4af264>:1: 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

Since I am creating a new column based on a series map, why is the error thrown (what is the copied slice?) and what is the proper way to rewrite this with i.loc since I am not using an indexer?

iskandarblue
  • 7,208
  • 15
  • 60
  • 130
  • 1
    Can you please include the last assignment to `x`? I'd say it's probably a copy-type assigmnent, thus the warning. – Mephy Feb 17 '21 at 16:37
  • I included the last assignment to x – iskandarblue Feb 17 '21 at 16:39
  • What do you mean by copy-type assignemnet? – iskandarblue Feb 17 '21 at 16:42
  • Assigment I mean when you do `x = ...`. In this part, the right-hand side is likely to be an operation where pandas creates a slice instead of a proper DataFrame. If you add `.copy()` at the end of that line it's likely the warning goes away (though may not be the most efficient way of doing so). – Mephy Feb 17 '21 at 19:06
  • Does this answer your question? [How to deal with SettingWithCopyWarning in Pandas](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – Mephy Feb 17 '21 at 19:07

0 Answers0