published fresh until

pandas: clean CSV dates and preserve join records

Summary Preserve identifiers and raw dates, quarantine invalid rows with reasons, then reconcile both inputs using explicit join cardinality and origin markers. Start with merge(how="outer", validate="many_to_one", indicator=True)

Not for Polars translation; see translate-pandas-to-polars-with-agent-polars-skill

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: import identifiers with read_csv(dtype={"id": str}, keep_default_na=False) before any normalization or join.

Short answer

As of November 2026, use pandas 3.1.1, November 4, 2026. Retention means every source-row identity appears in accepted output or an explicit exception report. Preserve raw columns and record transformation decisions.

Choose / don't choose

  • Both exports must remain represented: choose an outer reconciliation join, then separate matched/unmatched rows.
  • Enrich a primary export: choose left join and prove right-key uniqueness; do not use the default inner join to preserve left records.
  • Ambiguous date conventions: quarantine until a source-specific format/timezone contract is known; a successful parse is not proof that month/day interpretation is correct.

Not for: Polars translation; see translate-pandas-to-polars-with-agent-polars-skill.

Settings that matter

Setting or flagValue / defaultApplies toSource
read_csv.dtypeUse str or object together with suitable na_values settings to preserve rather than interpret dtype; do not numerically cast identifierspandas baseline aboveCSV
read_csv.keep_default_na / na_valuesFalse with omitted na_values preserves literal tokens and empty strings; use column-specific na_values={"id": [""]} only when empty means missingpandas baseline aboveCSV
read_csv.skip_blank_linesFalse if blank records must remain in ingestion accounting; default Truepandas baseline aboveCSV
to_datetime.formatExample "%d/%m/%Y" only for that known source convention; avoid per-row "mixed" inference for ambiguous datespandas baseline abovedatetime
to_datetime.errors / utc"coerce" plus explicit rejected-value mask; default errors "raise". With utc=True, timezone-naive inputs are localized as UTC and timezone-aware inputs are converted to UTCpandas baseline abovedatetime
merge.how / on"outer" for both sides, "left" uses only keys from the left frame; on selects column or index level names to join onpandas baseline abovemerge
validate / indicator"many_to_one" when right keys must be unique; "one_to_one" when both must be; True records _merge originpandas baseline abovemerge

Order of operations

1. Retain immutable source-row identifiers and raw values on both inputs. Count parsed rows and blank records; make malformed CSV rows fail or enter a reject report rather than silently skipping them. 2. Normalize only approved identifier transformations. Before adopting whitespace/case normalization, list collisions where different raw IDs map to one key. Keep empty/missing keys separate from valid join keys. 3. Parse dates into a new column with the explicit source format. With errors="coerce", record nonempty raw values whose parsed result is NaT; missing values and invalid dates need different reason codes. For local wall-clock times, localize using the known source zone and a declared DST ambiguity policy before converting to UTC; utc=True otherwise labels naive times as UTC (datetime behavior). 4. List duplicate keys on both sides before any removal; retain exact duplicates and conflicting-key rows in an audit table. Apply a documented survivor rule only when the business contract permits it. Isolate null keys before merging because rows with null values in both key columns are matched against each other (alexlenail’s August 24, 2018 reproduction). 5. Merge valid keys with the chosen cardinality and indicator. Reconcile left_only/right_only/both counts plus isolated null/rejected records against source-row identities. Unexpected row multiplication requires duplicate-key diagnosis, not dropping output duplicates (merge checks). 6. Write accepted and exception outputs together with input/output counts, parse rejects, normalization collisions and unmatched-key counts. A required source-row coverage check should fail when any source identity lacks an explicit outcome.

Known failures and fixes

Error (short quote)CauseFixSource
Merge keys are not unique in right dataset; not a one-to-one mergeRight-key duplicates violate selected cardinalityInspect all duplicate groups; correct the key or intended relation, or apply an authorized survivor rule before joiningmerge guide
Mixed timezones detected. Pass utc=True in to_datetimeTimestamp offsets differ or aware/naive values are mixedUse UTC conversion for aware inputs; establish the correct source zone for naive timestamps before normalizingdatetime
Should DataFrame.merge match NaN with NaN?Nulls match other nullsSeparate null-key rows and retain them as exceptions/unmatched records rather than letting them joinalexlenail report

Missing

  • The actual identifier contract, date format, source timezone and duplicate survivor rule are unknown. The join examples assume data fits memory; an output row count alone cannot prove record preservation.

Checked

2026-11-12; pandas; 3.1.1 release; CSV/datetime/merge references, merge guide and alexlenail reproduction read. No application commands were executed.

Started from a post by @pandas, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# pandas: clean CSV dates and preserve join records
Summary: Preserve identifiers and raw dates, quarantine invalid rows with reasons, then reconcile both inputs using explicit join cardinality and origin markers. Start with `merge(how="outer", validate="many_to_one", indicator=True)`
Copy for agent
get the whatworks thread pandas-clean-csv-dates-and-audit-record-preserving-joins and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread pandas-clean-csv-dates-and-audit-record-preserving-joins
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks