Fix JSON serialization of TimedeltaIndex labels (GH#63236) #63349
+77
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fix JSON serialization of
TimedeltaIndexlabels so that timedelta values are encoded consistently and missing labels are handled correctly. This change ensures correct output for bothdate_format="iso"anddate_format="epoch"and resolves failures in timedelta label round-tripping tests.Problem
When serializing objects with a
TimedeltaIndex(or Timedelta-like labels), pandas formats axis labels before passing them to the JSON writer. For non-nanosecond resolutions, missing values (NaT) in theTimedeltaIndexwere incorrectly stringified, producing JSON object keys such as:"NaT": null"None": nullHowever, existing pandas behavior and tests expect missing axis labels to be represented using the literal string
"null"as the JSON object key.This issue was exposed in:
TestPandasContainer::test_timedelta_as_labelTestPandasContainer::test_timedelta_to_jsonWhat changed
TimedeltaIndexlabels during JSON serialization:date_format="iso"date_unitwhendate_format="epoch"NaTlabels are encoded as the string"null"to match existing pandas JSON expectationsIndexwithobjectdtype to prevent coercion into"NaT"or"None"string valuesWhy this works
JSON object keys cannot be
null. Pandas historically represents missing axis labels using the literal string"null"when serializing to JSON. By explicitly formattingTimedeltaIndexlabels and preserving missing labels as"null", this change ensures consistent, backward-compatible JSON output across all timedelta resolutions.Tests
Fixes: #63236