Skip to content

Commit 302803f

Browse files
author
Marco Gorelli
committed
🐛 Series.diff was always setting the dtype to object
1 parent dd6e31a commit 302803f

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,7 @@ ExtensionArray
11461146
- Bug in :class:`arrays.PandasArray` when setting a scalar string (:issue:`28118`, :issue:`28150`).
11471147
- Bug where nullable integers could not be compared to strings (:issue:`28930`)
11481148
- Bug where :class:`DataFrame` constructor raised ``ValueError`` with list-like data and ``dtype`` specified (:issue:`30280`)
1149+
- Bug in :meth:`Series.diff` was always setting the ``dtype`` to ``Object`` (:issue:`30889`)
11491150

11501151

11511152
Other

pandas/core/series.py

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
is_bool,
3535
is_categorical_dtype,
3636
is_datetime64_dtype,
37+
is_datetime64tz_dtype,
3738
is_dict_like,
3839
is_extension_array_dtype,
3940
is_integer,
@@ -2316,6 +2317,12 @@ def diff(self, periods=1) -> "Series":
23162317
dtype: float64
23172318
"""
23182319
result = algorithms.diff(com.values_from_object(self), periods)
2320+
if is_extension_array_dtype(self.dtype) and not is_datetime64tz_dtype(
2321+
self.dtype
2322+
):
2323+
return self._constructor(
2324+
result, index=self.index, dtype=self.dtype
2325+
).__finalize__(self)
23192326
return self._constructor(result, index=self.index).__finalize__(self)
23202327

23212328
def autocorr(self, lag=1) -> float:

pandas/tests/series/methods/test_diff.py

+6
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,9 @@ def test_diff_object_dtype(self):
7575
result = s.diff()
7676
expected = s - s.shift(1)
7777
tm.assert_series_equal(result, expected)
78+
79+
def test_nullable_integer(self, any_nullable_int_dtype):
80+
# GH 30889
81+
dtype = any_nullable_int_dtype
82+
result = Series([1, 2, 3], dtype=dtype).diff().dtype
83+
assert result == dtype

0 commit comments

Comments
 (0)