Skip to content

Commit 5aabc85

Browse files
committed
BUG: DataFrame.diff(axis=0) with DatetimeTZ data
add whatsnew clarify comment Add addtional tests move diff into its own function in DatetimeTZBlock Use correct placement fix failing test formatting
1 parent 92dbc78 commit 5aabc85

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,7 @@ Timezones
798798
- Bug in the :class:`DataFrame` constructor, where tz-aware Datetimeindex and a given column name will result in an empty ``DataFrame`` (:issue:`19157`)
799799
- Bug in :func:`Timestamp.tz_localize` where localizing a timestamp near the minimum or maximum valid values could overflow and return a timestamp with an incorrect nanosecond value (:issue:`12677`)
800800
- Bug when iterating over :class:`DatetimeIndex` that was localized with fixed timezone offset that rounded nanosecond precision to microseconds (:issue:`19603`)
801+
- Bug in :func:`DataFrame.diff` that raised an ``IndexError`` with tz-aware values (:issue:`18578`)
801802

802803
Offsets
803804
^^^^^^^

pandas/core/internals.py

+12
Original file line numberDiff line numberDiff line change
@@ -2905,6 +2905,18 @@ def shift(self, periods, axis=0, mgr=None):
29052905
return [self.make_block_same_class(new_values,
29062906
placement=self.mgr_locs)]
29072907

2908+
def diff(self, n, axis=0, mgr=None):
2909+
"""1st discrete difference"""
2910+
if axis == 0:
2911+
# Cannot currently calculate diff across multiple blocks since this
2912+
# function is invoked via apply
2913+
raise NotImplementedError
2914+
new_values = (self.values - self.shift(n, axis=axis)[0].values).asi8
2915+
# Reshape the new_values like how algos.diff does for timedelta data
2916+
new_values = new_values.reshape(1, len(new_values))
2917+
new_values = new_values.astype('timedelta64[ns]')
2918+
return [TimeDeltaBlock(new_values, placement=self.mgr_locs.indexer)]
2919+
29082920
def concat_same_type(self, to_concat, placement=None):
29092921
"""
29102922
Concatenate list of single blocks of the same type.

pandas/tests/frame/test_timeseries.py

+23
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ def test_diff(self):
5757
1), 'z': pd.Series(1)}).astype('float64')
5858
assert_frame_equal(result, expected)
5959

60+
@pytest.mark.parametrize('axis', [0, 1])
61+
@pytest.mark.parametrize('tz', [None, 'UTC'])
62+
def test_diff_datetime(self, axis, tz):
63+
# GH 18578
64+
df = DataFrame({0: date_range('2010', freq='D', periods=2, tz=tz),
65+
1: date_range('2010', freq='D', periods=2, tz=tz)})
66+
if axis == 1:
67+
if tz is None:
68+
result = df.diff(axis=axis)
69+
expected = DataFrame({0: pd.TimedeltaIndex(['NaT', 'NaT']),
70+
1: pd.TimedeltaIndex(['0 days',
71+
'0 days'])})
72+
assert_frame_equal(result, expected)
73+
else:
74+
with pytest.raises(NotImplementedError):
75+
result = df.diff(axis=axis)
76+
77+
else:
78+
result = df.diff(axis=axis)
79+
expected = DataFrame({0: pd.TimedeltaIndex(['NaT', '1 days']),
80+
1: pd.TimedeltaIndex(['NaT', '1 days'])})
81+
assert_frame_equal(result, expected)
82+
6083
def test_diff_timedelta(self):
6184
# GH 4533
6285
df = DataFrame(dict(time=[Timestamp('20130101 9:01'),

0 commit comments

Comments
 (0)