From 86319723b4ab74dfd2f6bc0f99977e1eaff5c832 Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Fri, 15 Aug 2014 18:15:10 +0100 Subject: [PATCH] Don't call np.roll on empty arrays. On some versions of numpy (such as 1.7, but not 1.8), this throws a ZeroDivisionError. Fixes #8019. --- doc/source/v0.15.0.txt | 2 +- pandas/core/internals.py | 5 ++++- pandas/tests/test_frame.py | 7 +++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/doc/source/v0.15.0.txt b/doc/source/v0.15.0.txt index 4bd55b2172013..b3ee0980635e3 100644 --- a/doc/source/v0.15.0.txt +++ b/doc/source/v0.15.0.txt @@ -389,7 +389,7 @@ Enhancements - +- Bug in ``DataFrame.shift`` where empty columns would throw ``ZeroDivisionError`` on numpy 1.7 (:issue:`8019`) diff --git a/pandas/core/internals.py b/pandas/core/internals.py index f3b8a54034d56..58f98b4ee21e6 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -817,7 +817,10 @@ def shift(self, periods, axis=0): if f_ordered: new_values = new_values.T axis = new_values.ndim - axis - 1 - new_values = np.roll(new_values, periods, axis=axis) + + if np.prod(new_values.shape): + new_values = np.roll(new_values, periods, axis=axis) + axis_indexer = [ slice(None) ] * self.ndim if periods > 0: axis_indexer[axis] = slice(None,periods) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index d1e6a2bf59303..b4e548dd5b964 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -9610,6 +9610,13 @@ def test_shift_bool(self): columns=['high', 'low']) assert_frame_equal(rs, xp) + def test_shift_empty(self): + # Regression test for #8019 + df = DataFrame({'foo': []}) + rs = df.shift(-1) + + assert_frame_equal(df, rs) + def test_tshift(self): # PeriodIndex ps = tm.makePeriodFrame()