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

Added pct_change to Series #8650

Merged
merged 4 commits into from
Jul 22, 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
11 changes: 9 additions & 2 deletions python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1296,8 +1296,15 @@ def fillna(
if value is not None and method is not None:
raise ValueError("Cannot specify both 'value' and 'method'.")

if method and method not in {"ffill", "bfill"}:
raise NotImplementedError(f"Fill method {method} is not supported")
if method:
if method not in {"ffill", "bfill", "pad", "backfill"}:
raise NotImplementedError(
f"Fill method {method} is not supported"
)
if method == "pad":
method = "ffill"
elif method == "backfill":
method = "bfill"

if isinstance(value, cudf.Series):
value = value.reindex(self._data.names)
Expand Down
39 changes: 39 additions & 0 deletions python/cudf/cudf/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -6022,6 +6022,45 @@ def explode(self, ignore_index=False):

return super()._explode(self._column_names[0], ignore_index)

def pct_change(
self, periods=1, fill_method="ffill", limit=None, freq=None
):
"""
Calculates the percent change between sequential elements
in the Series.

Parameters
----------
periods : int, default 1
Periods to shift for forming percent change.
fill_method : str, default 'ffill'
How to handle NAs before computing percent changes.
limit : int, optional
The number of consecutive NAs to fill before stopping.
Not yet implemented.
freq : str, optional
Increment to use from time series API.
Not yet implemented.

Returns
-------
Series
"""
if limit is not None:
raise NotImplementedError("limit parameter not supported yet.")
if freq is not None:
raise NotImplementedError("freq parameter not supported yet.")
elif fill_method not in {"ffill", "pad", "bfill", "backfill"}:
raise ValueError(
"fill_method must be one of 'ffill', 'pad', "
"'bfill', or 'backfill'."
)

data = self.fillna(method=fill_method, limit=limit)
diff = data.diff(periods=periods)
change = diff / data.shift(periods=periods, freq=freq)
return change


class DatetimeProperties(object):
"""
Expand Down
26 changes: 26 additions & 0 deletions python/cudf/cudf/tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,32 @@ def test_series_median(dtype, num_na):
np.testing.assert_approx_equal(actual, desired)


@pytest.mark.parametrize(
"data",
[
np.random.normal(-100, 100, 1000),
np.random.randint(-50, 50, 1000),
np.zeros(100),
np.array([1.123, 2.343, np.nan, 0.0]),
np.array([-2, 3.75, 6, None, None, None, -8.5, None, 4.2]),
cudf.Series([]),
cudf.Series([-3]),
],
)
@pytest.mark.parametrize("periods", range(-5, 5))
@pytest.mark.parametrize("fill_method", ["ffill", "bfill", "pad", "backfill"])
def test_series_pct_change(data, periods, fill_method):
cs = cudf.Series(data)
ps = cs.to_pandas()

if np.abs(periods) <= len(cs):
got = cs.pct_change(periods=periods, fill_method=fill_method)
expected = ps.pct_change(periods=periods, fill_method=fill_method)
np.testing.assert_array_almost_equal(
got.to_array(fillna="pandas"), expected
)


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