Skip to content

bug in repeat() method with MultiIndex - fixed in 0.15 #9361

Closed
@glyg

Description

@glyg

Hi,

I found a bug in version 0.14, which has since been fixed in 0.15. I thought I'd report it because I couldn't find a mention of it and it might have been fixed silently. Also I couldn't find a specific test for this bug (as far as I could see).

Here is a test that fails with the bug:

import pandas as pd
print(pd.version.version)

def test_multiindex_repeat():
    idx = pd.Index([1, 2, 3, 4])
    m_idx = pd.MultiIndex.from_tuples([(1, 2), (3, 4),
                                       (5, 6), (7, 8)])
    data = ['a', 'b', 'c', 'd']

    df = pd.Series(data, index=idx)
    m_df = pd.Series(data, index=m_idx)

    assert df.repeat(3).shape == (3 * len(data),)
    assert m_df.repeat(3).shape == (3 * len(data),)

test_multiindex_repeat()

With pandas 0.14, here is the backtrace:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-7fcd65f0e025> in <module>()
----> 1 assert m_df.repeat(3).shape == (3 * len(data),)

/home/guillaume/anaconda/envs/pelican/lib/python2.7/site-packages/pandas/core/series.pyc in repeat(self, reps)
    705         new_values = self.values.repeat(reps)
    706         return self._constructor(new_values,
--> 707                                  index=new_index).__finalize__(self)
    708 
    709     def reshape(self, *args, **kwargs):

/home/guillaume/anaconda/envs/pelican/lib/python2.7/site-packages/pandas/core/series.pyc in __init__(self, data, index, dtype, name, copy, fastpath)
    229                                        raise_cast_failure=True)
    230 
--> 231                 data = SingleBlockManager(data, index, fastpath=True)
    232 
    233         generic.NDFrame.__init__(self, data, fastpath=True)

/home/guillaume/anaconda/envs/pelican/lib/python2.7/site-packages/pandas/core/internals.pyc in __init__(self, block, axis, do_integrity_check, fastpath)
   3028             block = make_block(block,
   3029                                placement=slice(0, len(axis)),
-> 3030                                ndim=1, fastpath=True)
   3031 
   3032         self.blocks = [block]

/home/guillaume/anaconda/envs/pelican/lib/python2.7/site-packages/pandas/core/internals.pyc in make_block(values, placement, klass, ndim, dtype, fastpath)
   1835 
   1836     return klass(values, ndim=ndim, fastpath=fastpath,
-> 1837                  placement=placement)
   1838 
   1839 

/home/guillaume/anaconda/envs/pelican/lib/python2.7/site-packages/pandas/core/internals.pyc in __init__(self, values, ndim, fastpath, placement)
   1231         super(ObjectBlock, self).__init__(values, ndim=ndim,
   1232                                           fastpath=fastpath,
-> 1233                                           placement=placement)
   1234 
   1235     @property

/home/guillaume/anaconda/envs/pelican/lib/python2.7/site-packages/pandas/core/internals.pyc in __init__(self, values, placement, ndim, fastpath)
     72             raise ValueError('Wrong number of items passed %d,'
     73                              ' placement implies %d' % (
---> 74                                  len(self.values), len(self.mgr_locs)))
     75 
     76     @property

ValueError: Wrong number of items passed 12, placement implies 4

Shall I make a PR adding this test to test_multiindex or is it not worth the (minimal) work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Testingpandas testing functions or related to the test suite

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions