Chalk DataFrames
Clean transaction descriptions, match text, extract identifiers, and redact values.
A payment processor supplies a free-text memo with each transaction. Preserve the source memo for reconciliation and derive clean fields for grouping, filters, and display. These examples build on the same four transactions.
Strip padding before lowercasing. Display the original in quotes so the spaces at either end are visible beside the cleaned value.
from chalk import _, functions as F
from chalkdf import DataFrame
tx = DataFrame({
"id": [1, 2, 3, 4],
"memo": [" WEB:Order-0042 ", "web:order-0178", "POS:refund-19", None],
})
clean = tx.with_columns(F.lower(F.trim(_.memo)).alias("text"))
quoted = ('"' + _.memo + '"').alias("quoted_memo")
clean.select("id", quoted, "text").run()| # | idint64 | quoted_memolarge_string | textlarge_string |
|---|---|---|---|
| 1 | 1 | " WEB:Order-0042 " | web:order-0042 |
| 2 | 2 | "web:order-0178" | web:order-0178 |
| 3 | 3 | "POS:refund-19" | pos:refund-19 |
| 4 | 4 | null | null |
A missing memo stays null through both operations. Fill it only when the application needs a display value.
To select all web transactions, match a literal prefix against the clean field.
clean.filter(F.starts_with(_.text, "web:")).select("id", quoted).run()| # | idint64 | quoted_memolarge_string |
|---|---|---|
| 1 | 1 | " WEB:Order-0042 " |
| 2 | 2 | "web:order-0178" |
Testing the original field would miss the padded uppercase memo. Filtering on the normalized field still lets you return the original memo.
The channel is everything before the colon. An order ID has a more specific pattern. Use a capture group to extract just its digits.
parsed = clean.project({
"id": _.id,
"channel": F.split_part(_.text, ":", 0),
"is_order": F.regexp_like(_.text, r"order-[0-9]+"),
"order_id": F.regexp_extract(_.text, r"order-([0-9]+)", 1),
})
parsed.run()| # | idint64 | channellarge_string | is_orderbool | order_idlarge_string |
|---|---|---|---|---|
| 1 | 1 | web | True | 0042 |
| 2 | 2 | web | True | 0178 |
| 3 | 3 | pos | False | null |
| 4 | 4 | null | null | null |
The pattern test returns a boolean, while extraction produces a field you can join or group on. Keep order IDs as text to preserve their leading zeros.
To keep the channel and event type in a support view, replace each run of digits while retaining the original memo on the source frame.
clean.project({
"id": _.id,
"display_memo": F.coalesce(F.regexp_replace(_.text, r"[0-9]+", "***"), "(missing)"),
}).run()| # | idint64 | display_memolarge_string |
|---|---|---|
| 1 | 1 | web:order-*** |
| 2 | 2 | web:order-*** |
| 3 | 3 | pos:refund-*** |
| 4 | 4 | (missing) |
The fallback applies to the display field. The missing transaction remains distinguishable when you group by the original field.
Group on the parsed field after removing transactions without a known channel.
parsed.filter(F.is_not_null(_.channel)).agg(
["channel"], _.count().alias("transactions")
).order_by("channel").run()| # | channellarge_string | transactionsint64 |
|---|---|---|
| 1 | pos | 1 |
| 2 | web | 2 |
The two web memos now contribute to the same channel total.
Take the channel and the original order identifier from the parsed fields. Join the two with a separator for display.
parsed.filter(_.is_order).project({
"label": _.channel + "/" + _.order_id,
}).run()| # | labellarge_string |
|---|---|
| 1 | web/0042 |
| 2 | web/0178 |
The labels preserve the leading zeros of the captured order IDs. Keep the fields separate for joins on channel or order ID.
Use a slice when a feed specifies the position of a field. In this feed the first three characters identify the issuing office.
codes = DataFrame({"code": ["SFO0042", "LAX0178", None]})
codes.project({
"code": _.code, "office": F.substr(_.code, 0, 3),
"serial": F.substr(_.code, 3),
}).run()| # | codestring | officestring | serialstring |
|---|---|---|---|
| 1 | SFO0042 | SFO | 0042 |
| 2 | LAX0178 | LAX | 0178 |
| 3 | null | null | null |
The start offset is zero-based. Omitting the length takes the rest of the string. Use a capture group when the prefix varies in length.
See string functions and missing data.