Home > Backend Development > Python Tutorial > How to efficiently combine date and time columns in Pandas with pd.to_datetime()?

How to efficiently combine date and time columns in Pandas with pd.to_datetime()?

DDD
Release: 2024-11-16 14:01:03
Original
403 people have browsed it

How to efficiently combine date and time columns in Pandas with pd.to_datetime()?

Combining Date and Time Columns in Pandas with pd.to_datetime()

When working with date and time data in Pandas, it's often necessary to combine separate columns into a single datetime object. One way to do this is by using the pd.to_datetime() function.

Problem Statement

Consider a dataframe with 'Date' and 'Time' columns, as shown below:

Date      Time
01-06-2013  23:00:00
02-06-2013  01:00:00
02-06-2013  21:00:00
02-06-2013  22:00:00
02-06-2013  23:00:00
03-06-2013  01:00:00
03-06-2013  21:00:00
03-06-2013  22:00:00
03-06-2013  23:00:00
04-06-2013  01:00:00
Copy after login

Our goal is to combine these two columns into a single 'Date & Time' column.

Solution Using String Concatenation

One approach is to concatenate the 'Date' and 'Time' columns as strings and then convert the resulting string to a datetime object using pd.to_datetime():

result = pd.to_datetime(df['Date'] + ' ' + df['Time'])
Copy after login

This converts the concatenated string to a series of datetime objects.

Solution Using format Parameter

Alternatively, you can use the format parameter of pd.to_datetime() to specify the exact format of the combined string:

result = pd.to_datetime(df['Date'] + df['Time'], format='%m-%d-%Y %H:%M:%S')
Copy after login

This approach is faster than the previous one, especially when dealing with large datasets.

Performance Comparison

Using the %%timeit magic command, we can compare the performance of both approaches:

df = pd.concat([df for _ in range(1000000)]).reset_index(drop=True)

%timeit pd.to_datetime(df['Date'] + ' ' + df['Time'])

%timeit pd.to_datetime(df['Date'] + df['Time'], format='%m-%d-%Y %H:%M:%S')
Copy after login

The second approach with the format parameter is significantly faster for large datasets.

The above is the detailed content of How to efficiently combine date and time columns in Pandas with pd.to_datetime()?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template