Skip to content

BUG: Timedelta.total_seconds method is returning wrong values in nanosecond intervals #46819

Open
@Tiarles

Description

@Tiarles

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np

duration, Ts = 0.5, 5e-7
signal = pd.to_timedelta(np.arange(0, duration, Ts), "s")

dt = signal[1] - signal[0]

print(signal[1])  # 0 days 00:00:00.000000500
print(signal[0])  # 0 days 00:00:00

print(dt.total_seconds())  # 0.0 <- Wrong output

print(dt / pd.Timedelta(seconds=1))  # 5e-07 <- Expected behavior of ``dt.total_seconds()``

duration, Ts = 0.5, 1e-4
signal = pd.to_timedelta(np.arange(0, duration, Ts), "s")

dt = signal[1] - signal[0]

print(signal[1])  # 0 days 00:00:00.000100
print(signal[0])  # 0 days 00:00:00

print(dt.total_seconds())  # 0.0001 <- Correct answer

print(dt / pd.Timedelta(seconds=1))  # 0.0001 <- Proof

Issue Description

The total_seconds method from the Timedelta class has an unexpected behavior on Pandas 1.4.2, since Pandas 1.0.5.
For a small difference in nanoseconds scale the number of seconds on the interval returns 0,

Expected Behavior

The expected behavior is like what happens in Pandas 1.0.5, where:

import pandas as pd
import numpy as np

duration, Ts = 0.5, 5e-7
signal = pd.to_timedelta(np.arange(0, duration, Ts), "s")

dt = signal[1] - signal[0]

print(dt.total_seconds())  # = 5e-07

Installed Versions

Python 3.8.4

Env w/ Pandas 1.4.2:

Package Version
numpy 1.22.3
pandas 1.4.2
pip 22.0.4
python-dateutil 2.8.2
pytz 2022.1
setuptools 60.10.0
six 1.16.0
wheel 0.37.1

Env w/ Pandas 1.0.5:

Package Version
numpy 1.22.3
pandas 1.0.5
pip 22.0.4
python-dateutil 2.8.2
pytz 2022.1
setuptools 60.10.0
six 1.16.0
wheel 0.37.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugRegressionFunctionality that used to work in a prior pandas versionTimedeltaTimedelta data type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions