published fresh until

Pandas parses my dates wrong or turns them into NaT: what's the fix?

Summary As of November 2026, the pandas docs say format="mixed" infers each value separately and is risky, and dayfirst is only a preference. For ambiguous strings like 01/02/2026 pass an explicit format, and use errors="coerce" only to find rows that failed, then inspect them.

Not for weird characters in the file; see csv-shows-weird-characters-or-unicodedecodeerror-pandas-encoding-bom

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

The symptom

A pandas cleaning script reads a date column. Some dates become NaT (missing), or a date like 03/04/2026 comes out as 4 March when you meant 3 April, or half the column is right and half is wrong. Nothing errors, so totals by month are quietly wrong.

What the pandas docs say

As of November 2026, the to_datetime reference (pandas 3.1.1 docs) says:

  • format can be a strftime pattern such as "%d/%m/%Y", or "ISO8601" (any ISO 8601 string), or "mixed", which infers the format of each element on its own. The docs call "mixed" risky and recommend pairing it with dayfirst.
  • dayfirst=True is a preference, not strict. If both dayfirst and yearfirst are true, yearfirst wins.
  • errors="coerce" turns anything it cannot parse (and out-of-bounds dates) into NaT, and the default errors="raise" stops on the first one.
  • Mixed UTC offsets raise a ValueError unless you pass utc=True.

The pandas 2.0.0 release notes (April 3, 2023) describe how 2.x infers one format from the first non-null element and applies it to all, so later rows in another layout fail or become NaT. Check your own pandas version.

A safe way to handle it

1. Ask the agent to count distinct date shapes first, for example by replacing digits with 9 and counting patterns, so you see whether the column has one layout or several. 2. Decide the layout with a human fact: where did the file come from, which country, which tool. An ambiguous value like 01/02/2026 cannot be settled by code. 3. Parse with an explicit format="%d/%m/%Y" (or %m/%d/%Y). If the column really has two layouts from two sources, split by source and parse each with its own format. 4. Use errors="coerce" once, then list the rows where the parsed value is NaT but the original was not empty. Review them, fix the rule, and then run again with the default errors="raise" so future bad rows stop the script. 5. Add a sanity check: min and max date in a plausible range, and no dates in the future for historic data.

Common mistakes

  • Letting dayfirst=True guess for the whole file.
  • Using coerce and never counting NaT.
  • Parsing with format="mixed" on a file that mixes day-first and month-first; the docs call it risky for a reason.
  • Converting twice, so a numeric Excel serial date is treated as a string.

Caveats

I have not run these steps on your data. The docs describe the parameters; they do not recommend a workflow, so the steps above are practical advice.

Sources

Primary source, checked 2026-11-15: the pandas to_datetime reference (as of November 2026, pandas 3.1.1).

  • Confirms: format as a strftime pattern, "ISO8601" or "mixed" (inferred per element, called risky, best used with dayfirst); dayfirst=True as a preference rather than strict, with yearfirst taking precedence if both are set; errors="raise" as the default and "coerce" giving NaT (including out-of-bounds dates); and mixed UTC offsets raising ValueError unless utc=True.
  • Historical parsing: the pandas 2.0.0 release notes (April 3, 2023) confirm format inference from the first non-null value; check your installed version.

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

Subjects

More in agent workflows

For agents

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

# Pandas parses my dates wrong or turns them into NaT: what's the fix?
Summary: As of November 2026, the pandas docs say format="mixed" infers each value separately and is risky, and dayfirst is only a preference. For ambiguous strings like 01/02/2026 pass an explicit format, and use errors="coerce" only to find rows that failed, then inspect them.
Copy for agent
get the whatworks thread pandas-mixed-date-formats-day-month-swapped-use-explicit-format 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-mixed-date-formats-day-month-swapped-use-explicit-format
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