0

I have a datarframe with a DatetimeIndex, and want to add a column with the day to my df. My code df.loc[:,'Day'] = df.index.date gives the error

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 

How do I do this with the index?

EDIT Below is my index

df.index 
>> DatetimeIndex(['2022-05-23 03:00:02+02:00', '2022-05-23 03:00:13+02:00',
               '2022-05-23 03:00:23+02:00', '2022-05-23 03:00:33+02:00',
               '2022-05-23 03:00:43+02:00', '2022-05-23 03:00:53+02:00',
               '2022-05-23 03:01:03+02:00', '2022-05-23 03:01:13+02:00',
               '2022-05-23 03:01:23+02:00', '2022-05-23 03:01:33+02:00',
               ...
               '2022-05-24 21:58:29+02:00', '2022-05-24 21:58:39+02:00',
               '2022-05-24 21:58:49+02:00', '2022-05-24 21:58:59+02:00',
               '2022-05-24 21:59:09+02:00', '2022-05-24 21:59:19+02:00',
               '2022-05-24 21:59:29+02:00', '2022-05-24 21:59:39+02:00',
               '2022-05-24 21:59:49+02:00', '2022-05-24 21:59:59+02:00'],
              dtype='datetime64[ns, Europe/Stockholm]', name='Timestamp', length=13758, freq=None)
Elsa
  • 11
  • 2
  • Can you show your sample data or just index data ? – Deven Ramani Jun 17 '22 at 06:51
  • 1
    Does this answer your question? [How to deal with SettingWithCopyWarning in Pandas](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – Shaido Jun 17 '22 at 06:52
  • Hi, I did look at the example you're linking, but can't seem to get it to work without showing any warnings – Elsa Jun 17 '22 at 13:55
  • @DevenRamani Added a copy of my dataframe index – Elsa Jun 17 '22 at 14:00

0 Answers0