Skip to content

PERF: Quick Shift Implementation (GH5609) #6672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ Improvements to existing features
- Support passing ``encoding`` with xlwt (:issue:`3710`)
- Performance improvement when converting ``DatetimeIndex`` to floating ordinals
using ``DatetimeConverter`` (:issue:`6636`)

- Performance improvement for ``DataFrame.shift`` (:issue: `5609`)

.. _release.bug_fixes-0.14.0:

Bug Fixes
Expand Down
12 changes: 0 additions & 12 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2003,18 +2003,6 @@ def intersection(*seqs):
return type(seqs[0])(list(result))


def _shift_indexer(N, periods):
# small reusable utility
indexer = np.zeros(N, dtype=int)

if periods > 0:
indexer[periods:] = np.arange(N - periods)
else:
indexer[:periods] = np.arange(-periods, N)

return indexer


def _asarray_tuplesafe(values, dtype=None):
from pandas.core.index import Index

Expand Down
4 changes: 1 addition & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3222,9 +3222,7 @@ def shift(self, periods=1, freq=None, axis=0, **kwds):
return self

if freq is None and not len(kwds):
block_axis = self._get_block_manager_axis(axis)
indexer = com._shift_indexer(len(self._get_axis(axis)), periods)
new_data = self._data.shift(indexer=indexer, periods=periods, axis=block_axis)
new_data = self._data.shift(periods=periods, axis=axis)
else:
return self.tshift(periods, freq, **kwds)

Expand Down
21 changes: 12 additions & 9 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,23 +961,20 @@ def diff(self, n):
return [make_block(new_values, self.items, self.ref_items,
ndim=self.ndim, fastpath=True)]

def shift(self, indexer, periods, axis=0):
def shift(self, periods, axis=0):
""" shift the block by periods, possibly upcast """

new_values = self.values.take(indexer, axis=axis)
# convert integer to float if necessary. need to do a lot more than
# that, handle boolean etc also
new_values, fill_value = com._maybe_upcast(new_values)

new_values, fill_value = com._maybe_upcast(self.values)
new_values = np.roll(new_values.T,periods,axis=axis)
axis_indexer = [ slice(None) ] * self.ndim
if periods > 0:
axis_indexer[axis] = slice(None,periods)
else:
axis_indexer = [ slice(None) ] * self.ndim
axis_indexer[axis] = slice(periods,None)
new_values[tuple(axis_indexer)] = fill_value

return [make_block(new_values, self.items, self.ref_items,
return [make_block(new_values.T, self.items, self.ref_items,
ndim=self.ndim, fastpath=True)]

def eval(self, func, other, raise_on_error=True, try_cast=False):
Expand Down Expand Up @@ -1910,9 +1907,15 @@ def fillna(self, value, limit=None, inplace=False, downcast=None):
values = self.values if inplace else self.values.copy()
return [self.make_block(values.get_values(value), fill_value=value)]

def shift(self, indexer, periods, axis=0):

def shift(self, periods, axis=0):
""" shift the block by periods """

N = len(self.values.T)
indexer = np.zeros(N, dtype=int)
if periods > 0:
indexer[periods:] = np.arange(N - periods)
else:
indexer[:periods] = np.arange(-periods, N)
new_values = self.values.to_dense().take(indexer)
# convert integer to float if necessary. need to do a lot more than
# that, handle boolean etc also
Expand Down
13 changes: 13 additions & 0 deletions vb_suite/frame_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,16 @@ def test_unequal(name):
setup,
start_date=datetime(2014, 2, 7))


#-------------------------------------------------------------------------
# frame shift speedup issue-5609

setup = common_setup + """
df = pd.DataFrame(np.random.rand(10000,500))
"""
frame_shift_axis0 = Benchmark('df.shift(1,axis=0)', setup,
name = 'frame_shift_axis_0',
start_date=datetime(2014,1,1))
Copy link
Contributor

Choose a reason for hiding this comment

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

can you post a vbench run at the top of the PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey .. I haven't been able to run vbench on my Windows machine. But I did run a test using time it. I will post the results from the previous PR here.

frame_shift_axis1 = Benchmark('df.shift(1,axis=1)', setup,
name = 'frame_shift_axis_1',
start_date=datetime(2014,1,1))