Skip to content

Commit 219ad75

Browse files
terrytangyuanjreback
authored andcommitted
BUG: Fixed bug in DataFrame.diff, #10907 when DataFrame is not consolidated
1 parent 92cb9e2 commit 219ad75

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v0.17.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ Bug Fixes
812812
- Bug in ``read_csv`` when using the ``nrows`` or ``chunksize`` parameters if file contains only a header line (:issue:`9535`)
813813
- Bug in serialization of ``category`` types in HDF5 in presence of alternate encodings. (:issue:`10366`)
814814
- Bug in ``pd.DataFrame`` when constructing an empty DataFrame with a string dtype (:issue:`9428`)
815+
- Bug in ``pd.DataFrame.diff`` when DataFrame is not consolidated (:issue:`10907`)
815816
- Bug in ``pd.unique`` for arrays with the ``datetime64`` or ``timedelta64`` dtype that meant an array with object dtype was returned instead the original dtype (:issue:`9431`)
816817
- Bug in ``DatetimeIndex.take`` and ``TimedeltaIndex.take`` may not raise ``IndexError`` against invalid index (:issue:`10295`)
817818
- Bug in ``Series([np.nan]).astype('M8[ms]')``, which now returns ``Series([pd.NaT])`` (:issue:`10747`)

pandas/core/internals.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,7 @@ def _verify_integrity(self):
24142414
'tot_items: {1}'.format(len(self.items),
24152415
tot_items))
24162416

2417-
def apply(self, f, axes=None, filter=None, do_integrity_check=False, **kwargs):
2417+
def apply(self, f, axes=None, filter=None, do_integrity_check=False, consolidate=True, **kwargs):
24182418
"""
24192419
iterate over the blocks, collect and create a new block manager
24202420
@@ -2425,6 +2425,7 @@ def apply(self, f, axes=None, filter=None, do_integrity_check=False, **kwargs):
24252425
filter : list, if supplied, only call the block if the filter is in
24262426
the block
24272427
do_integrity_check : boolean, default False. Do the block manager integrity check
2428+
consolidate: boolean, default True. Join together blocks having same dtype
24282429
24292430
Returns
24302431
-------
@@ -2443,6 +2444,9 @@ def apply(self, f, axes=None, filter=None, do_integrity_check=False, **kwargs):
24432444
else:
24442445
kwargs['filter'] = filter_locs
24452446

2447+
if consolidate:
2448+
self._consolidate_inplace()
2449+
24462450
if f == 'where':
24472451
align_copy = True
24482452
if kwargs.get('align', True):

pandas/tests/test_frame.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10771,6 +10771,14 @@ def test_diff(self):
1077110771
assert_series_equal(the_diff['A'],
1077210772
tf['A'] - tf['A'].shift(1))
1077310773

10774+
# issue 10907
10775+
df = pd.DataFrame({'y': pd.Series([2]), 'z': pd.Series([3])})
10776+
df.insert(0, 'x', 1)
10777+
result = df.diff(axis=1)
10778+
expected = pd.DataFrame({'x':np.nan, 'y':pd.Series(1), 'z':pd.Series(1)}).astype('float64')
10779+
self.assert_frame_equal(result, expected)
10780+
10781+
1077410782
def test_diff_timedelta(self):
1077510783
# GH 4533
1077610784
df = DataFrame(dict(time=[Timestamp('20130101 9:01'),

0 commit comments

Comments
 (0)