I have two columns in Pandas DataFrame, one containing a date as a string and one containing the hour of the day as an int.
I want to convert this into a datetime stamp.
I have managed this but feel it is slow.
The data is:
df = pd.DataFrame({'Date': {0: '01/12/2017',
1: '01/12/2017',
2: '01/12/2017',
3: '01/12/2017',
4: '01/12/2017'},
'Hour': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4},
'Rented Bike Count': {0: 254, 1: 204, 2: 173, 3: 107, 4: 78}})
where the dtypes of Date
is object
and Hour
is int64
and I am currently merging date and hour using:
# Generate the datetime column
df['datetime'] = pd.to_datetime(df.apply(lambda x: f"{x['Date']} {x['Hour']}:00:00", axis=1))
# Remove the old datetime columns
df = df.drop('Date', axis=1).drop('Hour', axis=1)
Is there a faster way of doing this?
CodePudding user response:
I'd do:
# is your data day-first or month-first?
df['datetime'] = pd.to_datetime(df['Date'], dayfirst=True) pd.to_timedelta(df['Hour'], unit='H')
df = df.drop(['Date','Hour'], axis=1)