0

I have the following column (within a broader Dataframe):

>>> d['date']
date 
2017-04-06
2017-04-05
2017-03-28
2017-04-06

I want to turn these dates into a string like: March 2016, Week 12. I am doing the following (which works fine):

d['date'] = d['date'].apply(lambda x: x.strftime('%B %Y, Week %W'))

I have the result I want but, unfortunately I get the following warning:

file.py:53: 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

I have tried to check other issues in SO and read pandas documentation but I am not too sure how to handle it as my various attempts have failed yet (e.g. using .loc()). What is proper way to do what I want to do?

Thanks!

jseiller
  • 222
  • 2
  • 16
  • What is your code before `d['date'] = d['date'].apply(lambda x: x.strftime('%B %Y, Week %W'))`? Because it seems there can be problem – jezrael Apr 06 '17 at 05:14
  • 1
    Also you can check [docs](http://pandas.pydata.org/pandas-docs/stable/indexing.html#returning-a-view-versus-a-copy) and [this tutorial (find header SettingWithCopy)](http://tomaugspurger.github.io/modern-1.html) (from [modern pandas](http://pandas.pydata.org/pandas-docs/stable/tutorials.html#modern-pandas), first article) – jezrael Apr 06 '17 at 05:21

3 Answers3

3

Better is use strftime:

d['date'] = d['date'].dt.strftime('%B %Y, Week %W')
print (d)
                  date
0  April 2017, Week 14
1  April 2017, Week 14
2  March 2017, Week 13
3  April 2017, Week 14
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Thanks for the tip, I have update my code accordingly. It does not solve the SettingWithCopyWarning issue though, I'll dig into the tutorial you linked - thanks for the support. – jseiller Apr 10 '17 at 04:16
3

I had a similar warning before and tried copying the data frame which fixed it apart from trying to use the .loc[row_indexer, column_indexer] = value.

Try this:

new_d = d.copy()

"In Pandas, indexing a DataFrame returns a reference to the initial DataFrame. Thus, changing the subset will change the initial DataFrame. Thus, you'd want to use the copy if you want to make sure the initial DataFrame shouldn't change." Quoted answer from cgold why should I make a copy of a data frame in pandas

Community
  • 1
  • 1
nrmb
  • 460
  • 1
  • 6
  • 17
2

I found what was the problem in my code:

My DataFrame d was derived from another DataFrame called data using:

d = data[((data['date'].dt.week == current_date.week)

Before manipulating d I was manipulating another DataFrame, months, also derived from data in a similar way and set up a index for months:

months = months.set_index('date').resample('M').sum()

Because these were references to data and not hard copies, the indexation was also in place for d which caused the Warning as I was manipulating an index in a dodgy way. Making hard copies of data solved my problem:

months = data.copy()
d = data.copy()

Thanks for your help jezrael, your tutorials sort of led to that solution. Thanks nrmb, your reply was actually the right answer (which I struggled to get initially).

jseiller
  • 222
  • 2
  • 16