Skip to content

BUG: Incorrect arithmetic with PeriodIndex #45999

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
khrapovs opened this issue Feb 15, 2022 · 1 comment · Fixed by #46006
Closed
2 of 3 tasks

BUG: Incorrect arithmetic with PeriodIndex #45999

khrapovs opened this issue Feb 15, 2022 · 1 comment · Fixed by #46006
Labels
Bug Numeric Operations Arithmetic, Comparison, and Logical operations Period Period data type
Milestone

Comments

@khrapovs
Copy link

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
periods = pd.period_range(start="2021-01-01", periods=3, freq="2H")
periods - periods[0]

Issue Description

Period index simple arithmetic produces either unexpected, or even wrong results. In the provided example the output is

Index([<0 * Hours>, <4 * Hours>, <8 * Hours>], dtype='object')

Expected Behavior

Using list comprehension instead of PeriodIndex directly:

[v - periods[0] for v in periods]

produces:

[<0 * Hours>, <2 * Hours>, <4 * Hours>]

as expected.

Installed Versions

pd.show_versions()
INSTALLED VERSIONS

commit : 06d2301
python : 3.9.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.13.0-28-generic
Version : #31-Ubuntu SMP Thu Jan 13 17:41:06 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.4.1
numpy : 1.21.4
pytz : 2021.3
dateutil : 2.8.2
pip : 22.0.3
setuptools : 58.4.0
Cython : None
pytest : 6.2.5
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 6.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@khrapovs khrapovs added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 15, 2022
@khrapovs khrapovs changed the title BUG: BUG: Incorrect arithmetic with PeriodIndex Feb 15, 2022
@jbrockmendel jbrockmendel added Numeric Operations Arithmetic, Comparison, and Logical operations Period Period data type and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 15, 2022
@jbrockmendel
Copy link
Member

Thanks for the report.

Best guess is that in PeriodArray._sub_period the line new_data = np.array([self.freq * x for x in new_data]) should be new_data = np.array([self.freq.base * x for x in new_data])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Numeric Operations Arithmetic, Comparison, and Logical operations Period Period data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants