Attach the latest quote price to each trade with pandas merge_asof
Quick Overview
Attach to each trade the most recent quote price for the same symbol at or before the trade time, using pandas merge_asof. Tests as-of join semantics, matching within a symbol, the sort order the join requires, trades with no earlier quote, and limits on quote staleness.
Attach the latest quote price to each trade with pandas merge_asof
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement `attach_price_asof(trades, quotes)` that attaches to each trade the most recent quote price for the same symbol at or before the trade time. `trades` has columns `time` (datetime64), `symbol` (str) and `qty` (int). `quotes` has columns `time` (datetime64), `symbol` (str) and `price` (float). Both frames are sorted by `time` ascending.
The function must:
- Use `pd.merge_asof` on `time` with `by='symbol'` and the default `direction='backward'`.
- Return exactly the columns `['time', 'symbol', 'qty', 'price']`.
- Give a `NaN` price to a trade with no prior quote for its symbol.
- Keep the `merge_asof` output order and reset to a fresh `RangeIndex` (drop the old index).
```text
trades:
time symbol qty
2024-01-01 09:30:05 AAPL 100
2024-01-01 09:30:07 MSFT 50
quotes:
time symbol price
2024-01-01 09:30:00 AAPL 189.1
2024-01-01 09:30:04 MSFT 370.2
2024-01-01 09:30:06 AAPL 189.4
attach_price_asof(trades, quotes):
time symbol qty price
2024-01-01 09:30:05 AAPL 100 189.1
2024-01-01 09:30:07 MSFT 50 370.2
```
```hint Order and grouping
Check what `merge_asof` assumes about the order of the `time` column in each frame, and whether matching within each symbol changes that assumption.
```
### Constraints & Assumptions
- Each frame is sorted by `time` ascending across all of its rows.
- A quote stamped at exactly the trade time counts as "at or before" the trade.
### What a Strong Answer Covers
- Why an exact-key join cannot attach "the most recent quote", and what the as-of join does for each trade.
- How the match stays within one symbol, and the sort order `merge_asof` requires on `time`.
- Trades with no earlier quote, and quotes stamped at exactly the trade time.
- The exact output columns and the fresh `RangeIndex`.
- The cost of the join compared with searching the quotes once per trade.
### Follow-up Questions
- How would you cap how stale a matched quote can be?
- What must hold about sort order, and what happens if it is violated?
- What would you change if a quote stamped at the same instant as the trade must not be used?
Overview: Attach to each trade the most recent quote price for the same symbol at or before the trade time, using pandas merge_asof. Tests as-of join semantics, matching within a symbol, the sort order the join requires, trades with no earlier quote, and limits on quote staleness.
Implement attach_price_asof(trades, quotes) that attaches to each trade the most recent quote price for the same symbol at or before the trade time. trades has columns time (datetime64), symbol (str) and qty (int). quotes has columns time (datetime64), symbol (str) and price (float). Both frames are sorted by time ascending.
The function must:
Use
pd.merge_asof
on
time
with
by='symbol'
and the default
direction='backward'
.
Return exactly the columns
['time', 'symbol', 'qty', 'price']
.
Give a
NaN
price to a trade with no prior quote for its symbol.
Keep the
merge_asof
output order and reset to a fresh
RangeIndex
(drop the old index).
trades:
time symbol qty
2024-01-01 09:30:05 AAPL 100
2024-01-01 09:30:07 MSFT 50
quotes:
time symbol price
2024-01-01 09:30:00 AAPL 189.1
2024-01-01 09:30:04 MSFT 370.2
2024-01-01 09:30:06 AAPL 189.4
attach_price_asof(trades, quotes):
time symbol qty price
2024-01-01 09:30:05 AAPL 100 189.1
2024-01-01 09:30:07 MSFT 50 370.2
Constraints & Assumptions
Each frame is sorted by
time
ascending across all of its rows.
A quote stamped at exactly the trade time counts as "at or before" the trade.
What a Strong Answer Covers Guidance
Why an exact-key join cannot attach "the most recent quote", and what the as-of join does for each trade.
How the match stays within one symbol, and the sort order
merge_asof
requires on
time
.
Trades with no earlier quote, and quotes stamped at exactly the trade time.
The exact output columns and the fresh
RangeIndex
.
The cost of the join compared with searching the quotes once per trade.
Follow-up Questions Guidance
How would you cap how stale a matched quote can be?
What must hold about sort order, and what happens if it is violated?
What would you change if a quote stamped at the same instant as the trade must not be used?