`parse_dates` tells `pd.read_csv` which CSV columns should become pandas datetime values during import, rather than remaining text-like objects. Pass column names or integer column positions. Parsing at the boundary keeps date handling close to the file-loading step. section 10.0
Load taxi timestamps ready for duration analysis
A taxi file has pickup and dropoff timestamps alongside numeric trip fields. Select the needed columns and name both timestamp columns in `parse_dates`; checking `df.dtypes` should then show `datetime64[ns]` for them. Subtracting dropoff from pickup can immediately produce a `trip_time` timedelta column for questions such as which rides lasted under one minute. `df = pd.read_csv(filename, usecols=['tpep_pickup_datetime', 'tpep_dropoff_datetime', 'trip_distance', 'passenger_count', 'total_amount'], parse_dates=['tpep_pickup_datetime', 'tpep_dropoff_datetime'])` `df['trip_time'] = df['tpep_dropoff_datetime'] - df['tpep_pickup_datetime']` section 10.2
Use `parse_dates` deliberately when loading CSV data:
- Include every date-bearing column needed for downstream `.dt` access, timestamp subtraction, grouping, or filtering. section 10.2 section 11.10
- When using `usecols`, also include each date column named in `parse_dates`; both lists must retain the timestamp fields. section 10.2
- Use `dayfirst` when input dates put the day before the month, and rely on straightforward parsing when dates use an unambiguous year-month-day layout. section 10.0 section 10.2
- For unusual serialized timestamps, supply the matching format when reading the file back so the datetime column is parsed appropriately. section 10.5 section 10.6
filename = '../data/nyc_taxi_2019-07.csv'
df = (
pd
.read_csv(filename,
usecols=['tpep_pickup_datetime',
'tpep_dropoff_datetime',
'trip_distance',
'passenger_count',
'total_amount'],
parse_dates=['tpep_pickup_datetime',
'tpep_dropoff_datetime'])
)
This import keeps only the analytical fields needed for a taxi-duration task while parsing pickup and dropoff as datetimes in the same call. section 10.2
Datetime parsing unlocks date-part extraction through `.dt`. Across multiple imported files, a parsed pickup timestamp can supply year, month, and day columns for filtering, grouping, and plotting after concatenation. If timestamps are left as objects, these operations do not begin from a datetime column. section 11.10 section 11.18
