Skip to content

Commit ac7bb30

Browse files
author
Marco Gorelli
committed
🎨 implement diff within extensionarray
1 parent 302803f commit ac7bb30

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

doc/source/whatsnew/v1.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1146,7 +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`)
1149+
- Bug in :meth:`Series.diff` was always setting the ``dtype`` to ``object`` (:issue:`30889`)
11501150

11511151

11521152
Other

pandas/core/arrays/base.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from pandas.core.dtypes.missing import isna
2525

2626
from pandas.core import ops
27-
from pandas.core.algorithms import _factorize_array, unique
27+
from pandas.core.algorithms import _factorize_array, diff, unique
2828
from pandas.core.missing import backfill_1d, pad_1d
2929
from pandas.core.sorting import nargsort
3030

@@ -516,6 +516,27 @@ def argsort(
516516
result = nargsort(self, kind=kind, ascending=ascending, na_position="last")
517517
return result
518518

519+
def diff(self, periods: int = 1):
520+
"""
521+
First discrete difference of element.
522+
523+
Calculates the difference of a ExtensionArray element compared with another
524+
element in the ExtensionArray (default is element in previous row).
525+
526+
Parameters
527+
----------
528+
periods : int, default 1
529+
Periods to shift for calculating difference, accepts negative
530+
values.
531+
532+
Returns
533+
-------
534+
ExtensionArray
535+
First differences of the ExtensionArray.
536+
"""
537+
538+
return self._from_sequence(diff(self, periods), dtype=self.dtype)
539+
519540
def fillna(self, value=None, method=None, limit=None):
520541
"""
521542
Fill NA/NaN values using the specified method.

pandas/core/series.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2316,13 +2316,11 @@ def diff(self, periods=1) -> "Series":
23162316
5 NaN
23172317
dtype: float64
23182318
"""
2319-
result = algorithms.diff(com.values_from_object(self), periods)
23202319
if is_extension_array_dtype(self.dtype) and not is_datetime64tz_dtype(
23212320
self.dtype
23222321
):
2323-
return self._constructor(
2324-
result, index=self.index, dtype=self.dtype
2325-
).__finalize__(self)
2322+
return self.values.diff(periods=periods)
2323+
result = algorithms.diff(com.values_from_object(self), periods)
23262324
return self._constructor(result, index=self.index).__finalize__(self)
23272325

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

0 commit comments

Comments
 (0)