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 flag | Value / default | Applies to | Source |
|---|---|---|---|
read_csv.dtype | Use str or object together with suitable na_values settings to preserve rather than interpret dtype; do not numerically cast identifiers | pandas baseline above | CSV |
read_csv.keep_default_na / na_values | False with omitted na_values preserves literal tokens and empty strings; use column-specific na_values={"id": [""]} only when empty means missing | pandas baseline above | CSV |
read_csv.skip_blank_lines | False if blank records must remain in ingestion accounting; default True | pandas baseline above | CSV |
to_datetime.format | Example "%d/%m/%Y" only for that known source convention; avoid per-row "mixed" inference for ambiguous dates | pandas baseline above | datetime |
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 UTC | pandas baseline above | datetime |
merge.how / on | "outer" for both sides, "left" uses only keys from the left frame; on selects column or index level names to join on | pandas baseline above | merge |
validate / indicator | "many_to_one" when right keys must be unique; "one_to_one" when both must be; True records _merge origin | pandas baseline above | merge |
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) | Cause | Fix | Source |
|---|---|---|---|
Merge keys are not unique in right dataset; not a one-to-one merge | Right-key duplicates violate selected cardinality | Inspect all duplicate groups; correct the key or intended relation, or apply an authorized survivor rule before joining | merge guide |
Mixed timezones detected. Pass utc=True in to_datetime | Timestamp offsets differ or aware/naive values are mixed | Use UTC conversion for aware inputs; establish the correct source zone for naive timestamps before normalizing | datetime |
Should DataFrame.merge match NaN with NaN? | Nulls match other nulls | Separate null-key rows and retain them as exceptions/unmatched records rather than letting them join | alexlenail 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