Measured on pandas 3.1.1: read_csv defaults drop leading zeros, turn NA and null into missing values and make long ids inexact floats. Declare a load contract.
The load step that edits your data
pandas.read_csv is usually the first line of a data job and the least reviewed one. With default arguments it does three things that look like convenience and behave like edits. It infers a type for every column from the values it sees, so a column of digit strings becomes integers. It treats a list of strings such as NA, null, N/A and None as missing values, in every column. And when an integer column has a blank, it cannot store the blank in an integer column, so the whole column becomes floating point.
None of this raises a warning, and for numeric measurements it is often what you want. For identifiers and codes it is data loss. At Belgavi's Book Store a nightly job loads the order export, joins it to the catalogue by ISBN and to customers by customer code, and writes a summary that Tanishka's team uses for restocking. ISBN-10 values starting with zero stopped matching the catalogue, customer codes such as 00412 turned into 412, and the region code for Namibia, NA, became missing for every Namibian order. The job did not fail; the joins just matched less.
Measured on pandas 3.1.1
The companion example loads a three-row synthetic export with pandas 3.1.1, the current release on 15 November 2026, under four sets of arguments, and records the dtype and values of every column. The file:
order_id,isbn,customer_code,region,qty,ref
9007199254740993,0143127748,00412,NA,2,null
9007199254740995,0306406152,00977,EU,,N/A
9007199254740997,9780306406157,01200,null,1,okWith defaults:
| Column | Loaded dtype | Loaded values | What changed |
|---|---|---|---|
order_id | int64 | 9007199254740993, ...995, ...997 | nothing; exact |
isbn | int64 | 143127748, 306406152, 9780306406157 | leading zeros dropped |
customer_code | int64 | 412, 977, 1200 | leading zeros dropped |
region | str | missing, EU, missing | NA and null became missing |
qty | float64 | 2.0, missing, 1.0 | blank forced the column to float |
ref | str | missing, missing, ok | null and N/A became missing |
The order ids are larger than 2 to the power 53 and still exact, because the column has no blanks and stays int64. A second file shows what happens when it does. A parent_order_id column with the values 9007199254740993, blank and 9007199254740995 loads as float64, and the values become 9007199254740992.0 and 9007199254740996.0. A float64 has 53 bits of significand, so above 2 to the power 53 not every integer is representable, and these two round to their even neighbours. Converting back to int does not restore them. Every downstream join on parent order silently misses.
Three arguments that stop the edits
Most of the fix is three keyword arguments.
dtype fixes the type per column instead of inferring it. Identifiers and codes should be strings even when every value is digits, because arithmetic on them is meaningless and their textual form, including leading zeros, is the identity. Counts that can be blank need the nullable Int64 extension type, which holds a missing value without becoming float.
keep_default_na=False turns off the built-in list of missing-value strings. The documented default list includes the empty string, NA, N/A, n/a, NULL, null, NaN, nan, None, <NA>, #N/A and several spreadsheet error forms. Any of them can be a legitimate value: a region code, a reference typed by a person, or a product named None.
na_values then says what really means missing, per column. Usually that is the empty string in the few columns where a blank is allowed.
Passing dtype=str alone is not enough. The recording shows it keeps the leading zeros but still turns NA, null and N/A into missing values, because missing-value detection still applies when the dtype is a string. Only with keep_default_na=False as well do the strings survive.
import pandas as pd
ORDERS = {
'dtype': {'order_id': 'string', 'isbn': 'string', 'customer_code': 'string',
'region': 'string', 'ref': 'string', 'qty': 'Int64'},
'keep_default_na': False,
'na_values': {'qty': ['']},
}
df = pd.read_csv('orders.csv', **ORDERS)With that contract every value in the recording loads exactly as written: string columns keep 00412, 0143127748, NA and null, and qty is Int64 with one missing value. For the parent order column, either string or Int64 keeps the ids exact; choose string if anything downstream treats them as identifiers.
A load contract as code
Arguments scattered across jobs drift. The example keeps the contract next to the data definition and generates a first version from the file itself. Its scanner, written with the standard library so it can run where pandas is not installed, reads the raw text and reports, per column:
- values with leading zeros that integer inference would drop;
- values that are on pandas' default missing-value list;
- integers above 2 to the power 53 in a column that also has blanks;
- whether blanks occur, and a suggested dtype.
On the export above plus the parent column, it flags both zero-padded columns, NA and null in region, N/A and null in ref, and two large integers with a blank in parent_order_id, and it emits a contract with keep_default_na=False and blanks as missing only in qty and parent_order_id. It suggests int64 for order_id because every value is a plain integer with no blank; a person should still override that to string, because it is an identifier. The scanner finds risks; it does not decide what a column means.
Migration for existing jobs
- Inventory every
read_csv,read_tableandread_excelcall and record its arguments.read_excelhas the same missing-value defaults and infers types from cell contents, so the same review applies. - For each input, list the identifier and code columns and declare them as strings. Declare counts and amounts with nullable types where blanks are allowed.
- Add
keep_default_na=Falseand explicitna_values. Expect some downstream code to start seeing literal strings such asnullthat it used to see as missing; that code was depending on a coincidence and should handle the real missing marker instead. - Add a contract test that loads a small fixture file containing each risky value, a zero-padded code, a real
NA, a blank count and a large id with a blank, and asserts the loaded values exactly. - Compare row-level outputs of the old and new job for a few days before switching. The differences are the records the old job was corrupting.
Failure modes
Inference changes with the data. A column that contains only digits today becomes string tomorrow when one value has a letter, and the type of every row changes with it. Explicit dtypes make the type independent of content.
Writing back out. A frame that loaded ISBNs as integers writes them back without the zeros, so the damage leaves the job. A round-trip test catches it.
Chunked reads. With chunksize, inference runs per chunk, so one column can come back with different dtypes in different chunks. Explicit dtypes remove that too.
Other engines. The pyarrow engine and other readers have their own inference and missing-value rules. Pin the engine along with the arguments, and rerun the contract test when you change either.
Spreadsheet round trips. If the file passed through a spreadsheet application before reaching you, leading zeros may already be gone and long numbers may be in scientific notation. No read argument can restore them; the contract test at least tells you.
When defaults are fine. For numeric measurement data with no codes and a known missing marker, inference is reasonable. Even then, declaring dtypes documents the file and catches a column that changes shape.
Verification
The companion example pins the scanner's report and contract in expected.json. Its standard-library tests check each risk class, that the plain integer id gets no warning, and the emitted contract. Three more tests run when pandas is installed: the scanner's missing-value list equals the one in the installed pandas, loading the sample with the emitted contract returns every value exactly as written, and loading it with defaults reproduces the damage. The recording comes from probe_pandas.py with pandas==3.1.1 on CPython 3.13.5, and a rerun of the probe in a fresh environment produced identical output.