Skip to content

Conversation

@pavansai018
Copy link

@pavansai018 pavansai018 commented Dec 12, 2025

Summary

Fix JSON serialization of TimedeltaIndex labels so that timedelta values are encoded consistently and missing labels are handled correctly. This change ensures correct output for both date_format="iso" and date_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 the TimedeltaIndex were incorrectly stringified, producing JSON object keys such as:

  • "NaT": null
  • "None": null

However, 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_label
  • TestPandasContainer::test_timedelta_to_json

What changed

  • Added explicit formatting of TimedeltaIndex labels during JSON serialization:
    • valid timedelta labels are formatted as ISO-8601 strings when date_format="iso"
    • valid timedelta labels are formatted as integers in the requested date_unit when date_format="epoch"
    • missing NaT labels are encoded as the string "null" to match existing pandas JSON expectations
    • labels are returned as an Index with object dtype to prevent coercion into "NaT" or "None" string values

Why 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 formatting TimedeltaIndex labels and preserving missing labels as "null", this change ensures consistent, backward-compatible JSON output across all timedelta resolutions.

Tests

python -m pytest pandas/tests/io/json/test_pandas.py

Fixes: #63236

Ensure TimedeltaIndex labels are formatted consistently for JSON output
across resolutions and date_format settings. Missing values in the
TimedeltaIndex are now preserved as None so they serialize as JSON null,
avoiding NaT/None string keys.

This fixes round-tripping/expectations for timedelta labels in JSON
serialization.
Ensure TimedeltaIndex labels are formatted consistently for JSON output
across resolutions and date_format settings. Missing values in the
TimedeltaIndex are now preserved as None so they serialize as JSON null,
avoiding NaT/None string keys.

This fixes round-tripping/expectations for timedelta labels in JSON
serialization.
Ensure TimedeltaIndex labels are formatted consistently for JSON output
across resolutions and date_format settings. Missing values in the
TimedeltaIndex are now preserved as None so they serialize as JSON null,
avoiding NaT/None string keys.

This fixes round-tripping/expectations for timedelta labels in JSON
serialization.
@pavansai018 pavansai018 changed the title GH#63236 Fix JSON serialization of TimedeltaIndex labels (GH#63236) Dec 13, 2025
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.

BUG/API: to_json with non-nano TimedeltaIndex

1 participant