1

I have a dateset as below.

                      dummy                
datetime
2015-10-25 06:00:00      1 
2015-04-05 20:00:00      1 
2015-11-24 00:00:00      1 
2015-08-18 08:00:00      1 
2015-10-21 12:00:00      1 

I want to change the datetime to the cloest predefined time point, say 00:00:00 and 12:00:00

                      dummy                
datetime
2015-10-25 00:00:00      1 
2015-04-05 12:00:00      1 
2015-11-24 00:00:00      1 
2015-08-18 00:00:00      1 
2015-10-21 12:00:00      1 
Usman
  • 1,983
  • 15
  • 28
Liu Yong
  • 515
  • 5
  • 16

1 Answers1

2

Here is possible use DatetimeIndex.floor:

df.index = df.index.floor('12H')
print (df)
                     dummy
datetime                  
2015-10-25 00:00:00      1
2015-04-05 12:00:00      1
2015-11-24 00:00:00      1
2015-08-18 00:00:00      1
2015-10-21 12:00:00      1
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252