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

Preserve base and loffset arguments in resample #7444

Merged
merged 18 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Add typing and support for pd.Timedelta as an loffset
  • Loading branch information
spencerkclark committed Mar 5, 2023
commit 3431b664bb387c39af1b655e8d2f22ecd19a974d
20 changes: 16 additions & 4 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

from xarray.core.dataarray import DataArray
from xarray.core.dataset import Dataset
from xarray.core.types import DatetimeLike, SideOptions
from xarray.core.utils import Frozen

GroupKey = Any
Expand Down Expand Up @@ -245,7 +246,10 @@ def _unique_and_monotonic(group: T_Group) -> bool:
return index.is_unique and index.is_monotonic_increasing


def _apply_loffset(loffset, result):
def _apply_loffset(
loffset: str | pd.DateOffset | datetime.timedelta | pd.Timedelta,
result: pd.Series | pd.DataFrame,
):
"""
(copied from pandas)
if loffset is set, offset the result index
Expand All @@ -258,9 +262,9 @@ def _apply_loffset(loffset, result):
result : Series or DataFrame
the result of resample
"""
if not isinstance(loffset, (str, pd.DateOffset, datetime.timedelta)):
if not isinstance(loffset, (str, pd.DateOffset, datetime.timedelta, pd.Timedelta)):
raise ValueError(
f"`loffset` must be a str, pd.DateOffset, or datetime.timedelta object. "
f"`loffset` must be a str, pd.DateOffset, datetime.timedelta, or pandas.Timedelta object. "
f"Got {loffset}."
)

Expand Down Expand Up @@ -1367,7 +1371,15 @@ class DatasetGroupBy( # type: ignore[misc]


class TimeResampleGrouper:
def __init__(self, freq, closed, label, origin, offset, loffset):
def __init__(
self,
freq: str,
closed: SideOptions | None,
label: SideOptions | None,
origin: str | DatetimeLike,
offset: pd.Timedelta | datetime.timedelta | str | None,
loffset: datetime.timedelta | str | None,
):
self.freq = freq
self.closed = closed
self.label = label
Expand Down
8 changes: 7 additions & 1 deletion xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,13 @@ def test_resample_origin(self) -> None:

@pytest.mark.skipif(has_pandas_version_two, reason="requires pandas < 2.0.0")
@pytest.mark.parametrize(
"loffset", ["-12H", datetime.timedelta(hours=-12), pd.DateOffset(hours=-12)]
"loffset",
[
"-12H",
datetime.timedelta(hours=-12),
pd.Timedelta(hours=-12),
pd.DateOffset(hours=-12),
],
)
def test_resample_loffset(self, loffset) -> None:
times = pd.date_range("2000-01-01", freq="6H", periods=10)
Expand Down