The Problem
Real data moment
Real NYC Taxi and Limousine Commission trip records, January 2025 — 3,475,226 genuine yellow-cab trips, each with a pickup timestamp and a trip distance. A completely natural real question: does average trip distance change by hour of day?
import pandas as pd
df = pd.read_parquet("yellow_tripdata_2025-01.parquet")
df["hour"] = df["tpep_pickup_datetime"].dt.hour
by_hour = df.groupby("hour")["trip_distance"].mean()
print(by_hour.round(2).head(6))
hour
0 5.34
1 4.57
2 3.16
3 3.37
4 9.16
5 39.51
dtype: float64
Hour 5's average of nearly 40 miles should stop you cold — that's not a real average taxi trip distance, that's a data-quality problem waiting for Phase 04. For the rest of this lesson, real trips are filtered to a sane 0-100 mile range first (removing 162 rows, out of 3.47 million, with obviously corrupted GPS-derived distances):
clean = df[df["trip_distance"].between(0, 100)]
by_hour = clean.groupby("hour")["trip_distance"].mean()
print(by_hour.round(2).head(6))
hour
0 3.62
1 3.20
2 3.07
3 3.37
4 4.60
5 5.59
dtype: float64
Real, believable numbers — early morning hours (4-6 AM) run slightly longer on average, likely airport and long-distance runs when traffic is light, before settling toward 2.5-3.5 miles for the rest of the day.
The gotcha
result = clean.groupby("hour")["trip_distance"].mean()
print(type(result))
print(result.index.name)
<class 'pandas.core.series.Series'>
hour
By default, the grouping column (hour) becomes the index of the result, not a regular column — this is as_index=True, pandas' default. That's often exactly what you want, until the very next operation assumes hour is a normal column:
result.sort_values("hour")
TypeError: Series.sort_values() takes 1 positional argument but 2 were given
A real TypeError, and it looks like nothing to do with groupby at all — sort_values just doesn't accept a column-name argument on a Series, because there's no such column; hour is the index. The fix is as_index=False, requested up front:
result2 = clean.groupby("hour", as_index=False)["trip_distance"].mean()
print(type(result2))
print(result2.columns.tolist())
result2.sort_values("hour")
<class 'pandas.core.frame.DataFrame'>
['hour', 'trip_distance']
as_index=False makes the result a DataFrame with hour as a genuine column — and now sort_values("hour") works exactly the way it looked like it should have the first time.
The bug is never in sort_values — it's in not knowing which shape groupby just handed you
Series vs. DataFrame, index vs. column — these aren't cosmetic details. Whatever code runs immediately after a groupby needs to match the actual shape the groupby produced, and as_index's default silently picks one shape unless you ask for the other.
clean.groupby("hour")["trip_distance"].mean().reset_index() is run. What does the result look like?
.reset_index() takes whatever is currently the index (hour, from the default as_index=True grouping) and turns it back into a regular column — producing a 2-column DataFrame identical in shape to what as_index=False would have given directly. Both are valid, equally common ways to reach the same result; as_index=False does it in the groupby call itself, reset_index() does it as a separate step afterward.