Home > Backend Development > Python Tutorial > How to Convert a String Column to Datetime in a Pandas DataFrame?

How to Convert a String Column to Datetime in a Pandas DataFrame?

Patricia Arquette
Release: 2024-12-10 01:01:10
Original
224 people have browsed it

How to Convert a String Column to Datetime in a Pandas DataFrame?

Converting DataFrame Column from String to Datetime

Problem:

How to convert DataFrame Convert a string column in (format dd/mm/yyyy) to datetime data type?

Answer:

The simplest way to convert is to use to_datetime:

df['col'] = pd.to_datetime(df['col'])
Copy after login

It also provides a dayfirst parameter for handling Europe time (but this is not strict).

The actual application is as follows:

In [11]: pd.to_datetime(pd.Series(['05/23/2005']))
Out[11]:
0   2005-05-23 00:00:00
dtype: datetime64[ns]
Copy after login

In addition, you can also specify a specific format:

In [12]: pd.to_datetime(pd.Series(['05/23/2005']), format="%m/%d/%Y")
Out[12]:
0   2005-05-23
dtype: datetime64[ns]
Copy after login

The above is the detailed content of How to Convert a String Column to Datetime in a Pandas DataFrame?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template