Skip to content

ENH: consistency of input args for boundaries for resample #48187

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

Closed
Closed
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
12 changes: 6 additions & 6 deletions doc/source/user_guide/timeseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1634,14 +1634,14 @@ a method of the returned object, including ``sum``, ``mean``, ``std``, ``sem``,
ts.resample("5Min").max()


For downsampling, ``closed`` can be set to 'left' or 'right' to specify which
For downsampling, ``inclusive`` can be set to 'left' or 'right' to specify which
end of the interval is closed:

.. ipython:: python

ts.resample("5Min", closed="right").mean()
ts.resample("5Min", inclusive="right").mean()

ts.resample("5Min", closed="left").mean()
ts.resample("5Min", inclusive="left").mean()

Parameters like ``label`` are used to manipulate the resulting labels.
``label`` specifies whether the result is labeled with the beginning or
Expand All @@ -1655,7 +1655,7 @@ the end of the interval.

.. warning::

The default values for ``label`` and ``closed`` is '**left**' for all
The default values for ``label`` and ``inclusive`` is '**left**' for all
frequency offsets except for 'M', 'A', 'Q', 'BM', 'BA', 'BQ', and 'W'
which all have a default of 'right'.

Expand All @@ -1669,7 +1669,7 @@ the end of the interval.
s.iloc[2] = pd.NaT
s.dt.day_name()

# default: label='left', closed='left'
# default: label='left', inclusive='left'
s.resample("B").last().dt.day_name()

Notice how the value for Sunday got pulled back to the previous Friday.
Expand All @@ -1678,7 +1678,7 @@ the end of the interval.

.. ipython:: python

s.resample("B", label="right", closed="right").last().dt.day_name()
s.resample("B", label="right", inclusive="right").last().dt.day_name()

The ``axis`` parameter can be set to 0 or 1 and allows you to resample the
specified axis for a ``DataFrame``.
Expand Down
12 changes: 6 additions & 6 deletions doc/source/user_guide/window.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ This can also be applied to datetime-like indices.
Rolling window endpoints
~~~~~~~~~~~~~~~~~~~~~~~~

The inclusion of the interval endpoints in rolling window calculations can be specified with the ``closed``
The inclusion of the interval endpoints in rolling window calculations can be specified with the ``inclusive``
parameter:

============= ====================
Expand Down Expand Up @@ -225,10 +225,10 @@ from present information back to past information. This allows the rolling windo
],
)

df["right"] = df.rolling("2s", closed="right").x.sum() # default
df["both"] = df.rolling("2s", closed="both").x.sum()
df["left"] = df.rolling("2s", closed="left").x.sum()
df["neither"] = df.rolling("2s", closed="neither").x.sum()
df["right"] = df.rolling("2s", inclusive="right").x.sum() # default
df["both"] = df.rolling("2s", inclusive="both").x.sum()
df["left"] = df.rolling("2s", inclusive="left").x.sum()
df["neither"] = df.rolling("2s", inclusive="neither").x.sum()

df

Expand Down Expand Up @@ -264,7 +264,7 @@ and we want to use an expanding window where ``use_expanding`` is ``True`` other
In [2]: from pandas.api.indexers import BaseIndexer

In [3]: class CustomIndexer(BaseIndexer):
...: def get_window_bounds(self, num_values, min_periods, center, closed):
...: def get_window_bounds(self, num_values, min_periods, center, inclusive):
...: start = np.empty(num_values, dtype=np.int64)
...: end = np.empty(num_values, dtype=np.int64)
...: for i in range(num_values):
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -11398,8 +11398,8 @@ def asfreq(
def resample(
self,
rule,
axis: Axis = 0,
closed: str | None = None,
axis=0,
inclusive: str | None = None,
label: str | None = None,
convention: str = "start",
kind: str | None = None,
Expand All @@ -11414,7 +11414,7 @@ def resample(
return super().resample(
rule=rule,
axis=axis,
closed=closed,
inclusive=inclusive,
label=label,
convention=convention,
kind=kind,
Expand Down
16 changes: 8 additions & 8 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8406,8 +8406,8 @@ def between_time(
def resample(
self,
rule,
axis: Axis = 0,
closed: str | None = None,
axis=0,
inclusive: str | None = None,
label: str | None = None,
convention: str = "start",
kind: str | None = None,
Expand Down Expand Up @@ -8435,7 +8435,7 @@ def resample(
Which axis to use for up- or down-sampling. For `Series` this parameter
is unused and defaults to 0. Must be
`DatetimeIndex`, `TimedeltaIndex` or `PeriodIndex`.
closed : {{'right', 'left'}}, default None
inclusive : {{'right', 'left'}}, default None
Which side of bin interval is closed. The default is 'left'
for all frequency offsets except for 'M', 'A', 'Q', 'BM',
'BA', 'BQ', and 'W' which all have a default of 'right'.
Expand Down Expand Up @@ -8570,7 +8570,7 @@ def resample(
Downsample the series into 3 minute bins as above, but close the right
side of the bin interval.

>>> series.resample('3T', label='right', closed='right').sum()
>>> series.resample('3T', label='right', inclusive='right').sum()
2000-01-01 00:00:00 0
2000-01-01 00:03:00 6
2000-01-01 00:06:00 15
Expand Down Expand Up @@ -8832,7 +8832,7 @@ def resample(
self,
freq=rule,
label=label,
closed=closed,
inclusive=inclusive,
axis=axis,
kind=kind,
loffset=loffset,
Expand Down Expand Up @@ -11950,7 +11950,7 @@ def rolling(
win_type: str | None = None,
on: str | None = None,
axis: Axis = 0,
closed: str | None = None,
inclusive: str | None = None,
step: int | None = None,
method: str = "single",
) -> Window | Rolling:
Expand All @@ -11965,7 +11965,7 @@ def rolling(
win_type=win_type,
on=on,
axis=axis,
closed=closed,
inclusive=inclusive,
step=step,
method=method,
)
Expand All @@ -11978,7 +11978,7 @@ def rolling(
win_type=win_type,
on=on,
axis=axis,
closed=closed,
inclusive=inclusive,
step=step,
method=method,
)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2762,7 +2762,7 @@ def resample(self, rule, *args, **kwargs):
Downsample the series into 3 minute bins as above, but close the right
side of the bin interval.

>>> df.groupby('a').resample('3T', closed='right').sum()
>>> df.groupby('a').resample('3T', inclusive='right').sum()
a b
a
0 1999-12-31 23:57:00 0 1
Expand All @@ -2773,7 +2773,7 @@ def resample(self, rule, *args, **kwargs):
the bin interval, but label each bin using the right edge instead of
the left.

>>> df.groupby('a').resample('3T', closed='right', label='right').sum()
>>> df.groupby('a').resample('3T', inclusive='right', label='right').sum()
a b
a
0 2000-01-01 00:00:00 0 1
Expand Down
40 changes: 20 additions & 20 deletions pandas/core/indexers/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
min_periods passed from the top level rolling API
center : bool, default None
center passed from the top level rolling API
closed : str, default None
closed passed from the top level rolling API
inclusive : str, default None
inclusive passed from the top level rolling API
step : int, default None
step passed from the top level rolling API
.. versionadded:: 1.5
Expand Down Expand Up @@ -64,7 +64,7 @@ def get_window_bounds(
num_values: int = 0,
min_periods: int | None = None,
center: bool | None = None,
closed: str | None = None,
inclusive: str | None = None,
step: int | None = None,
) -> tuple[np.ndarray, np.ndarray]:

Expand All @@ -80,7 +80,7 @@ def get_window_bounds(
num_values: int = 0,
min_periods: int | None = None,
center: bool | None = None,
closed: str | None = None,
inclusive: str | None = None,
step: int | None = None,
) -> tuple[np.ndarray, np.ndarray]:

Expand All @@ -91,9 +91,9 @@ def get_window_bounds(

end = np.arange(1 + offset, num_values + 1 + offset, step, dtype="int64")
start = end - self.window_size
if closed in ["left", "both"]:
if inclusive in ["left", "both"]:
start -= 1
if closed in ["left", "neither"]:
if inclusive in ["left", "neither"]:
end -= 1

end = np.clip(end, 0, num_values)
Expand All @@ -111,7 +111,7 @@ def get_window_bounds(
num_values: int = 0,
min_periods: int | None = None,
center: bool | None = None,
closed: str | None = None,
inclusive: str | None = None,
step: int | None = None,
) -> tuple[np.ndarray, np.ndarray]:

Expand All @@ -124,7 +124,7 @@ def get_window_bounds(
self.window_size,
min_periods,
center, # type: ignore[arg-type]
closed,
inclusive,
self.index_array, # type: ignore[arg-type]
)

Expand All @@ -150,7 +150,7 @@ def get_window_bounds(
num_values: int = 0,
min_periods: int | None = None,
center: bool | None = None,
closed: str | None = None,
inclusive: str | None = None,
step: int | None = None,
) -> tuple[np.ndarray, np.ndarray]:

Expand All @@ -160,11 +160,11 @@ def get_window_bounds(
return np.empty(0, dtype="int64"), np.empty(0, dtype="int64")

# if windows is variable, default is 'right', otherwise default is 'both'
if closed is None:
closed = "right" if self.index is not None else "both"
if inclusive is None:
inclusive = "right" if self.index is not None else "both"

right_closed = closed in ["right", "both"]
left_closed = closed in ["left", "both"]
right_closed = inclusive in ["right", "both"]
left_closed = inclusive in ["left", "both"]

if self.index[num_values - 1] < self.index[0]:
index_growth_sign = -1
Expand Down Expand Up @@ -226,7 +226,7 @@ def get_window_bounds(
num_values: int = 0,
min_periods: int | None = None,
center: bool | None = None,
closed: str | None = None,
inclusive: str | None = None,
step: int | None = None,
) -> tuple[np.ndarray, np.ndarray]:

Expand Down Expand Up @@ -267,15 +267,15 @@ def get_window_bounds(
num_values: int = 0,
min_periods: int | None = None,
center: bool | None = None,
closed: str | None = None,
inclusive: str | None = None,
step: int | None = None,
) -> tuple[np.ndarray, np.ndarray]:

if center:
raise ValueError("Forward-looking windows can't have center=True")
if closed is not None:
if inclusive is not None:
raise ValueError(
"Forward-looking windows don't support setting the closed argument"
"Forward-looking windows don't support setting the inclusive argument"
)
if step is None:
step = 1
Expand Down Expand Up @@ -333,7 +333,7 @@ def get_window_bounds(
num_values: int = 0,
min_periods: int | None = None,
center: bool | None = None,
closed: str | None = None,
inclusive: str | None = None,
step: int | None = None,
) -> tuple[np.ndarray, np.ndarray]:

Expand All @@ -356,7 +356,7 @@ def get_window_bounds(
**self.indexer_kwargs,
)
start, end = indexer.get_window_bounds(
len(indices), min_periods, center, closed, step
len(indices), min_periods, center, inclusive, step
)
start = start.astype(np.int64)
end = end.astype(np.int64)
Expand Down Expand Up @@ -391,7 +391,7 @@ def get_window_bounds(
num_values: int = 0,
min_periods: int | None = None,
center: bool | None = None,
closed: str | None = None,
inclusive: str | None = None,
step: int | None = None,
) -> tuple[np.ndarray, np.ndarray]:

Expand Down
Loading