I am trying to add hours 0 to 23 to each each date in a datetime field, could someone please help me in achieving this.
I searched online, but was not able to find the right answer
Attached image for the input and output expected, Thanks in advance.
I am trying to add hours 0 to 23 to each each date in a datetime field, could someone please help me in achieving this.
I searched online, but was not able to find the right answer
Attached image for the input and output expected, Thanks in advance.
Assuming you have these dates as the index of a pandas df, you would convert to datetime and use hour as frequency. Then you can strftime to the desired output format. Ex:
import pandas as pd
df = pd.DataFrame(index=pd.to_datetime(['01/11/2020', '02/11/2020', '03/11/2020'], dayfirst=True))
new_dti = df.asfreq('H').index
new_dti.strftime('%d/%m/%Y %H:%M')
Index(['01/11/2020 00:00', '01/11/2020 01:00', '01/11/2020 02:00',
'01/11/2020 03:00', '01/11/2020 04:00', '01/11/2020 05:00',
'01/11/2020 06:00', '01/11/2020 07:00', '01/11/2020 08:00',
...
'02/11/2020 15:00', '02/11/2020 16:00', '02/11/2020 17:00',
'02/11/2020 18:00', '02/11/2020 19:00', '02/11/2020 20:00',
'02/11/2020 21:00', '02/11/2020 22:00', '02/11/2020 23:00',
'03/11/2020 00:00'],
dtype='object')
This is fairly easy to achieve using a simple for loop:
from datetime import datetime, date
dates = [date(2020,11,1), date(2020,11,2)]
for dt in dates:
for hour in range(0, 24):
result.append(datetime(dt.year, dt.month, dt.day, hour, 0))
print(result)
>>>
[datetime.datetime(2020, 11, 1, 0, 0),
datetime.datetime(2020, 11, 1, 1, 0),
datetime.datetime(2020, 11, 1, 2, 0),
datetime.datetime(2020, 11, 1, 3, 0),
...
...
...
datetime.datetime(2020, 11, 2, 22, 0),
datetime.datetime(2020, 11, 2, 23, 0)]