Skip to content

Commit f04bf15

Browse files
committed
GH#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.
1 parent cd0261a commit f04bf15

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

pandas/io/json/_json.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Any,
1212
Generic,
1313
Literal,
14+
cast,
1415
Self,
1516
TypeVar,
1617
final,
@@ -73,6 +74,7 @@
7374
)
7475
from pandas.io.parsers.readers import validate_integer
7576

77+
DateUnit = Literal["s", "ms", "us", "ns"]
7678
if TYPE_CHECKING:
7779
from collections.abc import (
7880
Callable,
@@ -238,7 +240,7 @@ def _format_timedelta_labels(index, date_format: str, date_unit: str | None):
238240
return index
239241

240242
values = index._values # ndarray[td64]
241-
result = []
243+
result: list[object] = []
242244

243245
if date_format == "iso":
244246
for val in values:
@@ -250,7 +252,10 @@ def _format_timedelta_labels(index, date_format: str, date_unit: str | None):
250252
result.append(td.isoformat())
251253

252254
else: # epoch
253-
unit = date_unit or "ms"
255+
if date_unit is None:
256+
unit: DateUnit = "ms"
257+
else:
258+
unit = cast(DateUnit, date_unit)
254259

255260
for val in values:
256261
if isna(val):

0 commit comments

Comments
 (0)