Skip to content

Commit 1e1c922

Browse files
committed
BUG: DataFrame.shift converts to object for bool #1814
1 parent 4201388 commit 1e1c922

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

pandas/core/common.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,19 @@ def ensure_float(arr):
526526

527527
return arr
528528

529+
def ensure_nansafe(arr):
530+
"""
531+
convert int to float
532+
convert bool to object
533+
"""
534+
if issubclass(arr.dtype.type, np.integer):
535+
arr = arr.astype(float)
536+
537+
elif issubclass(arr.dtype.type, np.bool_):
538+
arr = arr.astype(object)
539+
540+
return arr
541+
529542
def _mut_exclusive(arg1, arg2):
530543
if arg1 is not None and arg2 is not None:
531544
raise Exception('mutually exclusive arguments')

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3567,7 +3567,7 @@ def _shift_block(blk, indexer):
35673567
new_values = blk.values.take(indexer, axis=1)
35683568
# convert integer to float if necessary. need to do a lot more than
35693569
# that, handle boolean etc also
3570-
new_values = com.ensure_float(new_values)
3570+
new_values = com.ensure_nansafe(new_values)
35713571
if periods > 0:
35723572
new_values[:, :periods] = nan
35733573
else:

pandas/tests/test_frame.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4930,6 +4930,15 @@ def test_shift(self):
49304930

49314931
self.assertRaises(ValueError, ps.shift, freq='D')
49324932

4933+
def test_shift_bool(self):
4934+
df = DataFrame({'high':[True, False],
4935+
'low':[False, False]})
4936+
rs = df.shift(1)
4937+
xp = DataFrame(np.array([[np.nan, np.nan],
4938+
[True, False]], dtype=object),
4939+
columns=['high', 'low'])
4940+
assert_frame_equal(rs, xp)
4941+
49334942
def test_tshift(self):
49344943
# PeriodIndex
49354944
ps = tm.makePeriodFrame()

0 commit comments

Comments
 (0)