@@ -70,7 +70,7 @@ def __init__(
70
70
center : Optional [bool ] = False ,
71
71
win_type : Optional [str ] = None ,
72
72
axis : Axis = 0 ,
73
- on : Optional [str ] = None ,
73
+ on : Optional [Union [ str , Index ] ] = None ,
74
74
closed : Optional [str ] = None ,
75
75
** kwargs
76
76
):
@@ -126,7 +126,7 @@ def _create_blocks(self):
126
126
obj = self ._selected_obj
127
127
128
128
# filter out the on from the object
129
- if self .on is not None :
129
+ if self .on is not None and not isinstance ( self . on , Index ) :
130
130
if obj .ndim == 2 :
131
131
obj = obj .reindex (columns = obj .columns .difference ([self .on ]), copy = False )
132
132
blocks = obj ._to_dict_of_blocks (copy = False ).values ()
@@ -637,10 +637,10 @@ class Window(_Window):
637
637
Provide a window type. If ``None``, all points are evenly weighted.
638
638
See the notes below for further information.
639
639
on : str, optional
640
- For a DataFrame, a datetime-like column on which to calculate the rolling
641
- window, rather than the DataFrame's index. Provided integer column is
642
- ignored and excluded from result since an integer index is not used to
643
- calculate the rolling window.
640
+ For a DataFrame, a datetime-like column or MultiIndex level on which
641
+ to calculate the rolling window, rather than the DataFrame's index.
642
+ Provided integer column is ignored and excluded from result since
643
+ an integer index is not used to calculate the rolling window.
644
644
axis : int or str, default 0
645
645
closed : str, default None
646
646
Make the interval closed on the 'right', 'left', 'both' or
@@ -1651,18 +1651,19 @@ def is_datetimelike(self):
1651
1651
1652
1652
@cache_readonly
1653
1653
def _on (self ):
1654
-
1655
1654
if self .on is None :
1656
1655
if self .axis == 0 :
1657
1656
return self .obj .index
1658
1657
elif self .axis == 1 :
1659
1658
return self .obj .columns
1659
+ elif isinstance (self .on , Index ):
1660
+ return self .on
1660
1661
elif isinstance (self .obj , ABCDataFrame ) and self .on in self .obj .columns :
1661
1662
return Index (self .obj [self .on ])
1662
1663
else :
1663
1664
raise ValueError (
1664
1665
"invalid on specified as {0}, "
1665
- "must be a column (if DataFrame) "
1666
+ "must be a column (of DataFrame), an Index "
1666
1667
"or None" .format (self .on )
1667
1668
)
1668
1669
@@ -1706,10 +1707,12 @@ def validate(self):
1706
1707
1707
1708
def _validate_monotonic (self ):
1708
1709
"""
1709
- Validate on is_monotonic .
1710
+ Validate monotonic (increasing or decreasing) .
1710
1711
"""
1711
- if not self ._on .is_monotonic :
1712
- formatted = self .on or "index"
1712
+ if not (self ._on .is_monotonic_increasing or self ._on .is_monotonic_decreasing ):
1713
+ formatted = self .on
1714
+ if self .on is None :
1715
+ formatted = "index"
1713
1716
raise ValueError ("{0} must be monotonic" .format (formatted ))
1714
1717
1715
1718
def _validate_freq (self ):
0 commit comments