Skip to content

pivot and pivot_table not able to handle complex numbers #25221

Open
@Haigutus

Description

@Haigutus

Code Sample, a copy-pastable example if possible

# Your code here
>>> branches
KEY          Terminal.ConductingEquipment           SvVoltage.TopologicalNode_x           SvVoltage.TopologicalNode_y                      Branch.y
1    d8f14106-d89c-4779-bd29-18ad4b4e671d  a94e594e-46c8-4cb3-b3d1-f412715bbdb4  ff1fb999-6411-4776-8ce9-0a875afed1ea  (0.009999999999999998-0.03j)
2    d8f14106-d89c-4779-bd29-18ad4b4e671d  ff1fb999-6411-4776-8ce9-0a875afed1ea  a94e594e-46c8-4cb3-b3d1-f412715bbdb4  (0.009999999999999998-0.03j)

# Works OK with strings

>>> branches[["Terminal.ConductingEquipment","SvVoltage.TopologicalNode_x", "SvVoltage.TopologicalNode_y"]].pivot(index = "SvVoltage.TopologicalNode_x", columns = "SvVoltage.TopologicalNode_y")
KEY                                           Terminal.ConductingEquipment                                      
SvVoltage.TopologicalNode_y           a94e594e-46c8-4cb3-b3d1-f412715bbdb4  ff1fb999-6411-4776-8ce9-0a875afed1ea
SvVoltage.TopologicalNode_x                                                                                     
a94e594e-46c8-4cb3-b3d1-f412715bbdb4                                   NaN  d8f14106-d89c-4779-bd29-18ad4b4e671d
ff1fb999-6411-4776-8ce9-0a875afed1ea  d8f14106-d89c-4779-bd29-18ad4b4e671d                                   NaN

# Fails with complex

>>> branches[["Branch.y","SvVoltage.TopologicalNode_x", "SvVoltage.TopologicalNode_y"]].pivot(index = "SvVoltage.TopologicalNode_x", columns = "SvVoltage.TopologicalNode_y")
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Users\kristjan.vilgo\AppData\Roaming\Python\Python27\site-packages\pandas\core\frame.py", line 5194, in pivot
    return pivot(self, index=index, columns=columns, values=values)
  File "C:\Users\kristjan.vilgo\AppData\Roaming\Python\Python27\site-packages\pandas\core\reshape\reshape.py", line 415, in pivot
    return indexed.unstack(columns)
  File "C:\Users\kristjan.vilgo\AppData\Roaming\Python\Python27\site-packages\pandas\core\frame.py", line 5534, in unstack
    return unstack(self, level, fill_value)
  File "C:\Users\kristjan.vilgo\AppData\Roaming\Python\Python27\site-packages\pandas\core\reshape\reshape.py", line 495, in unstack
    return _unstack_frame(obj, level, fill_value=fill_value)
  File "C:\Users\kristjan.vilgo\AppData\Roaming\Python\Python27\site-packages\pandas\core\reshape\reshape.py", line 516, in _unstack_frame
    return unstacker.get_result()
  File "C:\Users\kristjan.vilgo\AppData\Roaming\Python\Python27\site-packages\pandas\core\reshape\reshape.py", line 184, in get_result
    values, _ = self.get_new_values()
  File "C:\Users\kristjan.vilgo\AppData\Roaming\Python\Python27\site-packages\pandas\core\reshape\reshape.py", line 248, in get_new_values
    f = getattr(_reshape, "unstack_{name}".format(name=name))
AttributeError: 'module' object has no attribute 'unstack_complex128'

# Same with pivot_table

>>> branches[["Branch.y","SvVoltage.TopologicalNode_x", "SvVoltage.TopologicalNode_y"]].pivot_table(index = "SvVoltage.TopologicalNode_x", columns = "SvVoltage.TopologicalNode_y")
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Users\kristjan.vilgo\AppData\Roaming\Python\Python27\site-packages\pandas\core\frame.py", line 5303, in pivot_table
    margins_name=margins_name)
  File "C:\Users\kristjan.vilgo\AppData\Roaming\Python\Python27\site-packages\pandas\core\reshape\pivot.py", line 114, in pivot_table
    table = agged.unstack(to_unstack)
  File "C:\Users\kristjan.vilgo\AppData\Roaming\Python\Python27\site-packages\pandas\core\frame.py", line 5534, in unstack
    return unstack(self, level, fill_value)
  File "C:\Users\kristjan.vilgo\AppData\Roaming\Python\Python27\site-packages\pandas\core\reshape\reshape.py", line 495, in unstack
    return _unstack_frame(obj, level, fill_value=fill_value)
  File "C:\Users\kristjan.vilgo\AppData\Roaming\Python\Python27\site-packages\pandas\core\reshape\reshape.py", line 516, in _unstack_frame
    return unstacker.get_result()
  File "C:\Users\kristjan.vilgo\AppData\Roaming\Python\Python27\site-packages\pandas\core\reshape\reshape.py", line 184, in get_result
    values, _ = self.get_new_values()
  File "C:\Users\kristjan.vilgo\AppData\Roaming\Python\Python27\site-packages\pandas\core\reshape\reshape.py", line 248, in get_new_values
    f = getattr(_reshape, "unstack_{name}".format(name=name))
AttributeError: 'module' object has no attribute 'unstack_complex128'

Problem description

pivot and pivot_table not able to handle complex numbers

Expected Output

pivoted table, as demonstrated above with string values, but with complex values

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

pandas.show_versions()

INSTALLED VERSIONS

commit: None
python: 2.7.13.final.0
python-bits: 32
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.4
pytest: None
pip: 7.1.2
setuptools: 28.8.0
Cython: None
numpy: 1.15.4
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 5.4.1
sphinx: None
patsy: None
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.0
openpyxl: 2.4.5
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: None
lxml: 4.2.1
bs4: 4.6.3
html5lib: 0.999999999
sqlalchemy: 1.2.15
pymysql: 0.9.3
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugComplexComplex NumbersReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions