Skip to content

Quick shift fixes #6747

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 2 commits into from
Mar 31, 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
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3238,9 +3238,9 @@ def shift(self, periods=1, freq=None, axis=0, **kwds):
if periods == 0:
return self

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

Expand Down
13 changes: 11 additions & 2 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,15 +966,24 @@ def shift(self, periods, axis=0):
# 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(self.values)
new_values = np.roll(new_values.T,periods,axis=axis)
# make sure array sent to np.roll is c_contiguous
f_ordered = new_values.flags.f_contiguous
if f_ordered:
new_values = new_values.T
axis = new_values.ndim - axis - 1
new_values = np.roll(new_values, periods, axis=axis)
axis_indexer = [ slice(None) ] * self.ndim
if periods > 0:
axis_indexer[axis] = slice(None,periods)
else:
axis_indexer[axis] = slice(periods,None)
new_values[tuple(axis_indexer)] = fill_value

return [make_block(new_values.T, self.items, self.ref_items,
# restore original order
if f_ordered:
new_values = new_values.T

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

def eval(self, func, other, raise_on_error=True, try_cast=False):
Expand Down
20 changes: 15 additions & 5 deletions vb_suite/frame_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def test_equal(name):
def test_unequal(name):
df, df2 = pairs[name]
return df.equals(df2)

float_df = DataFrame(np.random.randn(1000, 1000))
object_df = DataFrame([['foo']*1000]*1000)
nonunique_cols = object_df.copy()
Expand Down Expand Up @@ -434,11 +434,21 @@ def test_unequal(name):
# frame shift speedup issue-5609

setup = common_setup + """
df = pd.DataFrame(np.random.rand(10000,500))
df = DataFrame(np.random.rand(10000,500))
# note: df._data.blocks are f_contigous
"""
frame_shift_axis0 = Benchmark('df.shift(1,axis=0)', setup,
name = 'frame_shift_axis_0',
start_date=datetime(2014,1,1))
frame_shift_axis1 = Benchmark('df.shift(1,axis=1)', setup,
name = 'frame_shift_axis_1',
start_date=datetime(2014,1,1))
start_date=datetime(2014,1,1))

#
setup = common_setup + """
df = DataFrame(np.random.rand(10000,500))
df = df.consolidate()
# note: df._data.blocks are c_contigous
"""
frame_shift_c_order_axis0 = Benchmark('df.shift(1,axis=0)', setup,
start_date=datetime(2014,1,1))
frame_shift_c_order_axis1 = Benchmark('df.shift(1,axis=1)', setup,
start_date=datetime(2014,1,1))