|
38 | 38 | from pandas.core.dtypes.common import ( |
39 | 39 | ensure_str, |
40 | 40 | is_string_dtype, |
| 41 | + is_timedelta64_dtype, |
41 | 42 | pandas_dtype, |
42 | 43 | ) |
43 | 44 | from pandas.core.dtypes.dtypes import PeriodDtype |
44 | 45 |
|
| 46 | +import pandas as pd |
45 | 47 | from pandas import ( |
46 | 48 | ArrowDtype, |
47 | 49 | DataFrame, |
@@ -222,6 +224,44 @@ def to_json( |
222 | 224 | return None |
223 | 225 |
|
224 | 226 |
|
| 227 | +def _format_timedelta_labels(index, date_format: str, date_unit: str | None): |
| 228 | + """ |
| 229 | + Format TimedeltaIndex labels for JSON serialization. |
| 230 | +
|
| 231 | + Rules: |
| 232 | + - Timedelta values → ISO 8601 (iso) or integer (epoch) |
| 233 | + - NaT MUST stay missing so JSON encodes it as null |
| 234 | + """ |
| 235 | + |
| 236 | + # Fast-path: empty index |
| 237 | + if len(index) == 0: |
| 238 | + return index |
| 239 | + |
| 240 | + values = index._values # ndarray[td64] |
| 241 | + result = [] |
| 242 | + |
| 243 | + if date_format == "iso": |
| 244 | + for val in values: |
| 245 | + if isna(val): |
| 246 | + # critical: preserve missing → JSON null |
| 247 | + result.append("null") |
| 248 | + else: |
| 249 | + td = pd.Timedelta(val) |
| 250 | + result.append(td.isoformat()) |
| 251 | + |
| 252 | + else: # epoch |
| 253 | + unit = date_unit or "ms" |
| 254 | + |
| 255 | + for val in values: |
| 256 | + if isna(val): |
| 257 | + result.append("null") |
| 258 | + else: |
| 259 | + td = pd.Timedelta(val).as_unit(unit) |
| 260 | + result.append(int(td._value)) |
| 261 | + |
| 262 | + return Index(result, dtype=object) |
| 263 | + |
| 264 | + |
225 | 265 | class Writer(ABC): |
226 | 266 | _default_orient: str |
227 | 267 |
|
@@ -287,6 +327,12 @@ def obj_to_write(self) -> NDFrame | Mapping[IndexLabel, Any]: |
287 | 327 | def _format_axes(self) -> None: |
288 | 328 | if not self.obj.index.is_unique and self.orient == "index": |
289 | 329 | raise ValueError(f"Series index must be unique for orient='{self.orient}'") |
| 330 | + # FIX:GH#63236 format TimedeltaIndex labels correctly before ujson_dumps |
| 331 | + if is_timedelta64_dtype(self.obj.index.dtype): |
| 332 | + self.obj = self.obj.copy(deep=False) |
| 333 | + self.obj.index = _format_timedelta_labels( |
| 334 | + self.obj.index, self.date_format, self.date_unit |
| 335 | + ) |
290 | 336 |
|
291 | 337 |
|
292 | 338 | class FrameWriter(Writer): |
@@ -317,6 +363,29 @@ def _format_axes(self) -> None: |
317 | 363 | raise ValueError( |
318 | 364 | f"DataFrame columns must be unique for orient='{self.orient}'." |
319 | 365 | ) |
| 366 | + # FIX:GH#63236 format Timedelta labels (Index and Columns) correctly |
| 367 | + if ( |
| 368 | + not isinstance(self.obj.index, MultiIndex) |
| 369 | + and is_timedelta64_dtype(self.obj.index.dtype) |
| 370 | + ) or ( |
| 371 | + not isinstance(self.obj.columns, MultiIndex) |
| 372 | + and is_timedelta64_dtype(self.obj.columns.dtype) |
| 373 | + ): |
| 374 | + self.obj = self.obj.copy(deep=False) |
| 375 | + |
| 376 | + if not isinstance(self.obj.index, MultiIndex) and is_timedelta64_dtype( |
| 377 | + self.obj.index.dtype |
| 378 | + ): |
| 379 | + self.obj.index = _format_timedelta_labels( |
| 380 | + self.obj.index, self.date_format, self.date_unit |
| 381 | + ) |
| 382 | + |
| 383 | + if not isinstance(self.obj.columns, MultiIndex) and is_timedelta64_dtype( |
| 384 | + self.obj.columns.dtype |
| 385 | + ): |
| 386 | + self.obj.columns = _format_timedelta_labels( |
| 387 | + self.obj.columns, self.date_format, self.date_unit |
| 388 | + ) |
320 | 389 |
|
321 | 390 |
|
322 | 391 | class JSONTableWriter(FrameWriter): |
|
0 commit comments