Skip to content

Commit f217fdb

Browse files
Marco GorelliMarcoGorelli
Marco Gorelli
authored andcommitted
🐛 Series.diff was always setting the dtype to object
1 parent 6f2c509 commit f217fdb

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
@@ -22,6 +22,7 @@
2222
is_bool,
2323
is_categorical_dtype,
2424
is_datetime64_dtype,
25+
is_datetime64tz_dtype,
2526
is_dict_like,
2627
is_extension_array_dtype,
2728
is_integer,
@@ -2301,6 +2302,12 @@ def diff(self, periods=1):
23012302
dtype: float64
23022303
"""
23032304
result = algorithms.diff(com.values_from_object(self), periods)
2305+
if is_extension_array_dtype(self.dtype) and not is_datetime64tz_dtype(
2306+
self.dtype
2307+
):
2308+
return self._constructor(
2309+
result, index=self.index, dtype=self.dtype
2310+
).__finalize__(self)
23042311
return self._constructor(result, index=self.index).__finalize__(self)
23052312

23062313
def autocorr(self, lag=1):

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)