• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Convert String to Datetime in pandas with pd.to_datetime()

Convert String to Datetime in pandas with pd.to_datetime()

October 17, 2022 Leave a Comment

To convert a column of strings representing dates to a column of datetimes in pandas, you can use the pd.to_datetime() function.

import pandas as pd

df = pd.DataFrame({
    "date": ["2021-09-30", "2021-12-31", "2022-03-31", "2022-06-30", "2022-09-30", "2022-12-31"]
})

df["date"] = pd.to_datetime(df["date"])

print(df["date"])

#Output:
0   2021-09-30
1   2021-12-31
2   2022-03-31
3   2022-06-30
4   2022-09-30
5   2022-12-31
Name: date, dtype: datetime64[ns]

When working with different types of data, the ability to easily be able to convert variables into variables of other types is valuable.

One such case is converting strings to datetimes in a pandas DataFrame.

To convert a column of strings representing dates to a column of datetimes in pandas, you can use the pd.to_datetime() function.

pd.to_datetime() convert a scalar, array-like, Series or DataFrame/dict-like to a pandas datetime object.

For valid string formats, pd.to_datetime() will convert a “date” string into a datetime. For example, if my strings look like “YYYY-MM-DD”, then pd.to_datetime() will convert this into a datetime formatted like “YYYY-MM-DD”.

Below is a simple example showing you how to use pd.datetime() in Python to convert a column of strings to a datetime column in pandas.

import pandas as pd

df = pd.DataFrame({
    "date": ["2021-09-30", "2021-12-31", "2022-03-31", "2022-06-30", "2022-09-30", "2022-12-31"]
})

df["date"] = pd.to_datetime(df["date"])

print(df["date"])

#Output:
0   2021-09-30
1   2021-12-31
2   2022-03-31
3   2022-06-30
4   2022-09-30
5   2022-12-31
Name: date, dtype: datetime64[ns]

Hopefully this article has been useful for you to learn how to pd.to_datetime() to convert a column of strings to datetime in pandas.

Other Articles You'll Also Like:

  • 1.  How to Remove All Punctuation from String in Python
  • 2.  Remove Trailing Zeros from String with rstrip() in Python
  • 3.  Find All Pythagorean Triples in a Range using Python
  • 4.  Break Out of Function in Python with return Statement
  • 5.  Open Multiple Files Using with open in Python
  • 6.  Sort a List of Strings Using Python
  • 7.  Reverse String with Recursion in Python
  • 8.  Convert pandas Series to Dictionary in Python
  • 9.  How to Split a String in Half Using Python
  • 10.  Python getsizeof() Function – Get Size of Object

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x