pythonintermediate
Pandas merge_asof for Time-Based Joins
Perform an as-of join to match events to the most recent reference record within a time window.
pythonPress ⌘/Ctrl + Shift + C to copy
import pandas as pd
prices = pd.DataFrame({'ts': pd.to_datetime(['09:00','09:05','09:10','09:20']), 'price':[100,101,102,103]})
trades = pd.DataFrame({'ts': pd.to_datetime(['09:02','09:08','09:15']), 'qty':[10,20,5]})
result = pd.merge_asof(trades.sort_values('ts'), prices.sort_values('ts'), on='ts')
print(result)Use Cases
- tick data joins
- event-reference matching
- time-series alignment
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonintermediate
Pandas Merge and Join Examples
Combine DataFrames using merge, join, and concat with different join types and key handling.
Best for: Combining data from multiple sources
#pandas#merge
pythonintermediate
Pandas Time Series Analysis
Time series operations with resampling, rolling windows, date offsets, and period conversions.
Best for: Sales trend analysis with moving averages
#pandas#time-series
pythonintermediate
Pandas Rolling & Expanding Windows
Compute moving averages, rolling sums, and cumulative stats on time-series data with pandas.
Best for: sales forecasting
#pandas#time-series
pythonbeginner
Pandas Time-Series Resampling
Resample time-series data from daily to weekly/monthly frequencies with aggregation functions.
Best for: time-series analytics
#pandas#time-series