I’m a bit confused trying to combine pandas DataFrame columns with datetime.date format and datetime.time format.
DF looks like this:
VJNo VJIdx lnTime lnDate
0 32613 1 05:00:00 2023-04-18
1 32613 2 05:01:00 2023-04-18
2 32613 3 05:02:30 2023-04-18
3 32613 5 05:05:30 2023-04-18
4 32613 6 05:06:30 2023-04-18
5 32613 8 05:07:30 2023-04-18
6 32613 9 05:08:30 2023-04-18
7 32613 11 05:10:30 2023-04-18
I wanted to use pandas.Timestamp.combine(date, time), but apparently it doesn’t want to work for DataSeries…(?) Running:
import pandas as pd
# Defining the data
data = {'VJNo': [32613, 32613, 32613, 32613, 32613, 32613, 32613, 32613],
'VJIdx': [1, 2, 3, 5, 6, 8, 9, 11],
'lnTime': ['05:00:00', '05:01:00', '05:02:30', '05:05:30', '05:06:30', '05:07:30', '05:08:30', '05:10:30'],
'lnDate': ['2023-04-18', '2023-04-18', '2023-04-18', '2023-04-18', '2023-04-18', '2023-04-18', '2023-04-18', '2023-04-18']}
# Create pandas dataframe
df = pd.DataFrame(data)
df['tmp'] = pd.Timestamp.combine( df['lnDate'], df['lnTime'])
Returns error: combine() argument 1 must be datetime.date, not Series, while it is datetime.date, but series of it…
Unfortunately other solutions like found here also doesn’t work (probably due to changes in pandas):
df['tmp'] = df.apply(pd.Timestamp.combine, df['lnDate'], df['lnTime'])
or
df['tmp'] = df.apply(lambda x: pd.Timestamp.combine(x['lnDate'], x['lnTime']))
Am I doing something wrong?
Last resort would be probably to convert date and time to strings and then use pd.to_datetime on them, but… I think it’s not the right way.
>Solution :
You need to vectorize your function:
import numpy as np
f = np.vectorize(pd.Timestamp.combine)
# f = np.vectorize(lambda d,t: pd.Timestamp.combine(d, t))
dfP['out'] = f(dfP['lnDate'], dfP['lnTime'])
Output:
VJNo VJIdx lnTime lnDate out
0 32613 1 05:00:00 2023-04-18 2023-04-18 05:00:00
1 32613 2 05:01:00 2023-04-18 2023-04-18 05:01:00
2 32613 3 05:02:30 2023-04-18 2023-04-18 05:02:30
3 32613 5 05:05:30 2023-04-18 2023-04-18 05:05:30
4 32613 6 05:06:30 2023-04-18 2023-04-18 05:06:30
5 32613 8 05:07:30 2023-04-18 2023-04-18 05:07:30
6 32613 9 05:08:30 2023-04-18 2023-04-18 05:08:30
7 32613 11 05:10:30 2023-04-18 2023-04-18 05:10:30