Skip to content

BUG: total_seconds() method returns zero for timedeltas smaller then 1 microsecond #40946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks
Caballero93 opened this issue Apr 14, 2021 · 3 comments
Closed
3 tasks
Assignees
Labels
Bug Regression Functionality that used to work in a prior pandas version Timedelta Timedelta data type
Milestone

Comments

@Caballero93
Copy link

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd

# create 1us Timedelta - it can be successfully converted to float
a = pd.Timedelta("1us")
print(a)
print(a.total_seconds())

# create 500ns Timedelta - it is not converted successfully to float - result is zero
b = pd.Timedelta("500ns")
print(b)
print(b.total_seconds())

Problem description

Starting with version 1.1.0 Timedelta objects that represent time shorter than 1 microsecond cannot be successfully converted to float -> the result is always zero. This is also present on the current version (1.2.4). when trying same code on pandas 1.0.5, situation is the opposite: Printed output for timedeltas smaller then 1us is bad; it shows all zeros, but converted float value is valid.

Output in pandas 1.0.5:

0 days 00:00:00.000001
1e-06
0 days 00:00:00.000000
5e-07

third line has unexpected value, but it doesn't affect float conversion, as can be seen in the 4th line.

Output in pandas 1.2.4:

0 days 00:00:00.000001
1e-06
0 days 00:00:00.000000500
0.0

now the timedelta representation is fixed, but its float value is 0.0 insteaad of 5e-7

Expected Output

0 days 00:00:00.000001
1e-06
0 days 00:00:00.000000500
5e-7

I am also interested in contributing and helping to fix this issue, if that is needed.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2cb9652
python : 3.8.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Serbian (Latin)_Serbia.1250

pandas : 1.2.4
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 47.1.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@Caballero93 Caballero93 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 14, 2021
@jbrockmendel jbrockmendel added Timedelta Timedelta data type Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Jun 7, 2021
@simonjayhawkins
Copy link
Member

Starting with version 1.1.0 Timedelta objects that represent time shorter than 1 microsecond cannot be successfully converted to float -> the result is always zero.

first bad commit: [3b2c8f6] BUG: nonexistent Timestamp pre-summer/winter DST w/dateutil timezone (#31155)

looks like this was an intentional code change, however, the release note did not mention this.

@deponovo
Copy link
Contributor

Take

@jreback jreback added this to the 1.4 milestone Jan 3, 2022
@jreback
Copy link
Contributor

jreback commented Jan 8, 2022

closed by #45108

@jreback jreback closed this as completed Jan 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Regression Functionality that used to work in a prior pandas version Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants