Member-only story

How to deal with dates, dates with timezones and dates with particular formats with python and pandas (code snippets for different use cases)

When dealing with dates, I’ve sometimes problems because the source is not clean, or not all the rows have the same format. Additionally, dates can be simple (year-month-day) or really complicate like

Diego Carrasco G.
2 min readJan 26, 2023

When dealing with dates, I’ve sometimes problems because the source is not clean, or not all the rows have the same format. Additionally, dates can be simple (year-month-day) or truly complicate like a timestamp with timezone.

Here are some code snippets for several use cases.

# import pandas
import pandas as pd # df is a dataframe with a column 'column_with_date' with a date like this '19.01.2023 16:45:46 +01:00'
# convert date string to datetime with pd.to_datetime
pd.to_datetime(df['column_with_date']) # sometimes the format is a bit weird and pandas cannot recognize it. In this case we give the date format as argument.
# for this format 19.01.2023 16:45:46 +01:00 we can use:
pd.to_datetime(df['column_with_date'], format='%Y-%m-%d %H:%M:%S%z') # if your string has timezone, use utr=True
pd.to_datetime(df['column_with_date'], format='%Y-%m-%d %H:%M:%S%z', utc=True) # sometimes your columns are as objects (strings) and numbers (floats, ints) and to_datetime cannot process it.
# You can force the…

--

--

Diego Carrasco G.
Diego Carrasco G.

Written by Diego Carrasco G.

Hi, I'm an entrepreneur-turned-developer living in Germany. I enjoy learning, write, coffee and solving problems. I write mainly about technology.

No responses yet