Skip to content
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
2 changes: 0 additions & 2 deletions pandas/api/typing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from pandas.core.indexes.frozen import FrozenList
from pandas.core.resample import (
DatetimeIndexResamplerGroupby,
PeriodIndexResamplerGroupby,
Resampler,
TimedeltaIndexResamplerGroupby,
TimeGrouper,
Expand Down Expand Up @@ -48,7 +47,6 @@
"NAType",
"NaTType",
"NoDefault",
"PeriodIndexResamplerGroupby",
"Resampler",
"Rolling",
"RollingGroupby",
Expand Down
6 changes: 0 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8835,12 +8835,6 @@ def resample(
Which bin edge label to label bucket with. The default is 'left'
for all frequency offsets except for 'ME', 'YE', 'QE', 'BME',
'BA', 'BQE', and 'W' which all have a default of 'right'.
convention : {{'start', 'end', 's', 'e'}}, default 'start'
For `PeriodIndex` only, controls whether to use the start or
end of `rule`.

.. deprecated:: 2.2.0
Convert PeriodIndex to DatetimeIndex before resampling instead.
on : str, optional
For a DataFrame, column to use instead of index for resampling.
Column must be datetime-like.
Expand Down
144 changes: 3 additions & 141 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from pandas._libs import lib
from pandas._libs.tslibs import (
BaseOffset,
IncompatibleFrequency,
NaT,
Period,
Timedelta,
Expand Down Expand Up @@ -79,10 +78,6 @@
)
from pandas.core.reshape.concat import concat

from pandas.tseries.frequencies import (
is_subperiod,
is_superperiod,
)
from pandas.tseries.offsets import (
Day,
Tick,
Expand Down Expand Up @@ -1938,128 +1933,6 @@ def _resampler_cls(self):
return DatetimeIndexResampler


class PeriodIndexResampler(DatetimeIndexResampler):
# error: Incompatible types in assignment (expression has type "PeriodIndex", base
# class "DatetimeIndexResampler" defined the type as "DatetimeIndex")
ax: PeriodIndex # type: ignore[assignment]

@property
def _resampler_for_grouping(self):
# TODO: Enforce in 3.0 (#55968)
warnings.warn(
"Resampling a groupby with a PeriodIndex is deprecated. "
"Cast to DatetimeIndex before resampling instead.",
FutureWarning, # pdlint: ignore[warning_class]
stacklevel=find_stack_level(),
)
return PeriodIndexResamplerGroupby

def _get_binner_for_time(self):
if isinstance(self.ax, DatetimeIndex):
return super()._get_binner_for_time()
return self._timegrouper._get_period_bins(self.ax)

def _convert_obj(self, obj: NDFrameT) -> NDFrameT:
obj = super()._convert_obj(obj)

if self._from_selection:
# see GH 14008, GH 12871
msg = (
"Resampling from level= or on= selection "
"with a PeriodIndex is not currently supported, "
"use .set_index(...) to explicitly set index"
)
raise NotImplementedError(msg)

# convert to timestamp
if isinstance(obj, DatetimeIndex):
obj = obj.to_timestamp(how=self.convention)

return obj

def _downsample(self, how, **kwargs):
"""
Downsample the cython defined function.

Parameters
----------
how : string / cython mapped function
**kwargs : kw args passed to how function
"""
# we may need to actually resample as if we are timestamps
if isinstance(self.ax, DatetimeIndex):
return super()._downsample(how, **kwargs)

ax = self.ax

if is_subperiod(ax.freq, self.freq):
# Downsampling
return self._groupby_and_aggregate(how, **kwargs)
elif is_superperiod(ax.freq, self.freq):
if how == "ohlc":
# GH #13083
# upsampling to subperiods is handled as an asfreq, which works
# for pure aggregating/reducing methods
# OHLC reduces along the time dimension, but creates multiple
# values for each period -> handle by _groupby_and_aggregate()
return self._groupby_and_aggregate(how)
return self.asfreq()
elif ax.freq == self.freq:
return self.asfreq()

raise IncompatibleFrequency(
f"Frequency {ax.freq} cannot be resampled to {self.freq}, "
"as they are not sub or super periods"
)

def _upsample(self, method, limit: int | None = None, fill_value=None):
"""
Parameters
----------
method : {'backfill', 'bfill', 'pad', 'ffill'}
Method for upsampling.
limit : int, default None
Maximum size gap to fill when reindexing.
fill_value : scalar, default None
Value to use for missing values.
"""
# we may need to actually resample as if we are timestamps
if isinstance(self.ax, DatetimeIndex):
return super()._upsample(method, limit=limit, fill_value=fill_value)

ax = self.ax
obj = self.obj
new_index = self.binner

# Start vs. end of period
memb = ax.asfreq(self.freq, how=self.convention)

# Get the fill indexer
if method == "asfreq":
method = None
indexer = memb.get_indexer(new_index, method=method, limit=limit)
new_obj = _take_new_index(
obj,
indexer,
new_index,
)
return self._wrap_result(new_obj)


# error: Definition of "ax" in base class "_GroupByMixin" is incompatible with
# definition in base class "PeriodIndexResampler"
class PeriodIndexResamplerGroupby( # type: ignore[misc]
_GroupByMixin, PeriodIndexResampler
):
"""
Provides a resample of a groupby implementation.
"""

@property
def _resampler_cls(self):
return PeriodIndexResampler


class TimedeltaIndexResampler(DatetimeIndexResampler):
# error: Incompatible types in assignment (expression has type "TimedeltaIndex",
# base class "DatetimeIndexResampler" defined the type as "DatetimeIndex")
Expand Down Expand Up @@ -2292,20 +2165,9 @@ def _get_resampler(self, obj: NDFrame) -> Resampler:
gpr_index=ax,
)
elif isinstance(ax, PeriodIndex):
if isinstance(ax, PeriodIndex):
# TODO: Enforce in 3.0 (#53481)
# GH#53481
warnings.warn(
"Resampling with a PeriodIndex is deprecated. "
"Cast index to DatetimeIndex before resampling instead.",
FutureWarning, # pdlint: ignore[warning_class]
stacklevel=find_stack_level(),
)
return PeriodIndexResampler(
obj,
timegrouper=self,
group_keys=self.group_keys,
gpr_index=ax,
raise TypeError(
"Resampling with a PeriodIndex is not supported. "
"Cast index to DatetimeIndex before resampling instead.",
)
elif isinstance(ax, TimedeltaIndex):
return TimedeltaIndexResampler(
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ class TestApi(Base):
"NaTType",
"NAType",
"NoDefault",
"PeriodIndexResamplerGroupby",
"Resampler",
"Rolling",
"RollingGroupby",
Expand Down
Loading
Loading