Skip to the content.

Datetime operations in Python

Some of the key codes for date time manipulation using datetime library of pandas. Useful for time series problems and doing feature engineering based on dates.


from datetime import datetime
from dateutil.parser import parse
import pandas as pd

Importing datetime by explicitly mentioning the date

df = pd.read_csv("filename.csv", parse_dates = ['date_column'])

Converting an unknown format into datetime


In case where ‘year’, ‘month’ and ‘day’ are explicitely tagged

>>df = pd.DataFrame({'year': [2015, 2016],
                       'month': [2, 3],
                       'day': [4, 5]})
>>> pd.to_datetime(df)
0   2015-02-04
1   2016-03-05
dtype: datetime64[ns]

Convert a string date format to datetime

data['dt'] = pd.to_datetime(data['date'])

When the format is different and needs to be specified explicitly

strftime and strptime

Mapping of strptime

date = '2011-01-03'
datetime.strptime(date, '%Y-%m-%d')

Convert string values in dates to datetime format - Does not work on already datetime converted column

dates = data['date'].apply(lambda x: datetime.strptime(x, '%Y-%m-%d'))

Splitting into date, day, month, year, weekofyear

data[‘date’] is the column with dates as string

data['date1'] = pd.to_datetime(data['date'])

data['onlydate'] = data['date1']
data['onlyday'] = data['date1']
data['onlymonth'] = data['date1'].dt.month
data['onlyyear'] = data['date1'].dt.year
data['onlyweek'] = data['date1'].dt.dayofweek
Written on January 23, 2018