Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combine pandas df [datetime] time and date columns into one datetime column

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):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading