Crash Code Part-7 (Find Longest Ride!)

Processing datasets with Pandas: Data cleaning, datetime manipulation, and grouping

BLZR

CodePython

Crash Code

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:

  1. Remove rows with missing pickup_datetime or dropoff_datetime.
  2. Determine the ride with the longest duration for each pickup month (formatted as YYYY-MM).
  3. Sort the resulting dataframe by the pickup month.

The provided dataframe includes four columns:

  • id: Unique trip identifier
  • vendor_id: Vendor identifier
  • pickup_datetime: Start time of the ride
  • dropoff_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):

idvendor_idpickup_datetimedropoff_datetime
id0123432016-06-06 06:06:202016-06-06 08:00:00
id0143442016-06-09 06:06:202016-06-09 06:07:20
id1323432016-07-06 03:06:10

Output:

pickup_monthid
2016-06id01234

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:

  1. Clean the Data: We start by utilizing .dropna() specifically targeting our two datetime columns to clear out incomplete records.
  2. 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.
  3. 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').
  4. 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.
  5. 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

Indexidpickup_monthduration
0id012342016-060 days 01:53:40
1id014342016-060 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_monthid
2016-06id01234

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/python3

import os
import pandas as pd

# Complete the 'longestRide' function below.

# The function is expected to return a dataframe.

def longestRide(df):
# Write your code here
df = df.dropna(subset=['pickup_datetime', 'dropoff_datetime']).copy()
df['pickup_datetime'] = pd.to_datetime(df['pickup_datetime'])
df['dropoff_datetime'] = pd.to_datetime(df['dropoff_datetime'])
df['duration'] = df['dropoff_datetime'] - df['pickup_datetime']
df['pickup_month'] = df['pickup_datetime'].dt.strftime('%Y-%m')
longest_idx = df.groupby('pickup_month')['duration'].idxmax()
result_df = df.loc[longest_idx, ['pickup_month', 'id']]
result_df = result_df.sort_values(by='pickup_month').reset_index(drop=True)
return result_df

if **name** == '**main**':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

df = pd.read_csv('/dev/stdin')

result = longestRide(df)

fptr.write(result.to_csv(index=False))

fptr.close()