A timedelta represents a span of time—the distance between two timestamps. A meeting’s start and end are timestamps; the meeting’s duration is a timedelta. Pandas uses these values for arithmetic, comparisons, and time-based analysis. section 10.0
Use the schedule illustration to distinguish timestamp points from timedelta spans. section 10.0
Measure and group taxi rides
Load pickup and drop-off columns as datetimes, then subtract them to create a trip_time column: df['trip_time'] = df['tpep_dropoff_datetime'] - df['tpep_pickup_datetime']. Compare durations with values such as '1 minute' and '10 hours', count matching rides, calculate percentages, and average fares. For broader analysis, convert '0 seconds', '10 minutes', '1 hour', and '100 hours' with pd.to_timedelta, then pass those bins to pd.cut to label rides short, medium, or long before grouping passenger counts. section 10.1 section 10.2
Useful timedelta operations include:
- Subtract two timestamps to obtain a timedelta; add or subtract a timedelta to move a timestamp. section 10.0
- Convert strings such as '2 days 3:20:10' with pd.to_timedelta and inspect their day, hour, minute, and smaller components. section 10.0
- Compare a timedelta Series with readable duration strings and use the result to filter rows. section 10.2
