Skip to content

BUG: index level of Series dropped if not containing >1 unique value in numeric operations in non-commutative way #45224

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
brandonskinner-8451 opened this issue Jan 6, 2022 · 0 comments · Fixed by #45564
Labels
Bug MultiIndex Numeric Operations Arithmetic, Comparison, and Logical operations
Milestone

Comments

@brandonskinner-8451
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 master branch of pandas.

Reproducible Example

# Series where level foo has only one unique value
a1 = pd.DataFrame(
    {
        'foo': [1, 1, 1, 1],
        'bar': [4, 4, 5, 5],
        'baz': [6, 7] * 2,
        'value': [100 * i for i in range(4)],
    }
).set_index(['foo', 'bar', 'baz']).value

# Series where level foo has more than one unique value
a2 = pd.DataFrame(
    {
        'foo': [1] * 4 + [2] * 4,
        'bar': [4, 4, 5, 5] * 2,
        'baz': [6, 7] * 4,
        'value': [100 * i for i in range(8)],
    }
).set_index(['foo', 'bar', 'baz']).value

# Other series
b = pd.DataFrame(
    {
        'bar': [4, 4, 5, 5],
        'baz': [6, 7, 6, 7],
        'value': [500, 600, 700, 0],
    }
).set_index(['bar', 'baz']).value

# See Expected Behavior for the conditions these Series were expected to pass

Issue Description

When using numeric operations between Series (esp. mathematically commutative ones), I observed non-commutative behavior in their retention of multi-index levels.

Say we have a Series ("a") with three index levels, and another ("b") with only two which are shared with the first.

If the former series third (unshared) level has >1 unique value within it, a * b and b * a both retain all three levels of the index.

However, if the unshared index level of "a" only has a single unique value, that index level is dropped in b * a but not a * b.

This value-dependent dropping of the level was unexpected behavior in my estimation.

Expected Behavior

Expected behavior is foo to not be dropped in Series multiplication, and therefore for all three of the following to pass:

# Passes because foo in a2 has >1 unique value
pd.testing.assert_index_equal(
    (a2 * b).index, (b * a2).index
)

# Fails as level foo is dropped in b * a1
pd.testing.assert_index_equal(
    (a1 * b).index, (b * a1).index
)

# Fails as level foo is dropped in b * a1
#     (a.rmul(b) is expected to equal b * a per its docs)
pd.testing.assert_index_equal(
    a1.rmul(b).index, (b * a1).index
)

Installed Versions

INSTALLED VERSIONS

commit : 66e3805
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-1063-azure
Version : #66~18.04.1-Ubuntu SMP Thu Oct 21 09:59:28 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.5
numpy : 1.19.2
pytz : 2020.5
dateutil : 2.8.1
pip : 21.0.1
setuptools : 52.0.0
Cython : 0.29.23
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.7.1
html5lib : None
pymysql : None
psycopg2 : 2.8.5 (dt dec pq3 ext lo64)
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 4.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@brandonskinner-8451 brandonskinner-8451 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 6, 2022
@mroeschke mroeschke added MultiIndex Numeric Operations Arithmetic, Comparison, and Logical operations and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 13, 2022
@jreback jreback added this to the 1.5 milestone Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug MultiIndex Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants