Skip to content

MultiIndex level dropped when multiplying two series with a single entry #25891

@mellesies

Description

@mellesies

When multiplying two Series with overlapping MultiIndices and a single entry, pandas drops levels from the MultiIndex. This doesn't happen when either Series contains multiple entries.

Using pandas 0.24.1 and Numpy 1.16.2

This works fine

# Create Series s1, with a scope over {T, N} and set two values
index1 = pd.MultiIndex.from_product([[], []], names=['T', 'N'])
s1 = pd.Series(index=index1)
s1['T.1A', 'N.0'] = 0.5
s1['T.1B', 'N.0'] = 0.5

# Create Series s2 with a scope over {N, M} and set a single value
index2 = pd.MultiIndex.from_product([[], []], names=['N', 'M'])
s2 = pd.Series(index=index2)
s2['N.0', 'M.0'] = 0.5

# When multiplying s1 and s2 pandas will align the Series using the index.
# Note that the index has 3 levels: {N, T, M}
print(s1 * s2)

# Prints:
# N    T     M  
# N.0  T.1A  M.0    0.25
#      T.1B  M.0    0.25
# dtype: float64

This doesn't return the expected index levels

# Create Series s1, with a scope over {T, N} and set a single value
index1 = pd.MultiIndex.from_product([[], []], names=['T', 'N'])
s1 = pd.Series(index=index1)
s1['T.1A', 'N.0'] = 0.5

# Create Series s2 with a scope over {N, M} and set a single value
index2 = pd.MultiIndex.from_product([[], []], names=['N', 'M'])
s2 = pd.Series(index=index2)
s2['N.0', 'M.0'] = 0.5

# Multiply s1 and s2. Correctly yields 0.25. But where did index level 'M' go!?
print(s1 * s2)

# Prints:
# T     N  
# T.1A  N.0    0.25
# dtype: float64

Problem description

Index level 'M' is dropped in the result. It appears the index from the first Series, s1 is carried over?

Expected Output

I'd expect the result to have an index that is essentially the union of the indices from s1 and s2.

# N    T     M  
# N.0  T.1A  M.0    0.25
# dtype: float64

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.8.final.0
python-bits: 64
OS: Darwin
OS-release: 18.2.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_US.utf-8
LANG: None
LOCALE: en_US.UTF-8

pandas: 0.24.1
pytest: None
pip: 19.0.3
setuptools: 40.6.2
Cython: None
numpy: 1.16.2
scipy: None
pyarrow: None
xarray: None
IPython: 7.3.0
sphinx: None
patsy: None
dateutil: 2.8.0
pytz: 2018.9
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions