6
6
from pandas .core .dtypes .common import ensure_int64
7
7
8
8
from pandas import Index , Series , isna
9
- from pandas .core .groupby .ops import generate_bins_generic
10
9
import pandas .util .testing as tm
11
- from pandas .util .testing import assert_almost_equal
12
10
13
11
14
12
def test_series_grouper ():
@@ -21,10 +19,10 @@ def test_series_grouper():
21
19
result , counts = grouper .get_result ()
22
20
23
21
expected = np .array ([obj [3 :6 ].mean (), obj [6 :].mean ()])
24
- assert_almost_equal (result , expected )
22
+ tm . assert_almost_equal (result , expected )
25
23
26
24
exp_counts = np .array ([3 , 4 ], dtype = np .int64 )
27
- assert_almost_equal (counts , exp_counts )
25
+ tm . assert_almost_equal (counts , exp_counts )
28
26
29
27
30
28
def test_series_bin_grouper ():
@@ -37,48 +35,37 @@ def test_series_bin_grouper():
37
35
result , counts = grouper .get_result ()
38
36
39
37
expected = np .array ([obj [:3 ].mean (), obj [3 :6 ].mean (), obj [6 :].mean ()])
40
- assert_almost_equal (result , expected )
38
+ tm . assert_almost_equal (result , expected )
41
39
42
40
exp_counts = np .array ([3 , 3 , 4 ], dtype = np .int64 )
43
- assert_almost_equal (counts , exp_counts )
44
-
45
-
46
- class TestBinGroupers :
47
- def setup_method (self , method ):
48
- self .obj = np .random .randn (10 , 1 )
49
- self .labels = np .array ([0 , 0 , 0 , 1 , 1 , 1 , 2 , 2 , 2 , 2 ], dtype = np .int64 )
50
- self .bins = np .array ([3 , 6 ], dtype = np .int64 )
51
-
52
- def test_generate_bins (self ):
53
- values = np .array ([1 , 2 , 3 , 4 , 5 , 6 ], dtype = np .int64 )
54
- binner = np .array ([0 , 3 , 6 , 9 ], dtype = np .int64 )
55
-
56
- for func in [lib .generate_bins_dt64 , generate_bins_generic ]:
57
- bins = func (values , binner , closed = "left" )
58
- assert (bins == np .array ([2 , 5 , 6 ])).all ()
59
-
60
- bins = func (values , binner , closed = "right" )
61
- assert (bins == np .array ([3 , 6 , 6 ])).all ()
62
-
63
- for func in [lib .generate_bins_dt64 , generate_bins_generic ]:
64
- values = np .array ([1 , 2 , 3 , 4 , 5 , 6 ], dtype = np .int64 )
65
- binner = np .array ([0 , 3 , 6 ], dtype = np .int64 )
66
-
67
- bins = func (values , binner , closed = "right" )
68
- assert (bins == np .array ([3 , 6 ])).all ()
69
-
70
- msg = "Invalid length for values or for binner"
71
- with pytest .raises (ValueError , match = msg ):
72
- generate_bins_generic (values , [], "right" )
73
- with pytest .raises (ValueError , match = msg ):
74
- generate_bins_generic (values [:0 ], binner , "right" )
75
-
76
- msg = "Values falls before first bin"
77
- with pytest .raises (ValueError , match = msg ):
78
- generate_bins_generic (values , [4 ], "right" )
79
- msg = "Values falls after last bin"
80
- with pytest .raises (ValueError , match = msg ):
81
- generate_bins_generic (values , [- 3 , - 1 ], "right" )
41
+ tm .assert_almost_equal (counts , exp_counts )
42
+
43
+
44
+ @pytest .mark .parametrize (
45
+ "binner,closed,expected" ,
46
+ [
47
+ (
48
+ np .array ([0 , 3 , 6 , 9 ], dtype = np .int64 ),
49
+ "left" ,
50
+ np .array ([2 , 5 , 6 ], dtype = np .int64 ),
51
+ ),
52
+ (
53
+ np .array ([0 , 3 , 6 , 9 ], dtype = np .int64 ),
54
+ "right" ,
55
+ np .array ([3 , 6 , 6 ], dtype = np .int64 ),
56
+ ),
57
+ (np .array ([0 , 3 , 6 ], dtype = np .int64 ), "left" , np .array ([2 , 5 ], dtype = np .int64 )),
58
+ (
59
+ np .array ([0 , 3 , 6 ], dtype = np .int64 ),
60
+ "right" ,
61
+ np .array ([3 , 6 ], dtype = np .int64 ),
62
+ ),
63
+ ],
64
+ )
65
+ def test_generate_bins (binner , closed , expected ):
66
+ values = np .array ([1 , 2 , 3 , 4 , 5 , 6 ], dtype = np .int64 )
67
+ result = lib .generate_bins_dt64 (values , binner , closed = closed )
68
+ tm .assert_numpy_array_equal (result , expected )
82
69
83
70
84
71
def test_group_ohlc ():
@@ -100,13 +87,13 @@ def _ohlc(group):
100
87
101
88
expected = np .array ([_ohlc (obj [:6 ]), _ohlc (obj [6 :12 ]), _ohlc (obj [12 :])])
102
89
103
- assert_almost_equal (out , expected )
90
+ tm . assert_almost_equal (out , expected )
104
91
tm .assert_numpy_array_equal (counts , np .array ([6 , 6 , 8 ], dtype = np .int64 ))
105
92
106
93
obj [:6 ] = np .nan
107
94
func (out , counts , obj [:, None ], labels )
108
95
expected [0 ] = np .nan
109
- assert_almost_equal (out , expected )
96
+ tm . assert_almost_equal (out , expected )
110
97
111
98
_check ("float32" )
112
99
_check ("float64" )
@@ -121,29 +108,29 @@ def test_int_index(self):
121
108
arr = np .random .randn (100 , 4 )
122
109
result = libreduction .compute_reduction (arr , np .sum , labels = Index (np .arange (4 )))
123
110
expected = arr .sum (0 )
124
- assert_almost_equal (result , expected )
111
+ tm . assert_almost_equal (result , expected )
125
112
126
113
result = libreduction .compute_reduction (
127
114
arr , np .sum , axis = 1 , labels = Index (np .arange (100 ))
128
115
)
129
116
expected = arr .sum (1 )
130
- assert_almost_equal (result , expected )
117
+ tm . assert_almost_equal (result , expected )
131
118
132
119
dummy = Series (0.0 , index = np .arange (100 ))
133
120
result = libreduction .compute_reduction (
134
121
arr , np .sum , dummy = dummy , labels = Index (np .arange (4 ))
135
122
)
136
123
expected = arr .sum (0 )
137
- assert_almost_equal (result , expected )
124
+ tm . assert_almost_equal (result , expected )
138
125
139
126
dummy = Series (0.0 , index = np .arange (4 ))
140
127
result = libreduction .compute_reduction (
141
128
arr , np .sum , axis = 1 , dummy = dummy , labels = Index (np .arange (100 ))
142
129
)
143
130
expected = arr .sum (1 )
144
- assert_almost_equal (result , expected )
131
+ tm . assert_almost_equal (result , expected )
145
132
146
133
result = libreduction .compute_reduction (
147
134
arr , np .sum , axis = 1 , dummy = dummy , labels = Index (np .arange (100 ))
148
135
)
149
- assert_almost_equal (result , expected )
136
+ tm . assert_almost_equal (result , expected )
0 commit comments