Skip to content

Timedelta in to_json object array and ISO dates not handled properly #28256

Closed
@WillAyd

Description

@WillAyd

Intertwined with #15137 though a slight different issue.

date_format="iso" has different behavior for Timedeltas depending on whether or not the Timedelta is in a DTA or an object array. To illustrate:

# Wrong format ref 15137, but at least tries to do some formatting
>>> pd.DataFrame([[pd.Timedelta("1D")]]).to_json(date_format="iso")
'{"0":{"0":"1970-01-02T00:00:00.000Z"}}'

# Object array has no formatting
>>> pd.DataFrame([[pd.Timedelta("1D")]]).astype(object).to_json(date_format="iso")
'{"0":{"0":86400000}}'

By contrast the same issue does not appear with datetimes

>>> pd.DataFrame([[pd.Timestamp(1)]]).to_json(date_format="iso")
'{"0":{"0":"1970-01-01T00:00:00.000Z"}}'

# Below still formats as iso in spite of being object array
>>> pd.DataFrame([[pd.Timestamp(1)]]).astype(object).to_json(date_format="iso")
'{"0":{"0":"1970-01-01T00:00:00.000Z"}}'

Metadata

Metadata

Assignees

No one assigned

    Labels

    IO JSONread_json, to_json, json_normalizeTimedeltaTimedelta data type

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions