Skip to content

BUG: Imaginary part of complex numbers gets dropped when using a MultiIndex #42145

@jasonboh

Description

@jasonboh
  • [X ] I have checked that this issue has not already been reported.

  • [X ] 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

df = pd.DataFrame(
    {
        "x": [1 + 2j, 4 - 3j, 10 - 1j, 4 + 5j, 5 - 8j],
        "y": [3, 4, 5, 6, 7],
        "z": [0.1, 0.2, 0.3, 0.4, 0.5],
    }
)
df.set_index(["x", "y"], inplace=True)

Problem description

If creating a MultiIndex, the imaginary part of complex numbers gets dropped.

Update: I did find a workaround that seems to be pretty usable, so not sure how important it is to fix this issue. But, I will leave it here and you can decide. The workaround is the explicitly define the 'x' complex variable as a Series with dtype='object'. That makes the MultiIndex work as expected, with the imag part of the complex number kept.

Here is the workaround code:

df = pd.DataFrame(
{
"x": pd.Series([1 + 2j, 4 - 3j, 10 - 1j, 4 + 5j, 5 - 8j], dtype="object"),
"y": [3, 4, 5, 6, 7],
"z": [0.1, 0.2, 0.3, 0.4, 0.5],
}
)
df.set_index(["x", "y"], inplace=True)

Expected Output

The expected output is shown below, with both x and y included in a MultiIndex, and x maintaining its imaginary part.

df.index

MultiIndex([( 1.0+2j, 3), ( 4.0-3j, 4), (10.0-1j, 5), ( 4.0+5j, 6), ( 5.0-8j, 7)], names=['x', 'y'])

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2cb9652
python : 3.8.9.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.2.4
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 54.2.0
Cython : None
pytest : 6.2.3
hypothesis : None
sphinx : 3.5.4
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
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 : 2.7.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : None
tables : 3.6.1
tabulate : None
xarray : 0.18.2
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions