Chalk DataFrames
Match records on equal keys or the nearest observation in an ordered series.
Use an equality join to attach a user attribute to each transaction. A left join keeps a transaction even when its user has no matching record.
from chalkdf import DataFrame
tx = DataFrame({"user": ["a", "a", "b"], "amount": [12, 8, 30]})
users = DataFrame({"user": ["a"], "region": ["west"]})
tx.join(users, on=["user"], how="left").select("user", "amount", "region").order_by("amount").run()| # | userstring | amountint64 | regionstring |
|---|---|---|---|
| 1 | a | 8 | west |
| 2 | a | 12 | west |
| 3 | b | 30 | null |
User b has no region in the lookup table. Use an inner join to keep only matches. If a key matches several right-hand rows, each match contributes a result row.
For a rate that changes over time, use join_asof with the timestamp as on and the currency as by. With the backward strategy, a transaction takes the latest rate at or before its timestamp within that currency. Sort observations in time order and choose one rate per currency and timestamp to make ties unambiguous.
Here the time key is an integer minute. A payment at minute 3 uses the rate published at minute 1. A payment at minute 5 can use the rate published that same minute.
payments = DataFrame({"minute": [3, 5], "currency": ["USD", "USD"]})
rates = DataFrame({"minute": [1, 5], "currency": ["USD", "USD"], "rate": [10, 12]})
payments.join_asof(rates, on="minute", by="currency", strategy="backward").run()| # | minuteint64 | currencystring | rateint64 |
|---|---|---|---|
| 1 | 3 | USD | 10 |
| 2 | 5 | USD | 12 |
A payment preceding the first rate for its currency has no match.