Skip to content

Series.dt.round fails if fraction is exactly 0.5 #28408

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
Els-K opened this issue Sep 12, 2019 · 5 comments
Closed

Series.dt.round fails if fraction is exactly 0.5 #28408

Els-K opened this issue Sep 12, 2019 · 5 comments

Comments

@Els-K
Copy link

Els-K commented Sep 12, 2019

Code Sample, a copy-pastable example if possible

rng = pd.date_range('1/1/2018 11:59:00.050', periods=10, freq='100ms')
rng
>>>DatetimeIndex(['2018-01-01 11:59:00.050000', '2018-01-01 11:59:00.150000',
                  '2018-01-01 11:59:00.250000', '2018-01-01 11:59:00.350000',
                  '2018-01-01 11:59:00.450000', '2018-01-01 11:59:00.550000',
                  '2018-01-01 11:59:00.650000', '2018-01-01 11:59:00.750000',
                  '2018-01-01 11:59:00.850000', '2018-01-01 11:59:00.950000'],
                 dtype='datetime64[ns]', freq='100L')
rng.round('100ms')
>>>DatetimeIndex(['2018-01-01 11:59:00', '2018-01-01 11:59:00.200000',
                  '2018-01-01 11:59:00.200000', '2018-01-01 11:59:00.400000',
                  '2018-01-01 11:59:00.400000', '2018-01-01 11:59:00.600000',
                  '2018-01-01 11:59:00.600000', '2018-01-01 11:59:00.800000',
                  '2018-01-01 11:59:00.800000',        '2018-01-01 11:59:01'],
                 dtype='datetime64[ns]', freq=None)

Problem description

If the time fraction is exactly 0.5, rounding half up/half down is applied depending on the digit left of it (even/odd). So, '11:59:00.350' would be rounded half up whilst '11:59:00.450' is rounded half down.

Expected Output

Expected output would be either:

>>>DatetimeIndex(['2018-01-01 11:59:00', '2018-01-01 11:59:00.100000',
                  '2018-01-01 11:59:00.200000', '2018-01-01 11:59:00.300000',
                  '2018-01-01 11:59:00.400000', '2018-01-01 11:59:00.500000',
                  '2018-01-01 11:59:00.600000', '2018-01-01 11:59:00.700000',
                  '2018-01-01 11:59:00.800000', '2018-01-01 11:59:00.900000'],
                 dtype='datetime64[ns]', freq=None)

or:

>>> DatetimeIndex(['2018-01-01 11:59:00.100000', '2018-01-01 11:59:00.200000',
                  '2018-01-01 11:59:00.300000', '2018-01-01 11:59:00.400000',
                  '2018-01-01 11:59:00.500000', '2018-01-01 11:59:00.600000',
                  '2018-01-01 11:59:00.700000', '2018-01-01 11:59:00.800000',
                  '2018-01-01 11:59:00.900000',        '2018-01-01 11:59:01'],
                 dtype='datetime64[ns]', freq=None)

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
INSTALLED VERSIONS

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

pandas : 0.25.1
numpy : 1.16.4
pytz : 2019.2
dateutil : 2.8.0
pip : 19.2.2
setuptools : 41.0.1
Cython : 0.29.13
pytest : 5.0.1
hypothesis : None
sphinx : 2.1.2
blosc : None
feather : None
xlsxwriter : 1.1.8
lxml.etree : 4.4.1
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.8.0
pandas_datareader: None
bs4 : 4.8.0
bottleneck : 1.2.1
fastparquet : None
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.1.1
numexpr : 2.7.0
odfpy : None
openpyxl : 2.6.2
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.3.1
sqlalchemy : 1.3.7
tables : 3.5.2
xarray : 0.12.1
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.1.8

@TomAugspurger
Copy link
Contributor

I haven't looked at the example, but is this just two's-complement rounding? https://en.wikipedia.org/wiki/Two%27s_complement

@datajanko
Copy link
Contributor

datajanko commented Sep 12, 2019

Actually, this is the same as the python rounding behavior

@mroeschke
Copy link
Member

This was an intentional change in 0.24.0: https://pandas.pydata.org/pandas-docs/version/0.24.2/whatsnew/v0.24.0.html#datetimelike

Here was the PR with the behavior fix.#22802

@Els-K
Copy link
Author

Els-K commented Sep 16, 2019 via email

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Sep 16, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants