diff --git a/bigframes/series.py b/bigframes/series.py index 185891bc01..b5e1d9d8cf 100644 --- a/bigframes/series.py +++ b/bigframes/series.py @@ -772,6 +772,9 @@ def corr(self, other: Series, method="pearson", min_periods=None) -> float: ) return self._apply_binary_aggregation(other, agg_ops.CorrOp()) + def autocorr(self, lag: int = 1) -> float: + return self.corr(self.shift(lag)) + def cov(self, other: Series) -> float: return self._apply_binary_aggregation(other, agg_ops.CovOp()) diff --git a/tests/system/small/test_series.py b/tests/system/small/test_series.py index c882677508..8536a8f272 100644 --- a/tests/system/small/test_series.py +++ b/tests/system/small/test_series.py @@ -718,6 +718,14 @@ def test_series_corr(scalars_dfs): assert math.isclose(pd_result, bf_result) +@skip_legacy_pandas +def test_series_autocorr(scalars_dfs): + scalars_df, scalars_pandas_df = scalars_dfs + bf_result = scalars_df["float64_col"].autocorr(2) + pd_result = scalars_pandas_df["float64_col"].autocorr(2) + assert math.isclose(pd_result, bf_result) + + def test_series_cov(scalars_dfs): scalars_df, scalars_pandas_df = scalars_dfs bf_result = scalars_df["int64_too"].cov(scalars_df["int64_too"]) diff --git a/third_party/bigframes_vendored/pandas/core/series.py b/third_party/bigframes_vendored/pandas/core/series.py index a75d6c2167..b377f61be8 100644 --- a/third_party/bigframes_vendored/pandas/core/series.py +++ b/third_party/bigframes_vendored/pandas/core/series.py @@ -819,6 +819,38 @@ def corr(self, other, method="pearson", min_periods=None) -> float: """ raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) + def autocorr(self, lag: int = 1) -> float: + """ + Compute the lag-N autocorrelation. + + This method computes the Pearson correlation between + the Series and its shifted self. + + **Examples:** + >>> import bigframes.pandas as bpd + >>> bpd.options.display.progress_bar = None + + >>> s = bpd.Series([0.25, 0.5, 0.2, -0.05]) + >>> s.autocorr() # doctest: +ELLIPSIS + 0.10355... + >>> s.autocorr(lag=2) + -1.0 + + If the Pearson correlation is not well defined, then 'NaN' is returned. + + >>> s = bpd.Series([1, 0, 0, 0]) + >>> s.autocorr() + nan + + Args: + lag (int, default 1): + Number of lags to apply before performing autocorrelation. + + Returns: + float: The Pearson correlation between self and self.shift(lag). + """ + raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) + def cov( self, other,