Skip to content

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

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
1 task
jasonboh opened this issue Jun 20, 2021 · 2 comments · Fixed by #43184
Closed
1 task

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

jasonboh opened this issue Jun 20, 2021 · 2 comments · Fixed by #43184
Labels

Comments

@jasonboh
Copy link

jasonboh commented Jun 20, 2021

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

@jasonboh jasonboh added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 20, 2021
@jasonboh jasonboh changed the title BUG: BUG: Imaginary part of complex numbers gets dropped when using a MultiIndex Jun 20, 2021
@jbrockmendel jbrockmendel added Complex Complex Numbers MultiIndex labels Jun 22, 2021
@mroeschke mroeschke removed the Needs Triage Issue that has not been reviewed by a pandas team member label Aug 21, 2021
@debnathshoham
Copy link
Member

I am getting the below on master. IIUC, this is the expected output?

In [1]: import pandas as pd

In [2]: pd.__version__
Out[2]: '1.4.0.dev0+485.ge8f359348a'

In [3]: 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)

In [4]: df.index
Out[4]: 
MultiIndex([( (1+2j), 3),
            ( (4-3j), 4),
            ((10-1j), 5),
            ( (4+5j), 6),
            ( (5-8j), 7)],
           names=['x', 'y'])

@jasonboh
Copy link
Author

@debnathshoham yes, that does look correct to me. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants