The 6 AI Engineering Patterns, come build with Greg live:Β Starts Jan 6th, 2025
Leverage
Pandas functions

Pandas To Datetime – String to Date – pd.to\_datetime()

Convert strings to datetime in pandas

One of the Top 10 Pandas functions you must know is Pandas To Datetime. It a need-to-have in your data analysis toolkit. The wonderful thing about to_datetime() is it’s flexibility to read 95% of any dates you’ll throw at it.

Interested in my Top 10 Pandas Functions? Get em here.

Pandas To Datetime (.to_datetime()) will convert your string representation of a date to an actual date format. This is extremely important when utilizing all of the Pandas Date functionality like resample.

1. pd.to_datetime(your_date_data, format="Your_datetime_format")

If you walk away with anything from this post, make sure it’s an understanding of how to use format codes when converting dates. Check out the code sample below.

Pseudo code: Given format, convert a string into a datetime object.

Pandas To Datetime

To DateTime Parameters

.to_datetime() has a ton of parameters and they are all are important to understand. After you become familiar with them, you’ll need to understand date format codes below.

  • arg: This is the β€˜thing’ that you want to convert to a datetime object. Pandas gives you a ton of flexibility; you can pass a int, float, string, datetime, list, tuple, Series, DataFrame, or dict. That’s a ton of input options!
  • format (Default=None): *Very Important* The format parameter will instruct Pandas how to interpret your strings when converting them to DateTime objects. The format must use the format codes below. See examples below.
  • origin (Default=’unix’): An origin is simply a reference date. Where do you want to have your universe of timestamps to start? By default is is set to unix which is 1970-01-01. 'julian' is January 1, 4713 BC. You can even set your own origin.
  • unit: Say you pass an int as your arg (like 20203939), with unit, you’ll be able specify what unit your int is is away from the origin. In the example here, if we set unit=’s’, this means pandas will interpret 20203939 as 20,203,939 seconds away from the origin. Available units are [D,s,ms,us,ns]
  • dayfirst: This parameter helps pandas understand if your β€˜day’ is first in your format (ex: 01/02/2020 > 2020-02-01). I suggest playing with other parameters first before you try this one.
  • yearfirst: Same as the dayfirst parameter above. This will help pandas parse your dates if your year is first. Try the format code options first.
  • utc (Default=None): If you want to convert your DateTime objects to timezone-aware (meaning each datetime object also has a timezone) and you want that timezone to be UTC then set utc=True:

DateTime Format Codes

One extremely important concept to understand is DateTime format codes. This is how you instruct Pandas what format your DateTime string is in. It’s magic every time you see it work. In fact, I look forward to gross strings with dates in them just to parse. See documentation.

Format CodeDescriptionExamples
%aWeekday, abbreviatedMon, Tues, Sat
%AWeekday, full nameMonday, Tuesday, Saturday
%wWeekday, decimal. 0=Sunday1, 2, 6
%dDay of month, zero-padded01, 02, 21
%bMonth, abbreviatedJan, Feb, Sep
%BMonth, full nameJanuary, February, September
%mMonth number, zero-padded01, 02, 09
%yYear, without century, zero-padded02, 95, 99
%YYear, with century1990, 2020
%HHour (24 hour), zero padded01, 22
%IHour (12 hour) zero padded01, 12
%pAM or PMAM, PM
%MMinute, zero-padded01, 02, 43
%SSecond, zero padded01, 32, 59
%fMicrosecond, zero-padded000001, 000342, 999999
%zUTC offset Β±HHMM[SS[.ffffff]]+0000, -1030, -3423.234
%ZTime zone nameITC, EST, CST
%jDay of year, zero-padded001, 365, 023
%UWeek # of year, zero-padded. Sunday first day of week00, 01, 51
%WWeek # of year, zero-padded. Monday first day of week00, 02, 51
%cAppropriate date and timeMonday Feb 01 21:30:00 1990
%xAppropriate Date02/01/90
%XAppropriate Time21:22:00
%%Literal '%' – Use this when you have a % sign in your format.%

Let’s run through each iteration of the above parameters

Link to code

Official Documentation

On this page