BUG: modulo of pd.Int64Index returns negative values under some conditions · Issue #36526 · pandas-dev/pandas (original) (raw)


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 (pd.Int64Index(list(range(0,24))*500) - 5) % 24

returns Int64Index([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4,

...

9, 10, 11, 12, 13, 14, 15, 16, 17, 18],

dtype='int64', length=12000)

note: (pd.Int64Index(list(range(0,24))*400) - 5) % 24 does not return negative values

Problem description

In Pandas 1.1.2, taking the modulo of an Int64Index can return negative values depending on the values in the index and on its length. This problem was not present in pandas 1.0.5. It is also inconsistent with the generally expected behaviour of modulo in python and numpy.

Expected Output

Int64Index([19, 20, 21, 22, 23,  0,  1,  2,  3,  4,
            ...
             9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
           dtype='int64', length=9600)

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2a7d332
python : 3.7.5.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.141.1~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.1.2
numpy : 1.19.2
pytz : 2020.1
dateutil : 2.8.1
pip : 19.2.3
setuptools : 41.2.0
Cython : None
pytest : 6.0.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : 0.10.1
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.11.2
IPython : 7.18.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 0.8.2
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 1.0.1
pytables : None
pyxlsb : None
s3fs : 0.4.2
scipy : 1.5.2
sqlalchemy : 1.3.19
tables : 3.6.1
tabulate : None
xarray : 0.16.1
xlrd : None
xlwt : None
numba : 0.51.2