|
5 | 5 | DataFrame,
|
6 | 6 | Index,
|
7 | 7 | MultiIndex,
|
| 8 | + NaT, |
8 | 9 | Series,
|
9 | 10 | Timestamp,
|
10 | 11 | date_range,
|
11 |
| - to_datetime, |
12 | 12 | )
|
13 | 13 | import pandas._testing as tm
|
14 | 14 |
|
@@ -134,7 +134,7 @@ def test_non_monotonic_on(self):
|
134 | 134 |
|
135 | 135 | assert not df.index.is_monotonic
|
136 | 136 |
|
137 |
| - msg = "index must be monotonic" |
| 137 | + msg = "index values must be monotonic" |
138 | 138 | with pytest.raises(ValueError, match=msg):
|
139 | 139 | df.rolling("2s").sum()
|
140 | 140 |
|
@@ -643,65 +643,6 @@ def agg_by_day(x):
|
643 | 643 |
|
644 | 644 | tm.assert_frame_equal(result, expected)
|
645 | 645 |
|
646 |
| - def test_groupby_monotonic(self): |
647 |
| - |
648 |
| - # GH 15130 |
649 |
| - # we don't need to validate monotonicity when grouping |
650 |
| - |
651 |
| - # GH 43909 we should raise an error here to match |
652 |
| - # behaviour of non-groupby rolling. |
653 |
| - |
654 |
| - data = [ |
655 |
| - ["David", "1/1/2015", 100], |
656 |
| - ["David", "1/5/2015", 500], |
657 |
| - ["David", "5/30/2015", 50], |
658 |
| - ["David", "7/25/2015", 50], |
659 |
| - ["Ryan", "1/4/2014", 100], |
660 |
| - ["Ryan", "1/19/2015", 500], |
661 |
| - ["Ryan", "3/31/2016", 50], |
662 |
| - ["Joe", "7/1/2015", 100], |
663 |
| - ["Joe", "9/9/2015", 500], |
664 |
| - ["Joe", "10/15/2015", 50], |
665 |
| - ] |
666 |
| - |
667 |
| - df = DataFrame(data=data, columns=["name", "date", "amount"]) |
668 |
| - df["date"] = to_datetime(df["date"]) |
669 |
| - df = df.sort_values("date") |
670 |
| - |
671 |
| - expected = ( |
672 |
| - df.set_index("date") |
673 |
| - .groupby("name") |
674 |
| - .apply(lambda x: x.rolling("180D")["amount"].sum()) |
675 |
| - ) |
676 |
| - result = df.groupby("name").rolling("180D", on="date")["amount"].sum() |
677 |
| - tm.assert_series_equal(result, expected) |
678 |
| - |
679 |
| - def test_non_monotonic_raises(self): |
680 |
| - # GH 13966 (similar to #15130, closed by #15175) |
681 |
| - |
682 |
| - # superseded by 43909 |
683 |
| - |
684 |
| - dates = date_range(start="2016-01-01 09:30:00", periods=20, freq="s") |
685 |
| - df = DataFrame( |
686 |
| - { |
687 |
| - "A": [1] * 20 + [2] * 12 + [3] * 8, |
688 |
| - "B": np.concatenate((dates, dates)), |
689 |
| - "C": np.arange(40), |
690 |
| - } |
691 |
| - ) |
692 |
| - |
693 |
| - expected = ( |
694 |
| - df.set_index("B").groupby("A").apply(lambda x: x.rolling("4s")["C"].mean()) |
695 |
| - ) |
696 |
| - with pytest.raises(ValueError, match=r".* must be monotonic"): |
697 |
| - df.groupby("A").rolling( |
698 |
| - "4s", on="B" |
699 |
| - ).C.mean() # should raise for non-monotonic t series |
700 |
| - |
701 |
| - df2 = df.sort_values("B") |
702 |
| - result = df2.groupby("A").rolling("4s", on="B").C.mean() |
703 |
| - tm.assert_series_equal(result, expected) |
704 |
| - |
705 | 646 | def test_rolling_cov_offset(self):
|
706 | 647 | # GH16058
|
707 | 648 |
|
@@ -757,3 +698,12 @@ def test_rolling_on_multi_index_level(self):
|
757 | 698 | {"column": [0.0, 1.0, 3.0, 6.0, 10.0, 15.0]}, index=df.index
|
758 | 699 | )
|
759 | 700 | tm.assert_frame_equal(result, expected)
|
| 701 | + |
| 702 | + |
| 703 | +@pytest.mark.parametrize("msg, axis", [["column", 1], ["index", 0]]) |
| 704 | +def test_nat_axis_error(msg, axis): |
| 705 | + idx = [Timestamp("2020"), NaT] |
| 706 | + kwargs = {"columns" if axis == 1 else "index": idx} |
| 707 | + df = DataFrame(np.eye(2), **kwargs) |
| 708 | + with pytest.raises(ValueError, match=f"{msg} values must not have NaT"): |
| 709 | + df.rolling("D", axis=axis).mean() |
0 commit comments