Skip to content

Commit 2260a4c

Browse files
jbrockmendelAlexKirko
authored andcommitted
REF: refactor cumulative op tests from test_analytics (pandas-dev#30358)
1 parent af254f3 commit 2260a4c

File tree

4 files changed

+262
-217
lines changed

4 files changed

+262
-217
lines changed

pandas/tests/frame/test_analytics.py

-106
Original file line numberDiff line numberDiff line change
@@ -1495,112 +1495,6 @@ def test_sum_bools(self):
14951495
bools = isna(df)
14961496
assert bools.sum(axis=1)[0] == 10
14971497

1498-
# ---------------------------------------------------------------------
1499-
# Cumulative Reductions - cumsum, cummax, ...
1500-
1501-
def test_cumsum_corner(self):
1502-
dm = DataFrame(np.arange(20).reshape(4, 5), index=range(4), columns=range(5))
1503-
# ?(wesm)
1504-
result = dm.cumsum() # noqa
1505-
1506-
def test_cumsum(self, datetime_frame):
1507-
datetime_frame.loc[5:10, 0] = np.nan
1508-
datetime_frame.loc[10:15, 1] = np.nan
1509-
datetime_frame.loc[15:, 2] = np.nan
1510-
1511-
# axis = 0
1512-
cumsum = datetime_frame.cumsum()
1513-
expected = datetime_frame.apply(Series.cumsum)
1514-
tm.assert_frame_equal(cumsum, expected)
1515-
1516-
# axis = 1
1517-
cumsum = datetime_frame.cumsum(axis=1)
1518-
expected = datetime_frame.apply(Series.cumsum, axis=1)
1519-
tm.assert_frame_equal(cumsum, expected)
1520-
1521-
# works
1522-
df = DataFrame({"A": np.arange(20)}, index=np.arange(20))
1523-
result = df.cumsum() # noqa
1524-
1525-
# fix issue
1526-
cumsum_xs = datetime_frame.cumsum(axis=1)
1527-
assert np.shape(cumsum_xs) == np.shape(datetime_frame)
1528-
1529-
def test_cumprod(self, datetime_frame):
1530-
datetime_frame.loc[5:10, 0] = np.nan
1531-
datetime_frame.loc[10:15, 1] = np.nan
1532-
datetime_frame.loc[15:, 2] = np.nan
1533-
1534-
# axis = 0
1535-
cumprod = datetime_frame.cumprod()
1536-
expected = datetime_frame.apply(Series.cumprod)
1537-
tm.assert_frame_equal(cumprod, expected)
1538-
1539-
# axis = 1
1540-
cumprod = datetime_frame.cumprod(axis=1)
1541-
expected = datetime_frame.apply(Series.cumprod, axis=1)
1542-
tm.assert_frame_equal(cumprod, expected)
1543-
1544-
# fix issue
1545-
cumprod_xs = datetime_frame.cumprod(axis=1)
1546-
assert np.shape(cumprod_xs) == np.shape(datetime_frame)
1547-
1548-
# ints
1549-
df = datetime_frame.fillna(0).astype(int)
1550-
df.cumprod(0)
1551-
df.cumprod(1)
1552-
1553-
# ints32
1554-
df = datetime_frame.fillna(0).astype(np.int32)
1555-
df.cumprod(0)
1556-
df.cumprod(1)
1557-
1558-
def test_cummin(self, datetime_frame):
1559-
datetime_frame.loc[5:10, 0] = np.nan
1560-
datetime_frame.loc[10:15, 1] = np.nan
1561-
datetime_frame.loc[15:, 2] = np.nan
1562-
1563-
# axis = 0
1564-
cummin = datetime_frame.cummin()
1565-
expected = datetime_frame.apply(Series.cummin)
1566-
tm.assert_frame_equal(cummin, expected)
1567-
1568-
# axis = 1
1569-
cummin = datetime_frame.cummin(axis=1)
1570-
expected = datetime_frame.apply(Series.cummin, axis=1)
1571-
tm.assert_frame_equal(cummin, expected)
1572-
1573-
# it works
1574-
df = DataFrame({"A": np.arange(20)}, index=np.arange(20))
1575-
result = df.cummin() # noqa
1576-
1577-
# fix issue
1578-
cummin_xs = datetime_frame.cummin(axis=1)
1579-
assert np.shape(cummin_xs) == np.shape(datetime_frame)
1580-
1581-
def test_cummax(self, datetime_frame):
1582-
datetime_frame.loc[5:10, 0] = np.nan
1583-
datetime_frame.loc[10:15, 1] = np.nan
1584-
datetime_frame.loc[15:, 2] = np.nan
1585-
1586-
# axis = 0
1587-
cummax = datetime_frame.cummax()
1588-
expected = datetime_frame.apply(Series.cummax)
1589-
tm.assert_frame_equal(cummax, expected)
1590-
1591-
# axis = 1
1592-
cummax = datetime_frame.cummax(axis=1)
1593-
expected = datetime_frame.apply(Series.cummax, axis=1)
1594-
tm.assert_frame_equal(cummax, expected)
1595-
1596-
# it works
1597-
df = DataFrame({"A": np.arange(20)}, index=np.arange(20))
1598-
result = df.cummax() # noqa
1599-
1600-
# fix issue
1601-
cummax_xs = datetime_frame.cummax(axis=1)
1602-
assert np.shape(cummax_xs) == np.shape(datetime_frame)
1603-
16041498
# ---------------------------------------------------------------------
16051499
# Miscellanea
16061500

pandas/tests/frame/test_cumulative.py

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"""
2+
Tests for DataFrame cumulative operations
3+
4+
See also
5+
--------
6+
tests.series.test_cumulative
7+
"""
8+
9+
import numpy as np
10+
11+
from pandas import DataFrame, Series
12+
import pandas.util.testing as tm
13+
14+
15+
class TestDataFrameCumulativeOps:
16+
# ---------------------------------------------------------------------
17+
# Cumulative Operations - cumsum, cummax, ...
18+
19+
def test_cumsum_corner(self):
20+
dm = DataFrame(np.arange(20).reshape(4, 5), index=range(4), columns=range(5))
21+
# TODO(wesm): do something with this?
22+
result = dm.cumsum() # noqa
23+
24+
def test_cumsum(self, datetime_frame):
25+
datetime_frame.loc[5:10, 0] = np.nan
26+
datetime_frame.loc[10:15, 1] = np.nan
27+
datetime_frame.loc[15:, 2] = np.nan
28+
29+
# axis = 0
30+
cumsum = datetime_frame.cumsum()
31+
expected = datetime_frame.apply(Series.cumsum)
32+
tm.assert_frame_equal(cumsum, expected)
33+
34+
# axis = 1
35+
cumsum = datetime_frame.cumsum(axis=1)
36+
expected = datetime_frame.apply(Series.cumsum, axis=1)
37+
tm.assert_frame_equal(cumsum, expected)
38+
39+
# works
40+
df = DataFrame({"A": np.arange(20)}, index=np.arange(20))
41+
df.cumsum()
42+
43+
# fix issue
44+
cumsum_xs = datetime_frame.cumsum(axis=1)
45+
assert np.shape(cumsum_xs) == np.shape(datetime_frame)
46+
47+
def test_cumprod(self, datetime_frame):
48+
datetime_frame.loc[5:10, 0] = np.nan
49+
datetime_frame.loc[10:15, 1] = np.nan
50+
datetime_frame.loc[15:, 2] = np.nan
51+
52+
# axis = 0
53+
cumprod = datetime_frame.cumprod()
54+
expected = datetime_frame.apply(Series.cumprod)
55+
tm.assert_frame_equal(cumprod, expected)
56+
57+
# axis = 1
58+
cumprod = datetime_frame.cumprod(axis=1)
59+
expected = datetime_frame.apply(Series.cumprod, axis=1)
60+
tm.assert_frame_equal(cumprod, expected)
61+
62+
# fix issue
63+
cumprod_xs = datetime_frame.cumprod(axis=1)
64+
assert np.shape(cumprod_xs) == np.shape(datetime_frame)
65+
66+
# ints
67+
df = datetime_frame.fillna(0).astype(int)
68+
df.cumprod(0)
69+
df.cumprod(1)
70+
71+
# ints32
72+
df = datetime_frame.fillna(0).astype(np.int32)
73+
df.cumprod(0)
74+
df.cumprod(1)
75+
76+
def test_cummin(self, datetime_frame):
77+
datetime_frame.loc[5:10, 0] = np.nan
78+
datetime_frame.loc[10:15, 1] = np.nan
79+
datetime_frame.loc[15:, 2] = np.nan
80+
81+
# axis = 0
82+
cummin = datetime_frame.cummin()
83+
expected = datetime_frame.apply(Series.cummin)
84+
tm.assert_frame_equal(cummin, expected)
85+
86+
# axis = 1
87+
cummin = datetime_frame.cummin(axis=1)
88+
expected = datetime_frame.apply(Series.cummin, axis=1)
89+
tm.assert_frame_equal(cummin, expected)
90+
91+
# it works
92+
df = DataFrame({"A": np.arange(20)}, index=np.arange(20))
93+
df.cummin()
94+
95+
# fix issue
96+
cummin_xs = datetime_frame.cummin(axis=1)
97+
assert np.shape(cummin_xs) == np.shape(datetime_frame)
98+
99+
def test_cummax(self, datetime_frame):
100+
datetime_frame.loc[5:10, 0] = np.nan
101+
datetime_frame.loc[10:15, 1] = np.nan
102+
datetime_frame.loc[15:, 2] = np.nan
103+
104+
# axis = 0
105+
cummax = datetime_frame.cummax()
106+
expected = datetime_frame.apply(Series.cummax)
107+
tm.assert_frame_equal(cummax, expected)
108+
109+
# axis = 1
110+
cummax = datetime_frame.cummax(axis=1)
111+
expected = datetime_frame.apply(Series.cummax, axis=1)
112+
tm.assert_frame_equal(cummax, expected)
113+
114+
# it works
115+
df = DataFrame({"A": np.arange(20)}, index=np.arange(20))
116+
df.cummax()
117+
118+
# fix issue
119+
cummax_xs = datetime_frame.cummax(axis=1)
120+
assert np.shape(cummax_xs) == np.shape(datetime_frame)

pandas/tests/series/test_analytics.py

-111
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import numpy as np
55
import pytest
66

7-
from pandas.compat.numpy import _np_version_under1p18
87
import pandas.util._test_decorators as td
98

109
import pandas as pd
@@ -125,116 +124,6 @@ def test_argsort_stable(self):
125124
with pytest.raises(AssertionError, match=msg):
126125
tm.assert_numpy_array_equal(qindexer, mindexer)
127126

128-
def test_cumsum(self, datetime_series):
129-
self._check_accum_op("cumsum", datetime_series)
130-
131-
def test_cumprod(self, datetime_series):
132-
self._check_accum_op("cumprod", datetime_series)
133-
134-
def test_cummin(self, datetime_series):
135-
tm.assert_numpy_array_equal(
136-
datetime_series.cummin().values,
137-
np.minimum.accumulate(np.array(datetime_series)),
138-
)
139-
ts = datetime_series.copy()
140-
ts[::2] = np.NaN
141-
result = ts.cummin()[1::2]
142-
expected = np.minimum.accumulate(ts.dropna())
143-
144-
tm.assert_series_equal(result, expected)
145-
146-
def test_cummax(self, datetime_series):
147-
tm.assert_numpy_array_equal(
148-
datetime_series.cummax().values,
149-
np.maximum.accumulate(np.array(datetime_series)),
150-
)
151-
ts = datetime_series.copy()
152-
ts[::2] = np.NaN
153-
result = ts.cummax()[1::2]
154-
expected = np.maximum.accumulate(ts.dropna())
155-
156-
tm.assert_series_equal(result, expected)
157-
158-
@pytest.mark.xfail(
159-
not _np_version_under1p18, reason="numpy 1.18 changed min/max behavior for NaT"
160-
)
161-
def test_cummin_datetime64(self):
162-
s = pd.Series(
163-
pd.to_datetime(["NaT", "2000-1-2", "NaT", "2000-1-1", "NaT", "2000-1-3"])
164-
)
165-
166-
expected = pd.Series(
167-
pd.to_datetime(["NaT", "2000-1-2", "NaT", "2000-1-1", "NaT", "2000-1-1"])
168-
)
169-
result = s.cummin(skipna=True)
170-
tm.assert_series_equal(expected, result)
171-
172-
expected = pd.Series(
173-
pd.to_datetime(
174-
["NaT", "2000-1-2", "2000-1-2", "2000-1-1", "2000-1-1", "2000-1-1"]
175-
)
176-
)
177-
result = s.cummin(skipna=False)
178-
tm.assert_series_equal(expected, result)
179-
180-
@pytest.mark.xfail(
181-
not _np_version_under1p18, reason="numpy 1.18 changed min/max behavior for NaT"
182-
)
183-
def test_cummax_datetime64(self):
184-
s = pd.Series(
185-
pd.to_datetime(["NaT", "2000-1-2", "NaT", "2000-1-1", "NaT", "2000-1-3"])
186-
)
187-
188-
expected = pd.Series(
189-
pd.to_datetime(["NaT", "2000-1-2", "NaT", "2000-1-2", "NaT", "2000-1-3"])
190-
)
191-
result = s.cummax(skipna=True)
192-
tm.assert_series_equal(expected, result)
193-
194-
expected = pd.Series(
195-
pd.to_datetime(
196-
["NaT", "2000-1-2", "2000-1-2", "2000-1-2", "2000-1-2", "2000-1-3"]
197-
)
198-
)
199-
result = s.cummax(skipna=False)
200-
tm.assert_series_equal(expected, result)
201-
202-
@pytest.mark.xfail(
203-
not _np_version_under1p18, reason="numpy 1.18 changed min/max behavior for NaT"
204-
)
205-
def test_cummin_timedelta64(self):
206-
s = pd.Series(pd.to_timedelta(["NaT", "2 min", "NaT", "1 min", "NaT", "3 min"]))
207-
208-
expected = pd.Series(
209-
pd.to_timedelta(["NaT", "2 min", "NaT", "1 min", "NaT", "1 min"])
210-
)
211-
result = s.cummin(skipna=True)
212-
tm.assert_series_equal(expected, result)
213-
214-
expected = pd.Series(
215-
pd.to_timedelta(["NaT", "2 min", "2 min", "1 min", "1 min", "1 min"])
216-
)
217-
result = s.cummin(skipna=False)
218-
tm.assert_series_equal(expected, result)
219-
220-
@pytest.mark.xfail(
221-
not _np_version_under1p18, reason="numpy 1.18 changed min/max behavior for NaT"
222-
)
223-
def test_cummax_timedelta64(self):
224-
s = pd.Series(pd.to_timedelta(["NaT", "2 min", "NaT", "1 min", "NaT", "3 min"]))
225-
226-
expected = pd.Series(
227-
pd.to_timedelta(["NaT", "2 min", "NaT", "2 min", "NaT", "3 min"])
228-
)
229-
result = s.cummax(skipna=True)
230-
tm.assert_series_equal(expected, result)
231-
232-
expected = pd.Series(
233-
pd.to_timedelta(["NaT", "2 min", "2 min", "2 min", "2 min", "3 min"])
234-
)
235-
result = s.cummax(skipna=False)
236-
tm.assert_series_equal(expected, result)
237-
238127
def test_np_diff(self):
239128
pytest.skip("skipping due to Series no longer being an ndarray")
240129

0 commit comments

Comments
 (0)