Skip to content

Commit

Permalink
Adding support for Series.autocorr (#9833)
Browse files Browse the repository at this point in the history
Fixes: #9635 

TODO:
- [x] add implementation
- [x] tests
- [x] add to `.rst` files for documentation

Authors:
  - Mayank Anand (https://github.com/mayankanand007)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)
  - Michael Wang (https://github.com/isVoid)

URL: #9833
  • Loading branch information
mayankanand007 authored Dec 7, 2021
1 parent 0ce9571 commit a5633c2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/cudf/source/api_docs/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Computations / descriptive stats
Series.abs
Series.all
Series.any
Series.autocorr
Series.ceil
Series.clip
Series.corr
Expand Down
1 change: 0 additions & 1 deletion python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1919,7 +1919,6 @@ def round(self, decimals=0, how="half_even"):
2 0.7 0.0
3 0.2 0.0
"""

if isinstance(decimals, cudf.Series):
decimals = decimals.to_pandas()

Expand Down
25 changes: 25 additions & 0 deletions python/cudf/cudf/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2824,6 +2824,31 @@ def corr(self, other, method="pearson", min_periods=None):

return lhs._column.corr(rhs._column)

def autocorr(self, lag=1):
"""Compute the lag-N autocorrelation. This method computes the Pearson
correlation between the Series and its shifted self.
Parameters
----------
lag : int, default 1
Number of lags to apply before performing autocorrelation.
Returns
-------
result : float
The Pearson correlation between self and self.shift(lag).
Examples
--------
>>> import cudf
>>> s = cudf.Series([0.25, 0.5, 0.2, -0.05])
>>> s.autocorr()
0.10355263309024071
>>> s.autocorr(lag=2)
-0.9999999999999999
"""
return self.corr(self.shift(lag))

def isin(self, values):
"""Check whether values are contained in Series.
Expand Down
17 changes: 17 additions & 0 deletions python/cudf/cudf/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,23 @@ def test_nullable_bool_dtype_series(data, bool_dtype):
assert_eq(psr, gsr.to_pandas(nullable=True))


@pytest.mark.parametrize(
"cudf_series",
[
cudf.Series([0.25, 0.5, 0.2, -0.05]),
cudf.Series([0, 1, 2, np.nan, 4, cudf.NA, 6]),
],
)
@pytest.mark.parametrize("lag", [1, 2, 3, 4])
def test_autocorr(cudf_series, lag):
psr = cudf_series.to_pandas()

cudf_corr = cudf_series.autocorr(lag=lag)
pd_corr = psr.autocorr(lag=lag)

assert_eq(pd_corr, cudf_corr)


@pytest.mark.parametrize(
"data",
[
Expand Down

0 comments on commit a5633c2

Please sign in to comment.