Skip to content

Commit 9743cef

Browse files
committed
CLN: Enforce deprecation of PeriodIndex in resample
1 parent 2891172 commit 9743cef

File tree

7 files changed

+30
-1352
lines changed

7 files changed

+30
-1352
lines changed

pandas/api/typing/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from pandas.core.indexes.frozen import FrozenList
1515
from pandas.core.resample import (
1616
DatetimeIndexResamplerGroupby,
17-
PeriodIndexResamplerGroupby,
1817
Resampler,
1918
TimedeltaIndexResamplerGroupby,
2019
TimeGrouper,
@@ -48,7 +47,6 @@
4847
"NAType",
4948
"NaTType",
5049
"NoDefault",
51-
"PeriodIndexResamplerGroupby",
5250
"Resampler",
5351
"Rolling",
5452
"RollingGroupby",

pandas/core/generic.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8835,12 +8835,6 @@ def resample(
88358835
Which bin edge label to label bucket with. The default is 'left'
88368836
for all frequency offsets except for 'ME', 'YE', 'QE', 'BME',
88378837
'BA', 'BQE', and 'W' which all have a default of 'right'.
8838-
convention : {{'start', 'end', 's', 'e'}}, default 'start'
8839-
For `PeriodIndex` only, controls whether to use the start or
8840-
end of `rule`.
8841-
8842-
.. deprecated:: 2.2.0
8843-
Convert PeriodIndex to DatetimeIndex before resampling instead.
88448838
on : str, optional
88458839
For a DataFrame, column to use instead of index for resampling.
88468840
Column must be datetime-like.

pandas/core/resample.py

Lines changed: 3 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from pandas._libs import lib
2020
from pandas._libs.tslibs import (
2121
BaseOffset,
22-
IncompatibleFrequency,
2322
NaT,
2423
Period,
2524
Timedelta,
@@ -79,10 +78,6 @@
7978
)
8079
from pandas.core.reshape.concat import concat
8180

82-
from pandas.tseries.frequencies import (
83-
is_subperiod,
84-
is_superperiod,
85-
)
8681
from pandas.tseries.offsets import (
8782
Day,
8883
Tick,
@@ -1938,128 +1933,6 @@ def _resampler_cls(self):
19381933
return DatetimeIndexResampler
19391934

19401935

1941-
class PeriodIndexResampler(DatetimeIndexResampler):
1942-
# error: Incompatible types in assignment (expression has type "PeriodIndex", base
1943-
# class "DatetimeIndexResampler" defined the type as "DatetimeIndex")
1944-
ax: PeriodIndex # type: ignore[assignment]
1945-
1946-
@property
1947-
def _resampler_for_grouping(self):
1948-
# TODO: Enforce in 3.0 (#55968)
1949-
warnings.warn(
1950-
"Resampling a groupby with a PeriodIndex is deprecated. "
1951-
"Cast to DatetimeIndex before resampling instead.",
1952-
FutureWarning, # pdlint: ignore[warning_class]
1953-
stacklevel=find_stack_level(),
1954-
)
1955-
return PeriodIndexResamplerGroupby
1956-
1957-
def _get_binner_for_time(self):
1958-
if isinstance(self.ax, DatetimeIndex):
1959-
return super()._get_binner_for_time()
1960-
return self._timegrouper._get_period_bins(self.ax)
1961-
1962-
def _convert_obj(self, obj: NDFrameT) -> NDFrameT:
1963-
obj = super()._convert_obj(obj)
1964-
1965-
if self._from_selection:
1966-
# see GH 14008, GH 12871
1967-
msg = (
1968-
"Resampling from level= or on= selection "
1969-
"with a PeriodIndex is not currently supported, "
1970-
"use .set_index(...) to explicitly set index"
1971-
)
1972-
raise NotImplementedError(msg)
1973-
1974-
# convert to timestamp
1975-
if isinstance(obj, DatetimeIndex):
1976-
obj = obj.to_timestamp(how=self.convention)
1977-
1978-
return obj
1979-
1980-
def _downsample(self, how, **kwargs):
1981-
"""
1982-
Downsample the cython defined function.
1983-
1984-
Parameters
1985-
----------
1986-
how : string / cython mapped function
1987-
**kwargs : kw args passed to how function
1988-
"""
1989-
# we may need to actually resample as if we are timestamps
1990-
if isinstance(self.ax, DatetimeIndex):
1991-
return super()._downsample(how, **kwargs)
1992-
1993-
ax = self.ax
1994-
1995-
if is_subperiod(ax.freq, self.freq):
1996-
# Downsampling
1997-
return self._groupby_and_aggregate(how, **kwargs)
1998-
elif is_superperiod(ax.freq, self.freq):
1999-
if how == "ohlc":
2000-
# GH #13083
2001-
# upsampling to subperiods is handled as an asfreq, which works
2002-
# for pure aggregating/reducing methods
2003-
# OHLC reduces along the time dimension, but creates multiple
2004-
# values for each period -> handle by _groupby_and_aggregate()
2005-
return self._groupby_and_aggregate(how)
2006-
return self.asfreq()
2007-
elif ax.freq == self.freq:
2008-
return self.asfreq()
2009-
2010-
raise IncompatibleFrequency(
2011-
f"Frequency {ax.freq} cannot be resampled to {self.freq}, "
2012-
"as they are not sub or super periods"
2013-
)
2014-
2015-
def _upsample(self, method, limit: int | None = None, fill_value=None):
2016-
"""
2017-
Parameters
2018-
----------
2019-
method : {'backfill', 'bfill', 'pad', 'ffill'}
2020-
Method for upsampling.
2021-
limit : int, default None
2022-
Maximum size gap to fill when reindexing.
2023-
fill_value : scalar, default None
2024-
Value to use for missing values.
2025-
"""
2026-
# we may need to actually resample as if we are timestamps
2027-
if isinstance(self.ax, DatetimeIndex):
2028-
return super()._upsample(method, limit=limit, fill_value=fill_value)
2029-
2030-
ax = self.ax
2031-
obj = self.obj
2032-
new_index = self.binner
2033-
2034-
# Start vs. end of period
2035-
memb = ax.asfreq(self.freq, how=self.convention)
2036-
2037-
# Get the fill indexer
2038-
if method == "asfreq":
2039-
method = None
2040-
indexer = memb.get_indexer(new_index, method=method, limit=limit)
2041-
new_obj = _take_new_index(
2042-
obj,
2043-
indexer,
2044-
new_index,
2045-
)
2046-
return self._wrap_result(new_obj)
2047-
2048-
2049-
# error: Definition of "ax" in base class "_GroupByMixin" is incompatible with
2050-
# definition in base class "PeriodIndexResampler"
2051-
class PeriodIndexResamplerGroupby( # type: ignore[misc]
2052-
_GroupByMixin, PeriodIndexResampler
2053-
):
2054-
"""
2055-
Provides a resample of a groupby implementation.
2056-
"""
2057-
2058-
@property
2059-
def _resampler_cls(self):
2060-
return PeriodIndexResampler
2061-
2062-
20631936
class TimedeltaIndexResampler(DatetimeIndexResampler):
20641937
# error: Incompatible types in assignment (expression has type "TimedeltaIndex",
20651938
# base class "DatetimeIndexResampler" defined the type as "DatetimeIndex")
@@ -2292,20 +2165,9 @@ def _get_resampler(self, obj: NDFrame) -> Resampler:
22922165
gpr_index=ax,
22932166
)
22942167
elif isinstance(ax, PeriodIndex):
2295-
if isinstance(ax, PeriodIndex):
2296-
# TODO: Enforce in 3.0 (#53481)
2297-
# GH#53481
2298-
warnings.warn(
2299-
"Resampling with a PeriodIndex is deprecated. "
2300-
"Cast index to DatetimeIndex before resampling instead.",
2301-
FutureWarning, # pdlint: ignore[warning_class]
2302-
stacklevel=find_stack_level(),
2303-
)
2304-
return PeriodIndexResampler(
2305-
obj,
2306-
timegrouper=self,
2307-
group_keys=self.group_keys,
2308-
gpr_index=ax,
2168+
raise TypeError(
2169+
"Resampling with a PeriodIndex is not supported. "
2170+
"Cast index to DatetimeIndex before resampling instead.",
23092171
)
23102172
elif isinstance(ax, TimedeltaIndex):
23112173
return TimedeltaIndexResampler(

pandas/tests/api/test_api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ class TestApi(Base):
267267
"NaTType",
268268
"NAType",
269269
"NoDefault",
270-
"PeriodIndexResamplerGroupby",
271270
"Resampler",
272271
"Rolling",
273272
"RollingGroupby",

0 commit comments

Comments
 (0)