|
27 | 27 | from pandas._typing import ArrayLike, Axis, FrameOrSeries, FrameOrSeriesUnion
|
28 | 28 | from pandas.compat._optional import import_optional_dependency
|
29 | 29 | 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 |
31 | 31 |
|
32 | 32 | from pandas.core.dtypes.common import (
|
33 | 33 | ensure_float64,
|
@@ -110,16 +110,23 @@ def __init__(
|
110 | 110 | self.win_type = win_type
|
111 | 111 | self.axis = obj._get_axis_number(axis) if axis is not None else None
|
112 | 112 | 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 | + ) |
113 | 128 | self.validate()
|
114 | 129 |
|
115 |
| - @property |
116 |
| - def is_datetimelike(self) -> Optional[bool]: |
117 |
| - return None |
118 |
| - |
119 |
| - @property |
120 |
| - def _on(self): |
121 |
| - return None |
122 |
| - |
123 | 130 | def validate(self) -> None:
|
124 | 131 | if self.center is not None and not is_bool(self.center):
|
125 | 132 | raise ValueError("center must be a boolean")
|
@@ -1822,37 +1829,16 @@ def _get_corr(a, b):
|
1822 | 1829 |
|
1823 | 1830 |
|
1824 | 1831 | 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 |
| - |
1849 | 1832 | def validate(self):
|
1850 | 1833 | super().validate()
|
1851 | 1834 |
|
1852 | 1835 | # 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)): |
1856 | 1842 |
|
1857 | 1843 | self._validate_monotonic()
|
1858 | 1844 |
|
|
0 commit comments