Skip to content
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

BUG:Floating point accuracy with DatetimeIndex.round (#14440) #15568

Closed
wants to merge 1 commit into from

Conversation

mroeschke
Copy link
Member

Employs @eoincondron's fix for float point inaccuracies when rounding by milliseconds for DatetimeIndex.round and Timestamp.round

@jreback jreback added Bug Datetime Datetime data dtype labels Mar 4, 2017
@jreback jreback added this to the 0.20.0 milestone Mar 4, 2017
@@ -175,6 +175,17 @@ def test_round(self):
tm.assertRaisesRegexp(ValueError, msg, rng.round, freq='M')
tm.assertRaisesRegexp(ValueError, msg, elt.round, freq='M')

# GH 14440
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also round to us/ns here as well (which should equal the original)

as an aside, you can now do parametrized tests (but have to move them to separate functions and not class based)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the rounding is happening correctly for microseconds but not with nanoseconds:

In [7]: pd.DatetimeIndex(['2016-10-17 12:00:00.0015']).round('ns')
Out[7]: DatetimeIndex(['2016-10-17 12:00:00.001499904'], dtype='datetime64[ns]', freq=None)

The rounding methodology seems sound. I am unsure if this is a limitation of the date going from int64 to float64 to int64 as this is essentially what is happening:

(Pdb) np.round(np.array([1476705600001500000])/1.).astype('i8')
array([1476705600001499904])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this is just losing precision - not sure much can be done

we could warn / raise potentially though
would that be useful?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created a new issue. #15578

@jreback
Copy link
Contributor

jreback commented Mar 4, 2017

small testing addition. ping when pushed / green.

@codecov-io
Copy link

Codecov Report

Merging #15568 into master will decrease coverage by -0.03%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master   #15568      +/-   ##
==========================================
- Coverage   91.05%   91.03%   -0.03%     
==========================================
  Files         137      137              
  Lines       49291    49291              
==========================================
- Hits        44883    44873      -10     
- Misses       4408     4418      +10
Impacted Files Coverage Δ
pandas/tseries/base.py 96.59% <100%> (ø)
pandas/io/gbq.py 25% <0%> (-58.34%)
pandas/tools/merge.py 91.78% <0%> (-0.35%)
pandas/core/frame.py 97.83% <0%> (-0.1%)
pandas/core/common.py 91.36% <0%> (+0.33%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d652485...c5a7cbc. Read the comment docs.

@jreback jreback closed this in 5067708 Mar 5, 2017
@jreback
Copy link
Contributor

jreback commented Mar 5, 2017

thanks!

AnkurDedania pushed a commit to AnkurDedania/pandas that referenced this pull request Mar 21, 2017
closes pandas-dev#14440

Employs @eoincondron's fix for float point inaccuracies when rounding
by milliseconds for `DatetimeIndex.round` and `Timestamp.round`

Author: Matt Roeschke <emailformattr@gmail.com>

Closes pandas-dev#15568 from mroeschke/fix_14440 and squashes the following commits:

c5a7cbc [Matt Roeschke] BUG:Floating point accuracy with DatetimeIndex.round (pandas-dev#14440)
@mroeschke mroeschke deleted the fix_14440 branch December 20, 2017 02:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Floating point accuracy problems in DatetimeIndex.round
3 participants