Skip to content

ENH: set multi-index names as NamedTuples #8263

Closed
@dr-leo

Description

@dr-leo

import pandas as PD
s1=PD.Series((1,4,9,16), name=dict(a=1,b=2))
s2=s1*2
s2.name=dict(c=5,d=7)
s1
0 1
1 4
2 9
3 16
Name: {'a': 1, 'b': 2}, dtype: int32
df = PD.concat((s1,s2), axis=1) # this should raise ValueError as the resulting df is broken
df.shape # ok, this works...
(4, 2)
df # But this doesn't.
<repr(<pandas.core.frame.DataFrame at 0x35af770>) failed: TypeError: unhashable type: 'dict'>
df.columns # This is not a proper index...
Index([{'a': 1, 'b': 2}, {'c': 5, 'd': 7}], dtype='object')

Comments:

  1. There may be other ways aside from concat that lead to broken df's if a Series.name attribute is unhashable.
  2. If Series.name is a dict, one could try to convert it into a namedtuple. That's hashable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Compatpandas objects compatability with Numpy or Python functionsDtype ConversionsUnexpected or buggy dtype conversions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions