Skip to content

Only check the date part of a reference date for a four-digit year - #11997

Open
1AbdullahButt wants to merge 1 commit into
pydata:mainfrom
1AbdullahButt:fix-padded-year-fractional-seconds
Open

1AbdullahButt wants to merge 1 commit into
pydata:mainfrom
1AbdullahButt:fix-padded-year-fractional-seconds

Conversation

@1AbdullahButt

@1AbdullahButt 1AbdullahButt commented Dec 5, 2026 •

Copy link
Copy Markdown

Description

I noticed that _ensure_padded_year checks the entire reference date for four consecutive digits, including the time part. This means that fractional seconds or UTC offsets can cause it to skip padding the year, resulting in an incorrect date.

For example:

import numpy as np
from xarray.coding.times import decode_cf_datetime

decode_cf_datetime(np.array([0]), "days since 1-1-1", "proleptic_gregorian")
# Correctly decodes to year 1

decode_cf_datetime(np.array([0]), "days since 1-1-1 00:00:00.000000", "proleptic_gregorian")
# Incorrectly decodes to 2001-01-01

decode_cf_datetime(np.array([0]), "days since 2-3-4 00:00:00 +0000", "proleptic_gregorian")
# Incorrectly decodes to 2004-02-03

Using use_cftime=True gives the correct result, so the decoded date can differ depending on which decoding path is used. There is also no warning when this happens.

I've changed the check to exclude the time part of the reference date. Dates that already contain a four-digit year, such as 1-1-1970, 19700101 or Jan 1 1970, are unaffected.

Testing

I extended test_decode_ambiguous_time_warns to cover reference dates with fractional seconds. Three cases fail on main, and all 18 pass with this change.

I also ran the full test_coding_times.py suite:

  • 1455 passed
  • 67 skipped
  • 1 xfailed
  • Ruff linting and formatting checks passed.

Checklist

  • Tests added
  • User visible changes (including notable bug fixes) are documented in whats-new.rst

AI Disclosure

I used Claude Code to assist with the implementation and reviewed the changes and test results myself.

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Claude Code

🤖 Generated with Claude Code

_ensure_padded_year skipped zero-padding whenever the reference date held
four consecutive digits anywhere, so fractional seconds in the time part
("1-1-1 00:00:00.000000") or a "+0000" offset counted as a year. The
unpadded date then reached pandas, which reads "1-1-1" as 2001-01-01, and
decoding with the default use_cftime=None shifted every date by 2000
years.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@1AbdullahButt
1AbdullahButt force-pushed the fix-padded-year-fractional-seconds branch from 067c708 to c8ad7ea Compare December 5, 2026 22:43

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant