Skip to content

Commit b34d8c7

Browse files
authored
CLN: Rolling attributes (#38664)
1 parent 3f3c852 commit b34d8c7

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed

pandas/core/window/rolling.py

+22-36
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from pandas._typing import ArrayLike, Axis, FrameOrSeries, FrameOrSeriesUnion
2828
from pandas.compat._optional import import_optional_dependency
2929
from pandas.compat.numpy import function as nv
30-
from pandas.util._decorators import Appender, Substitution, cache_readonly, doc
30+
from pandas.util._decorators import Appender, Substitution, doc
3131

3232
from pandas.core.dtypes.common import (
3333
ensure_float64,
@@ -110,16 +110,23 @@ def __init__(
110110
self.win_type = win_type
111111
self.axis = obj._get_axis_number(axis) if axis is not None else None
112112
self._win_freq_i8 = None
113+
if self.on is None:
114+
if self.axis == 0:
115+
self._on = self.obj.index
116+
else:
117+
# i.e. self.axis == 1
118+
self._on = self.obj.columns
119+
elif isinstance(self.on, Index):
120+
self._on = self.on
121+
elif isinstance(self.obj, ABCDataFrame) and self.on in self.obj.columns:
122+
self._on = Index(self.obj[self.on])
123+
else:
124+
raise ValueError(
125+
f"invalid on specified as {self.on}, "
126+
"must be a column (of DataFrame), an Index or None"
127+
)
113128
self.validate()
114129

115-
@property
116-
def is_datetimelike(self) -> Optional[bool]:
117-
return None
118-
119-
@property
120-
def _on(self):
121-
return None
122-
123130
def validate(self) -> None:
124131
if self.center is not None and not is_bool(self.center):
125132
raise ValueError("center must be a boolean")
@@ -1822,37 +1829,16 @@ def _get_corr(a, b):
18221829

18231830

18241831
class Rolling(RollingAndExpandingMixin):
1825-
@cache_readonly
1826-
def is_datetimelike(self) -> bool:
1827-
return isinstance(
1828-
self._on, (ABCDatetimeIndex, ABCTimedeltaIndex, ABCPeriodIndex)
1829-
)
1830-
1831-
@cache_readonly
1832-
def _on(self) -> Index:
1833-
if self.on is None:
1834-
if self.axis == 0:
1835-
return self.obj.index
1836-
else:
1837-
# i.e. self.axis == 1
1838-
return self.obj.columns
1839-
elif isinstance(self.on, Index):
1840-
return self.on
1841-
elif isinstance(self.obj, ABCDataFrame) and self.on in self.obj.columns:
1842-
return Index(self.obj[self.on])
1843-
else:
1844-
raise ValueError(
1845-
f"invalid on specified as {self.on}, "
1846-
"must be a column (of DataFrame), an Index or None"
1847-
)
1848-
18491832
def validate(self):
18501833
super().validate()
18511834

18521835
# we allow rolling on a datetimelike index
1853-
if (self.obj.empty or self.is_datetimelike) and isinstance(
1854-
self.window, (str, BaseOffset, timedelta)
1855-
):
1836+
if (
1837+
self.obj.empty
1838+
or isinstance(
1839+
self._on, (ABCDatetimeIndex, ABCTimedeltaIndex, ABCPeriodIndex)
1840+
)
1841+
) and isinstance(self.window, (str, BaseOffset, timedelta)):
18561842

18571843
self._validate_monotonic()
18581844

0 commit comments

Comments
 (0)