Skip to content

Exception in data module when using DataFrames with MultiIndex columns #5414

Closed

Description

@gabrielreid

The data module appears to fail when working with DataFrames that use a MultiIndex for columns. This can be easily demonstrated in statsmodels.base.tests.test_data.TestDataFrames by changing the creation of a DataFrame from

exog =  pandas.DataFrame(np.random.random((10,2)),
                                 columns=['x_1','x_2'])

to

exog = pandas.DataFrame(np.random.random((10,2)),
                                 columns=pandas.MultiIndex.from_product([['x'], ['1', '2']]))

Using MultiIndexes for DataFrame columns then propagates itself to other places because using the DataFrame.columns value as a simple list doesn't work (in a MultiIndex, DataFrame.columns is exposed as an iterable of tuples). The specific issue I ran into was when using a MultiIndex DataFrame with exponential smoothing, resulting in the following stack trace:

Traceback (most recent call last):
  File "xxx/__init__.py", line 451, in do_algorithm_usecase
    if af.compute():
  File "xxx/algorithms/es.py", line 153, in compute
    preds = self.fitted_model.predict(predict_from, predict_until)
  File "xxx/myenv/lib/python3.7/site-packages/statsmodels/base/wrapper.py", line 95, in wrapper
    obj = data.wrap_output(func(results, *args, **kwargs), how)
  File "xxx/myenv/lib/python3.7/site-packages/statsmodels/base/data.py", line 416, in wrap_output
    return self.attach_dates(obj)
  File "xxx/myenv/lib/python3.7/site-packages/statsmodels/base/data.py", line 563, in attach_dates
    columns=self.ynames)
  File "xxx/myenv/lib/python3.7/site-packages/pandas/core/frame.py", line 379, in __init__
    copy=copy)
  File "xxx/myenv/lib/python3.7/site-packages/pandas/core/frame.py", line 536, in _init_ndarray
    return create_block_manager_from_blocks([values], [columns, index])
  File "xxx/myenv/lib/python3.7/site-packages/pandas/core/internals.py", line 4866, in create_block_manager_from_blocks
    construction_error(tot_items, blocks[0].shape[1:], axes, e)
  File "xxx/myenv/lib/python3.7/site-packages/pandas/core/internals.py", line 4843, in construction_error
    passed, implied))
ValueError: Shape of passed values is (1, 3), indices imply (3, 3)

Activity

  1. added a commit that references this issue on Dec 10, 2018
    d28bc8a
  2. gabrielreid commented on Dec 10, 2018

    @gabrielreid
    Author

    Forgot to mention this previously, but this is with version 0.9.0 (and also verified it's still a problem in the master branch in Git).

  3. gabrielreid commented on Dec 10, 2018

    @gabrielreid
    Author

    I've opened PR #5415 to resolve this issue.

  4. added 2 commits that reference this issue on May 6, 2019
    e325032
    815460e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions