Skip to content

Commit 769e850

Browse files
committed
Tidy resample type hints
1 parent 977c7a4 commit 769e850

File tree

6 files changed

+28
-27
lines changed

6 files changed

+28
-27
lines changed

xarray/core/common.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
except ImportError:
2929
cftime = None
3030

31+
if TYPE_CHECKING:
32+
from xarray.core.types import ResampleCompatible
33+
3134
# Used as a sentinel value to indicate a all dimensions
3235
ALL_DIMS = ...
3336

@@ -890,7 +893,7 @@ def rolling_exp(
890893
def _resample(
891894
self,
892895
resample_cls: type[T_Resample],
893-
indexer: Mapping[Hashable, str | Resampler] | None,
896+
indexer: Mapping[Hashable, ResampleCompatible | Resampler] | None,
894897
skipna: bool | None,
895898
closed: SideOptions | None,
896899
label: SideOptions | None,
@@ -1077,7 +1080,7 @@ def _resample(
10771080
)
10781081

10791082
grouper: Resampler
1080-
if isinstance(freq, str | datetime.timedelta | pd.Timedelta | pd.DateOffset):
1083+
if isinstance(freq, ResampleCompatible):
10811084
grouper = TimeResampler(
10821085
freq=freq, closed=closed, label=label, origin=origin, offset=offset
10831086
)

xarray/core/dataarray.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
QueryEngineOptions,
111111
QueryParserOptions,
112112
ReindexMethodOptions,
113+
ResampleCompatible,
113114
Self,
114115
SideOptions,
115116
T_ChunkDimFreq,
@@ -7244,19 +7245,15 @@ def coarsen(
72447245
@_deprecate_positional_args("v2024.07.0")
72457246
def resample(
72467247
self,
7247-
indexer: Mapping[Hashable, str | Resampler] | None = None,
7248+
indexer: Mapping[Hashable, ResampleCompatible | Resampler] | None = None,
72487249
*,
72497250
skipna: bool | None = None,
72507251
closed: SideOptions | None = None,
72517252
label: SideOptions | None = None,
72527253
offset: pd.Timedelta | datetime.timedelta | str | None = None,
72537254
origin: str | DatetimeLike = "start_day",
72547255
restore_coord_dims: bool | None = None,
7255-
**indexer_kwargs: str
7256-
| datetime.timedelta
7257-
| pd.Timedelta
7258-
| pd.DateOffset
7259-
| Resampler,
7256+
**indexer_kwargs: ResampleCompatible | Resampler,
72607257
) -> DataArrayResample:
72617258
"""Returns a Resample object for performing resampling operations.
72627259
@@ -7267,7 +7264,7 @@ def resample(
72677264
72687265
Parameters
72697266
----------
7270-
indexer : Mapping of Hashable to str, optional
7267+
indexer : Mapping of Hashable to str, datetime.timedelta, pd.Timedelta, pd.DateOffset, or Resampler, optional
72717268
Mapping from the dimension name to resample frequency [1]_. The
72727269
dimension must be datetime-like.
72737270
skipna : bool, optional
@@ -7291,7 +7288,7 @@ def resample(
72917288
restore_coord_dims : bool, optional
72927289
If True, also restore the dimension order of multi-dimensional
72937290
coordinates.
7294-
**indexer_kwargs : str
7291+
**indexer_kwargs : str, datetime.timedelta, pd.Timedelta, pd.DateOffset, or Resampler
72957292
The keyword arguments form of ``indexer``.
72967293
One of indexer or indexer_kwargs must be provided.
72977294

xarray/core/dataset.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
QueryEngineOptions,
163163
QueryParserOptions,
164164
ReindexMethodOptions,
165+
ResampleCompatible,
165166
SideOptions,
166167
T_ChunkDimFreq,
167168
T_DatasetPadConstantValues,
@@ -10685,19 +10686,15 @@ def coarsen(
1068510686
@_deprecate_positional_args("v2024.07.0")
1068610687
def resample(
1068710688
self,
10688-
indexer: Mapping[Any, str | Resampler] | None = None,
10689+
indexer: Mapping[Any, ResampleCompatible | Resampler] | None = None,
1068910690
*,
1069010691
skipna: bool | None = None,
1069110692
closed: SideOptions | None = None,
1069210693
label: SideOptions | None = None,
1069310694
offset: pd.Timedelta | datetime.timedelta | str | None = None,
1069410695
origin: str | DatetimeLike = "start_day",
1069510696
restore_coord_dims: bool | None = None,
10696-
**indexer_kwargs: str
10697-
| datetime.timedelta
10698-
| pd.Timedelta
10699-
| pd.DateOffset
10700-
| Resampler,
10697+
**indexer_kwargs: ResampleCompatible | Resampler,
1070110698
) -> DatasetResample:
1070210699
"""Returns a Resample object for performing resampling operations.
1070310700
@@ -10708,7 +10705,7 @@ def resample(
1070810705
1070910706
Parameters
1071010707
----------
10711-
indexer : Mapping of Hashable to str, optional
10708+
indexer : Mapping of Hashable to str, datetime.timedelta, pd.Timedelta, pd.DateOffset, or Resampler, optional
1071210709
Mapping from the dimension name to resample frequency [1]_. The
1071310710
dimension must be datetime-like.
1071410711
skipna : bool, optional
@@ -10732,7 +10729,7 @@ def resample(
1073210729
restore_coord_dims : bool, optional
1073310730
If True, also restore the dimension order of multi-dimensional
1073410731
coordinates.
10735-
**indexer_kwargs : str
10732+
**indexer_kwargs : str, datetime.timedelta, pd.Timedelta, pd.DateOffset, or Resampler
1073610733
The keyword arguments form of ``indexer``.
1073710734
One of indexer or indexer_kwargs must be provided.
1073810735

xarray/core/resample_cftime.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
from xarray.core.types import SideOptions
5959

6060
if typing.TYPE_CHECKING:
61-
from xarray.core.types import CFTimeDatetime
61+
from xarray.core.types import CFTimeDatetime, ResampleCompatible
6262

6363

6464
class CFTimeGrouper:
@@ -75,11 +75,7 @@ class CFTimeGrouper:
7575

7676
def __init__(
7777
self,
78-
freq: str
79-
| datetime.timedelta
80-
| pd.Timedelta
81-
| pd.DateOffset
82-
| BaseCFTimeOffset,
78+
freq: ResampleCompatible | BaseCFTimeOffset,
8379
closed: SideOptions | None = None,
8480
label: SideOptions | None = None,
8581
origin: str | CFTimeDatetime = "start_day",

xarray/core/types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,5 @@ def copy(
309309
Bins = Union[
310310
int, Sequence[int], Sequence[float], Sequence[pd.Timestamp], np.ndarray, pd.Index
311311
]
312+
313+
ResampleCompatible = Union[str, datetime.timedelta, pd.Timedelta, pd.DateOffset]

xarray/groupers.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@
2121
from xarray.core.groupby import T_Group, _DummyGroup
2222
from xarray.core.indexes import safe_cast_to_index
2323
from xarray.core.resample_cftime import CFTimeGrouper
24-
from xarray.core.types import Bins, DatetimeLike, GroupIndices, SideOptions
24+
from xarray.core.types import (
25+
Bins,
26+
DatetimeLike,
27+
GroupIndices,
28+
ResampleCompatible,
29+
SideOptions,
30+
)
2531
from xarray.core.variable import Variable
2632

2733
__all__ = [
@@ -336,7 +342,7 @@ class TimeResampler(Resampler):
336342
337343
Attributes
338344
----------
339-
freq : str, datetime.timedelta, pandas.Timestamp, pandas.DateOffset
345+
freq : str, datetime.timedelta, pandas.Timestamp, or pandas.DateOffset
340346
Frequency to resample to. See `Pandas frequency
341347
aliases <https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases>`_
342348
for a list of possible values.
@@ -358,7 +364,7 @@ class TimeResampler(Resampler):
358364
An offset timedelta added to the origin.
359365
"""
360366

361-
freq: str | datetime.timedelta | pd.Timedelta | pd.DateOffset
367+
freq: ResampleCompatible
362368
closed: SideOptions | None = field(default=None)
363369
label: SideOptions | None = field(default=None)
364370
origin: str | DatetimeLike = field(default="start_day")

0 commit comments

Comments
 (0)