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

Merging Date and Hour in Pandas

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.

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

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?

>Solution :

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