Skip to content

ENH/API: Allow dicts with tuple keys in DataFrame constructor #3323

Closed
@cpcloud

Description

@cpcloud

related #4805

It would be nice to allow automatic conversion to a MultiIndex when a dict with tuple keys is passed into the DataFrame constructor. Here's what currently happens:

from pandas import Index, DataFrame
from numpy.random import rand
import itertools as itools
d = {(i, j): rand(10) for i, j in itools.product(xrange(3), repeat=2)}
df = DataFrame(d)
assert type(df.columns) == Index

The same issue shows up in pd.concat when you pass in a dict of sequences (lists and ndarrays and friends) and axis=1, however if you have a dict of DataFrames the columns keys are converted to a MultiIndex. E.g.,

from pandas import MultiIndex
d = {(i, j): DataFrame(rand(10, 2), columns=['a', 'b']) for i, j in itools.product(xrange(3), repeat=2)}
df = pd.concat(d, axis=1)
assert type(df.columns) == MultiIndex

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions