Summarizing datetime data in Pandas

Working with Dates and Times in Python

Max Shron

Data Scientist and Author

Summarizing data in Pandas

# Average time out of the dock
rides['Duration'].mean()
Timedelta('0 days 00:19:38.931034482')
# Total time out of the dock
rides['Duration'].sum()
Timedelta('3 days 22:58:10')
Working with Dates and Times in Python

Summarizing data in Pandas

# Percent of time out of the dock
rides['Duration'].sum() / timedelta(days=91)
0.04348417785917786
Working with Dates and Times in Python

Summarizing data in Pandas

# Count how many time the bike started at each station
rides['Member type'].value_counts()
Member 236
Casual  54
Name: Member type, dtype: int64
# Percent of rides by member
rides['Member type'].value_counts() / len(rides)
Member 0.814 
Casual 0.186 
Name: Member type, dtype: float64
Working with Dates and Times in Python

Summarizing datetime in Pandas

# Add duration (in seconds) column
rides['Duration seconds'] = rides['Duration'].dt.total_seconds()

# Average duration per member type rides.groupby('Member type')['Duration seconds'].mean()
Member type 
Casual 1994.667 
Member 992.280 
Name: Duration seconds, dtype: float64
Working with Dates and Times in Python

Summarizing datetime in Pandas

# Average duration by month
rides.resample('M', on = 'Start date')['Duration seconds'].mean()
Start date 
2017-10-31 1886.454 
2017-11-30 854.175 
2017-12-31 635.101 
Freq: M, Name: Duration seconds, dtype: float64
Working with Dates and Times in Python

Summarizing datetime in Pandas

# Size per group
rides.groupby('Member type').size()
Member type
Casual     54
Member    236
dtype: int64
# First ride per group
rides.groupby('Member type').first()
            Duration     ...     
Member type              ...
Casual      02:07:02     ...        
Member      00:03:01     ...
Working with Dates and Times in Python

Summarizing datetime in Pandas

rides\
  .resample('M', on = 'Start date')\
  ['Duration seconds']\
  .mean()\
  .plot()

Working with Dates and Times in Python

Summarizing datetime in Pandas

rides\
  .resample('D', on = 'Start date')\
  ['Duration seconds']\
  .mean()\
  .plot()

Working with Dates and Times in Python

Summarizing datetime data in Pandas

Working with Dates and Times in Python

Preparing Video For Download...