Skip to content

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

Closed
@brandonskinner-8451

Description

@brandonskinner-8451

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions