Skip to content

TYP: resample #41126

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

Merged
merged 3 commits into from
Apr 28, 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
50 changes: 39 additions & 11 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import timedelta
from textwrap import dedent
from typing import (
TYPE_CHECKING,
Callable,
no_type_check,
)
Expand All @@ -12,6 +13,7 @@

from pandas._libs import lib
from pandas._libs.tslibs import (
BaseOffset,
IncompatibleFrequency,
NaT,
Period,
Expand Down Expand Up @@ -84,6 +86,9 @@
Tick,
)

if TYPE_CHECKING:
from typing import Literal

_shared_docs_kwargs: dict[str, str] = {}


Expand Down Expand Up @@ -489,11 +494,11 @@ def _apply_loffset(self, result):
self.loffset = None
return result

def _get_resampler_for_grouping(self, groupby, **kwargs):
def _get_resampler_for_grouping(self, groupby):
"""
Return the correct class for resampling with groupby.
"""
return self._resampler_for_grouping(self, groupby=groupby, **kwargs)
return self._resampler_for_grouping(self, groupby=groupby)

def _wrap_result(self, result):
"""
Expand Down Expand Up @@ -1039,9 +1044,10 @@ class _GroupByMixin(PandasObject):
Provide the groupby facilities.
"""

_attributes: list[str]
_attributes: list[str] # in practice the same as Resampler._attributes

def __init__(self, obj, *args, **kwargs):
def __init__(self, obj, **kwargs):
# reached via ._gotitem and _get_resampler_for_grouping

parent = kwargs.pop("parent", None)
groupby = kwargs.pop("groupby", None)
Expand Down Expand Up @@ -1450,7 +1456,7 @@ class TimeGrouper(Grouper):
def __init__(
self,
freq="Min",
closed: str | None = None,
closed: Literal["left", "right"] | None = None,
label: str | None = None,
how="mean",
axis=0,
Expand Down Expand Up @@ -1822,8 +1828,13 @@ def _take_new_index(


def _get_timestamp_range_edges(
first, last, freq, closed="left", origin="start_day", offset=None
):
first: Timestamp,
last: Timestamp,
freq: BaseOffset,
closed: Literal["right", "left"] = "left",
origin="start_day",
offset: Timedelta | None = None,
) -> tuple[Timestamp, Timestamp]:
"""
Adjust the `first` Timestamp to the preceding Timestamp that resides on
the provided offset. Adjust the `last` Timestamp to the following
Expand Down Expand Up @@ -1895,8 +1906,13 @@ def _get_timestamp_range_edges(


def _get_period_range_edges(
first, last, freq, closed="left", origin="start_day", offset=None
):
first: Period,
last: Period,
freq: BaseOffset,
closed: Literal["right", "left"] = "left",
origin="start_day",
offset: Timedelta | None = None,
) -> tuple[Period, Period]:
"""
Adjust the provided `first` and `last` Periods to the respective Period of
the given offset that encompasses them.
Expand Down Expand Up @@ -1959,7 +1975,12 @@ def _insert_nat_bin(


def _adjust_dates_anchored(
first, last, freq, closed="right", origin="start_day", offset=None
first,
last,
freq,
closed: Literal["right", "left"] = "right",
origin="start_day",
offset: Timedelta | None = None,
):
# First and last offsets should be calculated from the start day to fix an
# error cause by resampling across multiple days when a one day period is
Expand Down Expand Up @@ -2029,7 +2050,14 @@ def _adjust_dates_anchored(
return fresult, lresult


def asfreq(obj, freq, method=None, how=None, normalize=False, fill_value=None):
def asfreq(
obj: FrameOrSeries,
freq,
method=None,
how=None,
normalize: bool = False,
fill_value=None,
) -> FrameOrSeries:
"""
Utility frequency conversion method for Series/DataFrame.

Expand Down
14 changes: 10 additions & 4 deletions pandas/core/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def decons_group_index(comp_labels, shape):
return label_list[::-1]


def decons_obs_group_ids(comp_ids, obs_ids, shape, labels, xnull: bool):
def decons_obs_group_ids(comp_ids: np.ndarray, obs_ids, shape, labels, xnull: bool):
"""
Reconstruct labels from observed group ids.

Expand Down Expand Up @@ -360,6 +360,10 @@ def nargsort(
key : Optional[Callable], default None
mask : Optional[np.ndarray], default None
Passed when called by ExtensionArray.argsort.

Returns
-------
np.ndarray[np.intp]
"""

if key is not None:
Expand Down Expand Up @@ -404,7 +408,7 @@ def nargsort(
indexer = np.concatenate([nan_idx, indexer])
else:
raise ValueError(f"invalid na_position: {na_position}")
return indexer
return ensure_platform_int(indexer)


def nargminmax(values, method: str, axis: int = 0):
Expand Down Expand Up @@ -644,7 +648,9 @@ def get_group_index_sorter(
return ensure_platform_int(sorter)


def compress_group_index(group_index, sort: bool = True):
def compress_group_index(
group_index: np.ndarray, sort: bool = True
) -> tuple[np.ndarray, np.ndarray]:
"""
Group_index is offsets into cartesian product of all possible labels. This
space can be huge, so this function compresses it, by computing offsets
Expand Down Expand Up @@ -682,7 +688,7 @@ def _reorder_by_uniques(
sorter = uniques.argsort()

# reverse_indexer is where elements came from
reverse_indexer = np.empty(len(sorter), dtype=np.int64)
reverse_indexer = np.empty(len(sorter), dtype=np.intp)
reverse_indexer.put(sorter, np.arange(len(sorter)))

mask = labels < 0
Expand Down
12 changes: 5 additions & 7 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@
)
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCDatetimeIndex,
ABCPeriodIndex,
ABCSeries,
ABCTimedeltaIndex,
)
from pandas.core.dtypes.missing import notna

Expand All @@ -58,8 +55,11 @@
)
import pandas.core.common as com
from pandas.core.indexes.api import (
DatetimeIndex,
Index,
MultiIndex,
PeriodIndex,
TimedeltaIndex,
)
from pandas.core.internals import ArrayManager
from pandas.core.reshape.concat import concat
Expand Down Expand Up @@ -1455,9 +1455,7 @@ def validate(self):
# we allow rolling on a datetimelike index
if (
self.obj.empty
or isinstance(
self._on, (ABCDatetimeIndex, ABCTimedeltaIndex, ABCPeriodIndex)
)
or isinstance(self._on, (DatetimeIndex, TimedeltaIndex, PeriodIndex))
) and isinstance(self.window, (str, BaseOffset, timedelta)):

self._validate_monotonic()
Expand All @@ -1470,7 +1468,7 @@ def validate(self):
f"passed window {self.window} is not "
"compatible with a datetimelike index"
) from err
if isinstance(self._on, ABCPeriodIndex):
if isinstance(self._on, PeriodIndex):
self._win_freq_i8 = freq.nanos / (self._on.freq.nanos / self._on.freq.n)
else:
self._win_freq_i8 = freq.nanos
Expand Down