Reading date and time data in Pandas

Working with Dates and Times in Python

Max Shron

Data Scientist and Author

A simple Pandas example

# Load Pandas
import pandas as pd

# Import W20529's rides in Q4 2017 rides = pd.read_csv('capital-onebike.csv')
Working with Dates and Times in Python

A simple Pandas example

# See our data
print(rides.head(3))

           Start date            End date                  Start station  \
0 2017-10-01 15:23:25 2017-10-01 15:26:26           Glebe Rd & 11th St N
1 2017-10-01 15:42:57 2017-10-01 17:49:59  George Mason Dr & Wilson Blvd
2 2017-10-02 06:37:10 2017-10-02 06:42:53  George Mason Dr & Wilson Blvd

                            End station Bike number Member type
0         George Mason Dr & Wilson Blvd      W20529      Member
1         George Mason Dr & Wilson Blvd      W20529      Casual
2  Ballston Metro / N Stuart & 9th St N      W20529      Member
Working with Dates and Times in Python

A simple Pandas example

rides['Start date']
0     2017-10-01 15:23:25
1     2017-10-01 15:42:57
...
Name: Start date, Length: 290, dtype: object
rides.iloc[2]
Start date                               2017-10-02 06:37:10
End date                                 2017-10-02 06:42:53
...
Name: 1, dtype: object
Working with Dates and Times in Python

Loading datetimes with parse_dates

# Import W20529's rides in Q4 2017
rides = pd.read_csv('capital-onebike.csv',
                    parse_dates = ['Start date', 'End date'])

# Or: rides['Start date'] = pd.to_datetime(rides['Start date'], format = "%Y-%m-%d %H:%M:%S")
Working with Dates and Times in Python

Loading datetimes with parse_dates

# Select Start date for row 2
rides['Start date'].iloc[2]
Timestamp('2017-10-02 06:37:10')
Working with Dates and Times in Python

Timezone-aware arithmetic

# Create a duration column
rides['Duration'] = rides['End date'] - rides['Start date']

# Print the first 5 rows print(rides['Duration'].head(5))
0 0 days 00:03:01 
1 0 days 02:07:02 
2 0 days 00:05:43 
3 0 days 00:21:18 
4 0 days 00:21:17 
Name: Duration, dtype: timedelta64[ns]
Working with Dates and Times in Python

Loading datetimes with parse_dates

rides['Duration']\
  .dt.total_seconds()\
  .head(5)
0     181.0
1    7622.0
2     343.0
3    1278.0
4    1277.0
Name: Duration, dtype: float64
Working with Dates and Times in Python

Reading date and time data in Pandas

Working with Dates and Times in Python

Preparing Video For Download...