Skip to content

Commit 5fc463c

Browse files
committed
BUG: DataFrame.shift with axis=1 was raising (GH6371)
1 parent 8393651 commit 5fc463c

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Bug Fixes
111111
- Bug in ``pd.eval`` when parsing strings with possible tokens like ``'&'``
112112
(:issue:`6351`)
113113
- Bug correctly handle placements of ``-inf`` in Panels when dividing by integer 0 (:issue:`6178`)
114+
- ``DataFrame.shift`` with ``axis=1`` was raising (:issue:`6371`)
114115

115116
pandas 0.13.1
116117
-------------

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3170,7 +3170,7 @@ def shift(self, periods=1, freq=None, axis=0, **kwds):
31703170

31713171
if freq is None and not len(kwds):
31723172
block_axis = self._get_block_manager_axis(axis)
3173-
indexer = com._shift_indexer(len(self), periods)
3173+
indexer = com._shift_indexer(len(self._get_axis(axis)), periods)
31743174
new_data = self._data.shift(indexer=indexer, periods=periods, axis=block_axis)
31753175
else:
31763176
return self.tshift(periods, freq, **kwds)

pandas/core/internals.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -934,19 +934,14 @@ def shift(self, indexer, periods, axis=0):
934934
# that, handle boolean etc also
935935
new_values, fill_value = com._maybe_upcast(new_values)
936936

937-
# 1-d
938-
if self.ndim == 1:
939-
if periods > 0:
940-
new_values[:periods] = fill_value
941-
else:
942-
new_values[periods:] = fill_value
943-
944-
# 2-d
937+
axis_indexer = [ slice(None) ] * self.ndim
938+
if periods > 0:
939+
axis_indexer[axis] = slice(None,periods)
945940
else:
946-
if periods > 0:
947-
new_values[:, :periods] = fill_value
948-
else:
949-
new_values[:, periods:] = fill_value
941+
axis_indexer = [ slice(None) ] * self.ndim
942+
axis_indexer[axis] = slice(periods,None)
943+
new_values[tuple(axis_indexer)] = fill_value
944+
950945
return [make_block(new_values, self.items, self.ref_items,
951946
ndim=self.ndim, fastpath=True)]
952947

pandas/tests/test_frame.py

+24-20
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,13 @@
2727
from numpy.testing import assert_array_equal
2828
import numpy.ma.mrecords as mrecords
2929

30-
import pandas as pan
3130
import pandas.core.nanops as nanops
3231
import pandas.core.common as com
3332
import pandas.core.format as fmt
3433
import pandas.core.datetools as datetools
35-
from pandas.core.api import (DataFrame, Index, Series, notnull, isnull,
36-
MultiIndex, DatetimeIndex, Timestamp)
37-
from pandas import date_range
34+
from pandas import (DataFrame, Index, Series, notnull, isnull,
35+
MultiIndex, DatetimeIndex, Timestamp, date_range, read_csv)
3836
import pandas as pd
39-
from pandas.io.parsers import read_csv
4037
from pandas.parser import CParserError
4138
from pandas.util.misc import is_little_endian
4239

@@ -3740,7 +3737,7 @@ def test_to_dict(self):
37403737
def test_to_records_dt64(self):
37413738
df = DataFrame([["one", "two", "three"],
37423739
["four", "five", "six"]],
3743-
index=pan.date_range("2012-01-01", "2012-01-02"))
3740+
index=date_range("2012-01-01", "2012-01-02"))
37443741
self.assertEqual(df.to_records()['index'][0], df.index[0])
37453742

37463743
rs = df.to_records(convert_datetime64=False)
@@ -5883,7 +5880,7 @@ def create_cols(name):
58835880
#### this is a bug in read_csv right now ####
58845881
#df_dt.ix[30:50,1:3] = np.nan
58855882

5886-
df = pan.concat([ df_float, df_int, df_bool, df_object, df_dt ], axis=1)
5883+
df = pd.concat([ df_float, df_int, df_bool, df_object, df_dt ], axis=1)
58875884

58885885
# dtype
58895886
dtypes = dict()
@@ -5893,7 +5890,7 @@ def create_cols(name):
58935890

58945891
with ensure_clean() as filename:
58955892
df.to_csv(filename)
5896-
rs = pan.read_csv(filename, index_col=0, dtype=dtypes, parse_dates=create_cols('date'))
5893+
rs = read_csv(filename, index_col=0, dtype=dtypes, parse_dates=create_cols('date'))
58975894
assert_frame_equal(rs, df)
58985895

58995896
def test_to_csv_dups_cols(self):
@@ -5911,7 +5908,7 @@ def test_to_csv_dups_cols(self):
59115908
df_bool = DataFrame(True,index=df_float.index,columns=lrange(3))
59125909
df_object = DataFrame('foo',index=df_float.index,columns=lrange(3))
59135910
df_dt = DataFrame(Timestamp('20010101'),index=df_float.index,columns=lrange(3))
5914-
df = pan.concat([ df_float, df_int, df_bool, df_object, df_dt ], axis=1, ignore_index=True)
5911+
df = pd.concat([ df_float, df_int, df_bool, df_object, df_dt ], axis=1, ignore_index=True)
59155912

59165913
cols = []
59175914
for i in range(5):
@@ -5955,7 +5952,7 @@ def test_to_csv_chunking(self):
59555952
for chunksize in [10000,50000,100000]:
59565953
with ensure_clean() as filename:
59575954
aa.to_csv(filename,chunksize=chunksize)
5958-
rs = pan.read_csv(filename,index_col=0)
5955+
rs = read_csv(filename,index_col=0)
59595956
assert_frame_equal(rs, aa)
59605957

59615958
def test_to_csv_bug(self):
@@ -5966,7 +5963,7 @@ def test_to_csv_bug(self):
59665963
with ensure_clean() as path:
59675964
newdf.to_csv(path)
59685965

5969-
recons = pan.read_csv(path, index_col=0)
5966+
recons = read_csv(path, index_col=0)
59705967
assert_frame_equal(recons, newdf, check_names=False) # don't check_names as t != 1
59715968

59725969
def test_to_csv_unicode(self):
@@ -5975,11 +5972,11 @@ def test_to_csv_unicode(self):
59755972
with ensure_clean() as path:
59765973

59775974
df.to_csv(path, encoding='UTF-8')
5978-
df2 = pan.read_csv(path, index_col=0, encoding='UTF-8')
5975+
df2 = read_csv(path, index_col=0, encoding='UTF-8')
59795976
assert_frame_equal(df, df2)
59805977

59815978
df.to_csv(path, encoding='UTF-8', index=False)
5982-
df2 = pan.read_csv(path, index_col=None, encoding='UTF-8')
5979+
df2 = read_csv(path, index_col=None, encoding='UTF-8')
59835980
assert_frame_equal(df, df2)
59845981

59855982
def test_to_csv_unicode_index_col(self):
@@ -5993,14 +5990,14 @@ def test_to_csv_unicode_index_col(self):
59935990
df.to_csv(buf, encoding='UTF-8')
59945991
buf.seek(0)
59955992

5996-
df2 = pan.read_csv(buf, index_col=0, encoding='UTF-8')
5993+
df2 = read_csv(buf, index_col=0, encoding='UTF-8')
59975994
assert_frame_equal(df, df2)
59985995

59995996
def test_to_csv_stringio(self):
60005997
buf = StringIO()
60015998
self.frame.to_csv(buf)
60025999
buf.seek(0)
6003-
recons = pan.read_csv(buf, index_col=0)
6000+
recons = read_csv(buf, index_col=0)
60046001
assert_frame_equal(recons, self.frame, check_names=False) # TODO to_csv drops column name
60056002

60066003
def test_to_csv_float_format(self):
@@ -6013,7 +6010,7 @@ def test_to_csv_float_format(self):
60136010

60146011
df.to_csv(filename, float_format='%.2f')
60156012

6016-
rs = pan.read_csv(filename, index_col=0)
6013+
rs = read_csv(filename, index_col=0)
60176014
xp = DataFrame([[0.12, 0.23, 0.57],
60186015
[12.32, 123123.20, 321321.20]],
60196016
index=['A', 'B'], columns=['X', 'Y', 'Z'])
@@ -6359,7 +6356,7 @@ def test_asfreq_datetimeindex(self):
63596356
tm.assert_isinstance(ts.index, DatetimeIndex)
63606357

63616358
def test_at_time_between_time_datetimeindex(self):
6362-
index = pan.date_range("2012-01-01", "2012-01-05", freq='30min')
6359+
index = date_range("2012-01-01", "2012-01-05", freq='30min')
63636360
df = DataFrame(randn(len(index), 5), index=index)
63646361
akey = time(12, 0, 0)
63656362
bkey = slice(time(13, 0, 0), time(14, 0, 0))
@@ -8009,12 +8006,11 @@ def test_replace_with_dict_with_bool_keys(self):
80098006
df.replace({'asdf': 'asdb', True: 'yes'})
80108007

80118008
def test_combine_multiple_frames_dtypes(self):
8012-
from pandas import concat
80138009

80148010
# GH 2759
80158011
A = DataFrame(data=np.ones((10, 2)), columns=['foo', 'bar'], dtype=np.float64)
80168012
B = DataFrame(data=np.ones((10, 2)), dtype=np.float32)
8017-
results = concat((A, B), axis=1).get_dtype_counts()
8013+
results = pd.concat((A, B), axis=1).get_dtype_counts()
80188014
expected = Series(dict( float64 = 2, float32 = 2 ))
80198015
assert_series_equal(results,expected)
80208016

@@ -8994,6 +8990,14 @@ def test_shift(self):
89948990
assertRaisesRegexp(ValueError, 'does not match PeriodIndex freq',
89958991
ps.shift, freq='D')
89968992

8993+
8994+
# shift other axis
8995+
# GH 6371
8996+
df = DataFrame(np.random.rand(10,5))
8997+
expected = pd.concat([DataFrame(np.nan,index=df.index,columns=[0]),df.iloc[:,0:-1]],ignore_index=True,axis=1)
8998+
result = df.shift(1,axis=1)
8999+
assert_frame_equal(result,expected)
9000+
89979001
def test_shift_bool(self):
89989002
df = DataFrame({'high': [True, False],
89999003
'low': [False, False]})
@@ -11339,7 +11343,7 @@ def test_columns_with_dups(self):
1133911343
df_bool = DataFrame(True,index=df_float.index,columns=df_float.columns)
1134011344
df_object = DataFrame('foo',index=df_float.index,columns=df_float.columns)
1134111345
df_dt = DataFrame(Timestamp('20010101'),index=df_float.index,columns=df_float.columns)
11342-
df = pan.concat([ df_float, df_int, df_bool, df_object, df_dt ], axis=1)
11346+
df = pd.concat([ df_float, df_int, df_bool, df_object, df_dt ], axis=1)
1134311347

1134411348
result = df._data._set_ref_locs()
1134511349
self.assertEqual(len(result), len(df.columns))

0 commit comments

Comments
 (0)