Skip to content

ENH: support for non-nanosecond precision in pd.Timedelta #59389

Open
@veenstrajelmer

Description

@veenstrajelmer

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

When converting matlab datenums, I would expect this should be easily possible with pd.Timedelta(), however, several approaches give different errors:

import pandas as pd
# raises OutOfBoundsTimedelta: seconds=62703763200000000000, milliseconds=0, microseconds=0, nanoseconds=0
pd.Timedelta(days=725738)
# raises OutOfBoundsTimedelta: Cannot cast 725738 from D to 'ns' without overflow.
pd.to_timedelta(725738, unit='days')

I expect this is because of the nanosecond accuracy. Matlab dates are in days (dtype float) and I would be happy with microsecond (or less) precision. pd.date_range() and pd.Timestamp() now support non-nanosecond accuracy. Would this also be possible with pd.Timedelta()?

Feature Description

Add support for non-nanosecond accuracy for pd.Timedelta()

Alternative Solutions

It is possible to convert matlab datenums to pandas datetimes with the following approach from https://stackoverflow.com/a/49135037/5578371:

import pandas as pd
import numpy as np
time_wl = pd.to_datetime(np.array([725738])-719529, unit='D').round('s')

Gives: DatetimeIndex(['1987-01-01'], dtype='datetime64[ns]', freq=None)

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Closing CandidateMay be closeable, needs more eyeballsEnhancementNon-Nanodatetime64/timedelta64 with non-nanosecond resolution

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions