Convert String to Datetime
February 3, 2018
Convert String to Datetime
Before we can perform time-series analysis, it helps greatly to convert the column to Python’s datatime object.
Load Libraries
import pandas as pd
Load Data
The data that we will use for this exercise is adapted from Bay Area Bike Share’s (now known as Ford GoBike) open data project to analyse and visualise ride information.
data = [{'Trip ID': 913465, 'Start Date': '9/1/2015 00:10:00',
'End Date': '9/1/2015 00:23:00',
'Start Station': 'San Francisco Caltrain 2 (330 Townsend)'},
{'Trip ID': 913466, 'Start Date': '9/1/2015 00:15:00',
'End Date': '9/1/2015 00:31:00',
'Start Station': 'Clay at Battery'}]
df = pd.DataFrame(data)
df.dtypes
End Date object
Start Date object
Start Station object
Trip ID int64
dtype: object
By default, Pandas does not parse the strings to datetime objects. We will need to do so ourselves.
Parsing String to Datetime
Pandas’ to_datetime
function transforms the input strings to datetime objects. You can find more information on how to create the format
parameter on Python’s strftime() and strptime() documentation page.
df['Start Date'] = pd.to_datetime(df['Start Date'],
format='%m/%d/%Y %H:%M:%S')
df['End Date'] = pd.to_datetime(df['End Date'],
format='%m/%d/%Y %H:%M:%S')
df.dtypes
End Date datetime64[ns]
Start Date datetime64[ns]
Start Station object
Trip ID int64
dtype: object
Having the Start Date
and End Date
columns in datetime
allows for far easier manipulation of the data.
df['Ride Time'] = df['End Date'] - df['Start Date']
df.loc[:, ['Start Date', 'End Date', 'Ride Time']].head()
Start Date | End Date | Ride Time | |
---|---|---|---|
0 | 2015-09-01 00:10:00 | 2015-09-01 00:23:00 | 00:13:00 |
1 | 2015-09-01 00:15:00 | 2015-09-01 00:31:00 | 00:16:00 |