Skip to content

BUG: df.shift(axis=1) complains about index out of range #7978

Closed
@alan-wong

Description

@alan-wong

I just tried to pass axis=1 on a shift call to a dataframe and it threw an error, I couldn't find an issue here and this is the first time I tried this function so not sure if this was something that used to work and then got broken.

I'm using python 64-bit 3.3.4, pandas 0.14.1 and numpy 1.8.1

In [57]:

df = pd.DataFrame({'a':randn(10), 'b':randn(10)})
df.shift(axis=1)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-57-160107eee7e4> in <module>()
      1 df = pd.DataFrame({'a':randn(10), 'b':randn(10)})
----> 2 df.shift(axis=1)

C:\WinPython-64bit-3.3.5.0\python-3.3.5.amd64\lib\site-packages\pandas\core\generic.py in shift(self, periods, freq, axis, **kwds)
   3173             block_axis = self._get_block_manager_axis(axis)
   3174             indexer = com._shift_indexer(len(self), periods)
-> 3175             new_data = self._data.shift(indexer, periods, axis=block_axis)
   3176         else:
   3177             return self.tshift(periods, freq, **kwds)

C:\WinPython-64bit-3.3.5.0\python-3.3.5.amd64\lib\site-packages\pandas\core\internals.py in shift(self, *args, **kwargs)
   2405 
   2406     def shift(self, *args, **kwargs):
-> 2407         return self.apply('shift', *args, **kwargs)
   2408 
   2409     def fillna(self, *args, **kwargs):

C:\WinPython-64bit-3.3.5.0\python-3.3.5.amd64\lib\site-packages\pandas\core\internals.py in apply(self, f, *args, **kwargs)
   2373 
   2374             else:
-> 2375                 applied = getattr(blk, f)(*args, **kwargs)
   2376 
   2377             if isinstance(applied, list):

C:\WinPython-64bit-3.3.5.0\python-3.3.5.amd64\lib\site-packages\pandas\core\internals.py in shift(self, indexer, periods, axis)
    916         """ shift the block by periods, possibly upcast """
    917 
--> 918         new_values = self.values.take(indexer, axis=axis)
    919         # convert integer to float if necessary. need to do a lot more than
    920         # that, handle boolean etc also

IndexError: index 2 is out of bounds for size 2
In [57]:

df = pd.DataFrame({'a':randn(10), 'b':randn(10)})
df.shift(axis=1)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-57-160107eee7e4> in <module>()
      1 df = pd.DataFrame({'a':randn(10), 'b':randn(10)})
----> 2 df.shift(axis=1)

C:\WinPython-64bit-3.3.5.0\python-3.3.5.amd64\lib\site-packages\pandas\core\generic.py in shift(self, periods, freq, axis, **kwds)
   3173             block_axis = self._get_block_manager_axis(axis)
   3174             indexer = com._shift_indexer(len(self), periods)
-> 3175             new_data = self._data.shift(indexer, periods, axis=block_axis)
   3176         else:
   3177             return self.tshift(periods, freq, **kwds)

C:\WinPython-64bit-3.3.5.0\python-3.3.5.amd64\lib\site-packages\pandas\core\internals.py in shift(self, *args, **kwargs)
   2405 
   2406     def shift(self, *args, **kwargs):
-> 2407         return self.apply('shift', *args, **kwargs)
   2408 
   2409     def fillna(self, *args, **kwargs):

C:\WinPython-64bit-3.3.5.0\python-3.3.5.amd64\lib\site-packages\pandas\core\internals.py in apply(self, f, *args, **kwargs)
   2373 
   2374             else:
-> 2375                 applied = getattr(blk, f)(*args, **kwargs)
   2376 
   2377             if isinstance(applied, list):

C:\WinPython-64bit-3.3.5.0\python-3.3.5.amd64\lib\site-packages\pandas\core\internals.py in shift(self, indexer, periods, axis)
    916         """ shift the block by periods, possibly upcast """
    917 
--> 918         new_values = self.values.take(indexer, axis=axis)
    919         # convert integer to float if necessary. need to do a lot more than
    920         # that, handle boolean etc also

IndexError: index 2 is out of bounds for size 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions