-1

I have a few data frames that i am resampling to match each other. I would like to set the timestamps (index) for all the data to be the first days of the month of the dsy the measurements were taken. I cannot find anywhere how to do it, the closest I got was with the resample(period=...), but it leaves me without the day. The code I tried

df['value'].resample('M',kind = 'period').sum() 

enter image description here

It comes like like this whereas I would like the timestamp to have the form of 2018-09-01.

1 Answers1

2

This line is all what you need:

df.index = pd.to_datetime(df.index).strftime('%Y-%m-%d')

# Output
#             value
# 2018-09-01     11
# 2018-10-01     12

It transforms your index column to a datetime type column. The first day of the month is automatically inserted. For more details, see the docs.

scandav
  • 749
  • 1
  • 7
  • 21
  • I still get the time stamp as the last day of the month.. I treid using format = '%01/%m/%Y', but doesnt work either.. – user13696679 Oct 14 '21 at 16:15
  • Strange, it works for me. I modified the answer, forcing the date format. Could you please try again? – scandav Oct 14 '21 at 16:22
  • still all of them either -30th day or 31st... is there a way to force the number of the day to be "1"? – user13696679 Oct 14 '21 at 16:32
  • I am not sure why you get the last day of the month as default. If you want to force it, just use `df.index = df.index.strftime('%Y-%m') + '-01'` – scandav Oct 14 '21 at 16:36