Skip to content

Rounding errors when parsing timedeltas #12690

Closed
@belteshassar

Description

@belteshassar

Found this while working on #12136.

Rounding errors occur for timedeltas initialized using fractional microsecond values:

>>> pd.Timedelta('12.312us') == pd.Timedelta(12312)
False
>>> pd.Timedelta(12.312, unit='us') == pd.Timedelta(12312)
False
>>> pd.Timedelta('12.312us') == pd.Timedelta(12311)
True

Additionally, precision is lost when initializing timedeltas with fractional millisecond (and probably second) values:

>>> pd.Timedelta(0.012312, unit='ms') == pd.Timedelta(12311)
False
>>> pd.Timedelta(0.012311, unit='ms') == pd.Timedelta(12311)
False
>>> pd.Timedelta(0.012311, unit='ms') == pd.Timedelta(12000)
True

I believe the problem is here: https://github.com/pydata/pandas/blob/288059adfb751d52be57e444198198518d256c05/pandas/tslib.pyx#L3399
bumping up the values of p should probably solve this.

output of pd.show_versions()

commit: None
python: 2.7.11.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0+unknown
nose: 1.3.7
pip: 8.1.0
setuptools: 20.2.2
Cython: 0.23.4
numpy: 1.10.4
scipy: 0.17.0
statsmodels: None
xarray: None
IPython: 4.1.2
sphinx: 1.3.5
patsy: 0.4.0
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: 1.0.0
tables: 3.2.2
numexpr: 2.4.6
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.8.4
lxml: 3.5.0
bs4: 4.3.2
html5lib: 0.999
httplib2: None
apiclient: None
sqlalchemy: 1.0.12
pymysql: 0.6.7.None
psycopg2: None
jinja2: 2.8
boto: None

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