Crash Code Part-7 (Find Longest Ride!)
Processing datasets with Pandas: Data cleaning, datetime manipulation, and grouping
670 Words [Mind Tax: 3:02m]
01 September 2026, 6:30:00 PM
Problem Description
Given a pandas dataframe containing a dataset of completed taxi rides, you need to process the dataframe to find the longest ride for each month.
Follow these specified steps:
- Remove rows with missing
pickup_datetimeordropoff_datetime. - Determine the ride with the longest duration for each pickup month (formatted as
YYYY-MM). - Sort the resulting dataframe by the pickup month.
The provided dataframe includes four columns:
id: Unique trip identifiervendor_id: Vendor identifierpickup_datetime: Start time of the ridedropoff_datetime: End time of the ride
Constraints
- 1 <= the number of rows in the dataframe <= 1000.
- It is guaranteed that the resulting dataframe consists of at least one row with no nulls.
Input
- Input is formatted as a CSV file. The first row has column names, and the remaining rows contain data.
Output
Return a dataframe containing only the pickup_month and the id of the longest ride for that month, sorted chronologically by month.
Examples
Example
Input (Conceptual Dataframe):
| id | vendor_id | pickup_datetime | dropoff_datetime |
|---|---|---|---|
| id01234 | 3 | 2016-06-06 06:06:20 | 2016-06-06 08:00:00 |
| id01434 | 4 | 2016-06-09 06:06:20 | 2016-06-09 06:07:20 |
| id13234 | 3 | 2016-07-06 03:06:10 |
Output:
| pickup_month | id |
|---|---|
| 2016-06 | id01234 |
Explanation:
The third row (id13234) is missing a pickup_datetime, so it is removed. For the month of June 2016 (2016-06), we have two valid rides:
- id01234: Duration is roughly 1 hour and 53 minutes.
- id01434: Duration is exactly 1 minute.
The longest ride in 2016-06 is id01234. We isolate the month and the ID, resulting in our final table.
The Concepts Under the Hood
Instead of iterating through rows with for loops (which is a pandas anti-pattern and highly inefficient), we can use vectorized operations and the split-apply-combine strategy utilizing pandas’ powerful groupby functionality.
Here is the step-by-step logic:
- Clean the Data: We start by utilizing
.dropna()specifically targeting our two datetime columns to clear out incomplete records. - Type Casting: Data loaded from a CSV is often parsed as raw strings. We convert the datetime columns into actual pandas datetime objects (
pd.to_datetime) so we can perform mathematical operations on them. - Calculate and Format: We subtract the pickup time from the drop-off time to get the duration. Concurrently, we extract the year and month into a new column using
.dt.strftime('%Y-%m'). - Group and Isolate: We group the data by our newly created
pickup_month. Instead of just getting the maximum duration, we use.idxmax()on the duration column. This handy method returns the index of the row containing the maximum value for each group. - Filter and Sort: Using those indices, we filter our dataframe down to just the winning rows, select only the required columns, and run a final
.sort_values()on the month.
Visualizing the Grouping and Max Index
Here is how the .idxmax() technique looks conceptually once we’ve calculated the durations for our example data.
Step 1: The Calculated Dataframe
| Index | id | pickup_month | duration |
|---|---|---|---|
| 0 | id01234 | 2016-06 | 0 days 01:53:40 |
| 1 | id01434 | 2016-06 | 0 days 00:01:00 |
Step 2: Grouping and finding idxmax
We group by pickup_month (‘2016-06’) and ask for the index of the max duration.
- For group ‘2016-06’, comparing durations:
01:53:40>00:01:00. - The maximum duration is at Index 0.
Step 3: Filtering with loc
We pass [0] into df.loc and ask for the ['pickup_month', 'id'] columns.
Final Output:
| pickup_month | id |
|---|---|
| 2016-06 | id01234 |
Solve
Here is the final code, combined with the required HackerRank/platform boilerplate to read a CSV from /dev/stdin and write to the output path environment variable.
| |