Skip to content

Commit 4ec8034

Browse files
feat: Add Series.autocorr (#605)
1 parent 231cf29 commit 4ec8034

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

bigframes/series.py

+3
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,9 @@ def corr(self, other: Series, method="pearson", min_periods=None) -> float:
791791
)
792792
return self._apply_binary_aggregation(other, agg_ops.CorrOp())
793793

794+
def autocorr(self, lag: int = 1) -> float:
795+
return self.corr(self.shift(lag))
796+
794797
def cov(self, other: Series) -> float:
795798
return self._apply_binary_aggregation(other, agg_ops.CovOp())
796799

tests/system/small/test_series.py

+8
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,14 @@ def test_series_corr(scalars_dfs):
718718
assert math.isclose(pd_result, bf_result)
719719

720720

721+
@skip_legacy_pandas
722+
def test_series_autocorr(scalars_dfs):
723+
scalars_df, scalars_pandas_df = scalars_dfs
724+
bf_result = scalars_df["float64_col"].autocorr(2)
725+
pd_result = scalars_pandas_df["float64_col"].autocorr(2)
726+
assert math.isclose(pd_result, bf_result)
727+
728+
721729
def test_series_cov(scalars_dfs):
722730
scalars_df, scalars_pandas_df = scalars_dfs
723731
bf_result = scalars_df["int64_too"].cov(scalars_df["int64_too"])

third_party/bigframes_vendored/pandas/core/series.py

+32
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,38 @@ def corr(self, other, method="pearson", min_periods=None) -> float:
844844
"""
845845
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
846846

847+
def autocorr(self, lag: int = 1) -> float:
848+
"""
849+
Compute the lag-N autocorrelation.
850+
851+
This method computes the Pearson correlation between
852+
the Series and its shifted self.
853+
854+
**Examples:**
855+
>>> import bigframes.pandas as bpd
856+
>>> bpd.options.display.progress_bar = None
857+
858+
>>> s = bpd.Series([0.25, 0.5, 0.2, -0.05])
859+
>>> s.autocorr() # doctest: +ELLIPSIS
860+
0.10355...
861+
>>> s.autocorr(lag=2)
862+
-1.0
863+
864+
If the Pearson correlation is not well defined, then 'NaN' is returned.
865+
866+
>>> s = bpd.Series([1, 0, 0, 0])
867+
>>> s.autocorr()
868+
nan
869+
870+
Args:
871+
lag (int, default 1):
872+
Number of lags to apply before performing autocorrelation.
873+
874+
Returns:
875+
float: The Pearson correlation between self and self.shift(lag).
876+
"""
877+
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
878+
847879
def cov(
848880
self,
849881
other,

0 commit comments

Comments
 (0)