Skip to content

BUG: modulo of pd.Int64Index returns negative values under some conditions #36526

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
2 of 3 tasks
felixpatzelt opened this issue Sep 21, 2020 · 12 comments
Closed
2 of 3 tasks
Assignees
Labels
Duplicate Report Duplicate issue or pull request Numeric Operations Arithmetic, Comparison, and Logical operations Regression Functionality that used to work in a prior pandas version

Comments

@felixpatzelt
Copy link

felixpatzelt commented Sep 21, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


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

@felixpatzelt felixpatzelt added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 21, 2020
@jorisvandenbossche jorisvandenbossche added Regression Functionality that used to work in a prior pandas version Index Related to the Index class or subclasses Numeric Operations Arithmetic, Comparison, and Logical operations and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member Index Related to the Index class or subclasses labels Sep 21, 2020
@jorisvandenbossche
Copy link
Member

@felixpatzelt Thanks a lot for the report, that's indeed a regression and wrong result ..

It's not specific to Index, also a Series (or DataFrame) operation has the same issue:

In [43]: (pd.Series(list(range(0,24))*500) - 5) % 24
Out[43]: 
0        -5
1        -4
2        -3
3        -2
4        -1
         ..
11995    14
11996    15
11997    16
11998    17
11999    18
Length: 12000, dtype: int64

@jorisvandenbossche jorisvandenbossche added this to the 1.1.3 milestone Sep 21, 2020
@dishak331
Copy link

I would like to work on this

@dishak331
Copy link

take

@jorisvandenbossche
Copy link
Member

@dishak331 Thanks!

See also #35685 in case you might first want to find out which change caused the regression (sometimes that can be helpful)

@asishm
Copy link
Contributor

asishm commented Sep 21, 2020

possibly duplicate of #36047
don't have access to an environment right now

@dishak331
Copy link

@jorisvandenbossche thanks!

@jbrockmendel
Copy link
Member

xref pydata/numexpr#365

@dishak331
Copy link

okay so i just checked, this is a bug in Python itself. When we are doing -5%24 it is outputting as 19 instead of -5

@marcelmindemann
Copy link

marcelmindemann commented Sep 22, 2020

@dishak331 No, it's not a bug in Python. Thats the defined behaviour of the modulo operation in Python 3. It's numexpr that is wrong here by applying C conventions into Python.

@dishak331
Copy link

oh okay okay @m1nde

simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Sep 22, 2020
@simonjayhawkins
Copy link
Member

possibly duplicate of #36047

looks like the same PR responsible, #36047 (comment)

first bad commit: [01623f8] CLN: Refactor pandas/tests/base - part3 (#30147)

https://github.com/simonjayhawkins/pandas/runs/1150901284?check_suite_focus=true

@simonjayhawkins
Copy link
Member

I think can close this as duplicate of #36047

@simonjayhawkins simonjayhawkins added the Duplicate Report Duplicate issue or pull request label Sep 22, 2020
@simonjayhawkins simonjayhawkins removed this from the 1.1.3 milestone Sep 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request Numeric Operations Arithmetic, Comparison, and Logical operations Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

No branches or pull requests

7 participants