Sitemap
Python Pandemonium

A place to read and write about all things Python. If you want to become a writer for this publication then let me know.

Member-only story

Managing the Date Format With Pandas

--

Photo by Carlos Muza on Unsplash

Working with dates holds very unwelcome surprises due to the different formats they can take. In this short article, we will see how to choose the format we prefer, thanks to the Python pandas library.

European or American?

Here we are grappling with the age-old question: European or American format? When we write 02/03/2010 do we want to indicate March 2nd or February 3rd?

Unlike what happens in Excel, the format of dates in pandas does not depend on the geolocation settings of our device, but it is something that we can specify from time to time.

We work with pandas

Let’s imagine we have the following data set:

Image by Author

As you have noticed, the fourth column contains dates that can be in both American and European format. How do we make sure our script “interprets” them as we would?
Simple! We just need to convert the “Date” column into the Python “datetime” module format and specify whether the first number contained in the cells indicates the day or the month. Let’s take a look at the actual command:

--

--

Python Pandemonium
Python Pandemonium

Published in Python Pandemonium

A place to read and write about all things Python. If you want to become a writer for this publication then let me know.

Responses (1)