Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for Series.autocorr #9833

Merged
merged 19 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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