Skip to content

Timedelta to_json with date_format='iso' #15137

Closed
@TomAugspurger

Description

@TomAugspurger

Is this formatting for timdelta columns intended / correct?

In [12]: df = pd.DataFrame({"A": pd.date_range("2016", periods=4, freq='D'),
    ...:                    "B": pd.timedelta_range('1D', periods=4, freq='D')})

In [13]: df.to_json(date_format='iso')
Out[13]: '{"A":{"0":"2016-01-01T00:00:00.000Z","1":"2016-01-02T00:00:00.000Z","2":"2016-01-03T00:00:00.000Z","3":"2016-01-04T00:00:00.000Z"},"B":{"0":"1970-01-02T00:00:00.000Z","1":"1970-01-03T00:00:00.000Z","2":"1970-01-04T00:00:00.000Z","3":"1970-01-05T00:00:00.000Z"}}'

So a pd.Timedelta('1D') goes to "1970-01-02T00:00:00.000Z".

The expected might be a duration like

"P1DT0H0M0S".

It seems to be reusing the logic for datetime columns and treating it as the equivalent int -> iso format. Ideally to_json will have a new paramater timedelta_format that takes the same values as date_format.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions