I would like to create an empty DataFrame with a MultiIndex before assigning rows to it. I already found that empty DataFrames don't like to be assigned MultiIndexes on the fly, so I'm setting the MultiIndex names during creation. However, I don't want to assign levels, as this will be done later. This is the best code I got to so far:
def empty_multiindex(names): """ Creates empty MultiIndex from a list of level names. """ return MultiIndex.from_tuples(tuples=[(None,) * len(names)], names=names)
Which gives me
In : empty_multiindex(['one','two', 'three']) Out: MultiIndex(levels=[, , ], labels=[[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]], names=[u'one', u'two', u'three'])
In : DataFrame(index=empty_multiindex(['one','two', 'three'])) Out: one two three NaN NaN NaN
Well, I have no use for these NaNs. I can easily drop them later, but this is obviously a hackish solution. Anyone has a better one?