Skip to content

Commit 7a9a833

Browse files
author
Marco Gorelli
committed
🐛 Series.diff was always setting the dtype to object
1 parent 6f2c509 commit 7a9a833

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-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

+13
Original file line numberDiff line numberDiff line change
@@ -2301,6 +2301,19 @@ def diff(self, periods=1):
23012301
dtype: float64
23022302
"""
23032303
result = algorithms.diff(com.values_from_object(self), periods)
2304+
if self.dtype in [
2305+
"Int8",
2306+
"Int16",
2307+
"Int32",
2308+
"Int64",
2309+
"UInt8",
2310+
"UInt16",
2311+
"UInt32",
2312+
"UInt64",
2313+
]:
2314+
return self._constructor(
2315+
result, index=self.index, dtype=self.dtype
2316+
).__finalize__(self)
23042317
return self._constructor(result, index=self.index).__finalize__(self)
23052318

23062319
def autocorr(self, lag=1):

pandas/tests/series/methods/test_diff.py

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from pandas import Series, TimedeltaIndex, date_range
55
import pandas._testing as tm
6+
from pandas.conftest import ALL_EA_INT_DTYPES
67

78

89
class TestSeriesDiff:
@@ -75,3 +76,9 @@ def test_diff_object_dtype(self):
7576
result = s.diff()
7677
expected = s - s.shift(1)
7778
tm.assert_series_equal(result, expected)
79+
80+
@pytest.mark.parametrize("dtype", ALL_EA_INT_DTYPES)
81+
def test_nullable_integer(self, dtype):
82+
# GH 30889
83+
result = Series([1, 2, 3], dtype=dtype).diff().dtype
84+
assert result == dtype

0 commit comments

Comments
 (0)