Return a pandas boolean mask of rows where end_date is on or after start_date
Quick Overview
Write a pandas function that returns a boolean mask marking bookings whose end date falls on or after the start date. It tests vectorized comparisons on datetime columns, index alignment, the dtype and name of the result, and how missing dates behave.
Return a pandas boolean mask of rows where end_date is on or after start_date
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `def valid_date_order_mask(df: pd.DataFrame) -> pd.Series`. The input has columns `['booking_id', 'start_date', 'end_date']` with dates already parsed as `datetime64[ns]`. Return a boolean mask (not the filtered frame) that flags the valid intervals.
The mask must meet these requirements:
- `True` exactly where `end_date >= start_date` (equal dates are valid).
- dtype `bool`, aligned to `df`'s index.
- The Series has no name (`name is None`).
Example:
```text
df:
booking_id start_date end_date
0 b1 2026-01-01 2026-01-05
1 b2 2026-02-10 2026-02-08
2 b3 2026-03-01 2026-03-01
Output (name=None):
0 True
1 False
2 True
dtype: bool
```
Booking `b2` ends two days before it starts, so it is the only invalid row. Booking `b3` starts and ends on the same day, which counts as valid.
```hint Name of the result
Compare whole columns at once, then check what name pandas gives the result of an operation between two Series with different names.
```
### Constraints & Assumptions
- `start_date` and `end_date` are both `datetime64[ns]`.
- The index can hold any labels, and the mask must carry the same index as `df`.
### What a Strong Answer Covers
- A vectorized comparison of the two columns that treats equal dates as valid.
- Why the result has dtype `bool` and the same index as `df`.
- How the result ends up unnamed, and how to guarantee it.
- What happens to rows where either date is missing.
### Follow-up Questions
- How would you handle `NaT` values in either date column?
- Why prefer a vectorized comparison over `df.apply`?
- What happens if one date column is timezone-aware and the other is naive, and how would you fix it?
Overview: Write a pandas function that returns a boolean mask marking bookings whose end date falls on or after the start date. It tests vectorized comparisons on datetime columns, index alignment, the dtype and name of the result, and how missing dates behave.
Implement def valid_date_order_mask(df: pd.DataFrame) -> pd.Series. The input has columns ['booking_id', 'start_date', 'end_date'] with dates already parsed as datetime64[ns]. Return a boolean mask (not the filtered frame) that flags the valid intervals.
The mask must meet these requirements:
True
exactly where
end_date >= start_date
(equal dates are valid).