Skip to content

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

Closed
@Els-K

Description

@Els-K

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions