Skip to content

BUG: Flatten pandas MultiIndexes - #5415

Closed
gabrielreid wants to merge 1 commit into
statsmodels:masterfrom
gabrielreid:pandas_multiindex_handling_bug
Closed

gabrielreid wants to merge 1 commit into
statsmodels:masterfrom
gabrielreid:pandas_multiindex_handling_bug

Conversation

@gabrielreid

@gabrielreid gabrielreid commented Dec 10, 2018 •

Copy link
Copy Markdown

Flatten pandas MultiIndexes when extracting column names in the
data module. Prior to this commit, the MultiIndexes would be
exposed as a list of tuples, which would then result in the
DataFrame being treated as having more than 2 dimensions.

closes #5414

Flatten pandas MultiIndexes when extracting column names in the
data module. Prior to this commit, the MultiIndexes would be
exposed as a list of tuples, which would then result in the
DataFrame being treated as having more than 2 dimensions.

Fixes statsmodels#5414.
Comment thread statsmodels/base/data.py
return list(arr.columns)
if isinstance(arr.columns, MultiIndex):
# Flatten MultiIndexes into "simple" column names
return [".".join((level for level in c if level)) for c in arr.columns]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't underline instead of a dot be better as joining character?
I usually use underline to keep it as valid python name (if it already is), but I don't know of any conventions for this.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pandas uses underscore for these scenarios. Like Groupby('z').describe() would create 'x_mean' or when you merge/join it can create x_left and x_right for example.

@josef-pkt

Copy link
Copy Markdown
Member

unit tests fail in statsmodels.iolib.tests.test_table.TestSimpleTable
which actually uses a tuple as names.

I don't know what that test is supposed to be doing, but this needs more investigation.

@josef-pkt

Copy link
Copy Markdown
Member

@gabrielreid Thanks for the report and the PR.

It looks good to me, but given that there is a failing unit test, we need to check whether flattening is the correct fix for this.

@josef-pkt

Copy link
Copy Markdown
Member

temporarily prio-high

@josef-pkt

Copy link
Copy Markdown
Member

Note: this is if the column names are a multi-index, not for observations

The failing unit test is for multi-index endog.
Needs a unit test for multi-index endog, when endog is 2-D.
And a check how MNLogit (and maybe VAR) currently handle multi-index endog.
My guess would be that multi-index endog raises an exception in MNLogit and needs to be fixed also.

@jbrockmendel

Copy link
Copy Markdown
Contributor

MNLogit wrapping is hit or miss isn’t it? And lots of VAR just doesn’t wrap at all.

I’m pretty sure pandas recently added a MultiIndex.to_flat method (or something like that). It’ll be a few years before statsmodels requires pandas >=0.24.0 though

@josef-pkt

Copy link
Copy Markdown
Member

MNLogit has problems in post estimation, but has special code to handle the multivariate endog.
MultinomialModel._handle_data does model specific preprocessing of endog, so maybe multi-index endog columns will not be relevant in MNLogit

@bashtage bashtage self-assigned this May 3, 2019
bashtage added a commit to bashtage/statsmodels that referenced this pull request May 6, 2019
Use _ to make a python name
Clean up for flake compliance

closes statsmodels#5415
@bashtage bashtage mentioned this pull request May 6, 2019
4 tasks done
bashtage added a commit to bashtage/statsmodels that referenced this pull request May 6, 2019
Use _ to make a python name
Clean up for flake compliance

closes statsmodels#5415
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exception in data module when using DataFrames with MultiIndex columns

5 participants