|
25 | 25 | import pandas.util._test_decorators as td
|
26 | 26 |
|
27 | 27 |
|
28 |
| - def _check_stat_op(self, name, alternative, main_frame, float_frame, |
29 |
| - float_string_frame, has_skipna=True, |
30 |
| - has_numeric_only=False, check_dtype=True, |
31 |
| - check_dates=False, check_less_precise=False, |
32 |
| - skipna_alternative=None): |
33 |
| - |
34 |
| - f = getattr(main_frame, name) |
35 |
| - |
36 |
| - if check_dates: |
37 |
| - df = DataFrame({'b': date_range('1/1/2001', periods=2)}) |
38 |
| - _f = getattr(df, name) |
39 |
| - result = _f() |
40 |
| - assert isinstance(result, Series) |
41 |
| - |
42 |
| - df['a'] = lrange(len(df)) |
43 |
| - result = getattr(df, name)() |
44 |
| - assert isinstance(result, Series) |
45 |
| - assert len(result) |
46 |
| - |
47 |
| - if has_skipna: |
48 |
| - def wrapper(x): |
49 |
| - return alternative(x.values) |
50 |
| - |
51 |
| - skipna_wrapper = tm._make_skipna_wrapper(alternative, |
52 |
| - skipna_alternative) |
53 |
| - result0 = f(axis=0, skipna=False) |
54 |
| - result1 = f(axis=1, skipna=False) |
55 |
| - tm.assert_series_equal(result0, main_frame.apply(wrapper), |
56 |
| - check_dtype=check_dtype, |
57 |
| - check_less_precise=check_less_precise) |
58 |
| - # HACK: win32 |
59 |
| - tm.assert_series_equal(result1, main_frame.apply(wrapper, axis=1), |
60 |
| - check_dtype=False, |
61 |
| - check_less_precise=check_less_precise) |
62 |
| - else: |
63 |
| - skipna_wrapper = alternative |
| 28 | +def _check_stat_op(self, name, alternative, main_frame, float_frame, |
| 29 | + float_string_frame, has_skipna=True, |
| 30 | + has_numeric_only=False, check_dtype=True, |
| 31 | + check_dates=False, check_less_precise=False, |
| 32 | + skipna_alternative=None): |
| 33 | + |
| 34 | + f = getattr(main_frame, name) |
| 35 | + |
| 36 | + if check_dates: |
| 37 | + df = DataFrame({'b': date_range('1/1/2001', periods=2)}) |
| 38 | + _f = getattr(df, name) |
| 39 | + result = _f() |
| 40 | + assert isinstance(result, Series) |
| 41 | + |
| 42 | + df['a'] = lrange(len(df)) |
| 43 | + result = getattr(df, name)() |
| 44 | + assert isinstance(result, Series) |
| 45 | + assert len(result) |
| 46 | + |
| 47 | + if has_skipna: |
| 48 | + def wrapper(x): |
| 49 | + return alternative(x.values) |
64 | 50 |
|
65 |
| - result0 = f(axis=0) |
66 |
| - result1 = f(axis=1) |
67 |
| - tm.assert_series_equal(result0, main_frame.apply(skipna_wrapper), |
| 51 | + skipna_wrapper = tm._make_skipna_wrapper(alternative, |
| 52 | + skipna_alternative) |
| 53 | + result0 = f(axis=0, skipna=False) |
| 54 | + result1 = f(axis=1, skipna=False) |
| 55 | + tm.assert_series_equal(result0, main_frame.apply(wrapper), |
68 | 56 | check_dtype=check_dtype,
|
69 | 57 | check_less_precise=check_less_precise)
|
| 58 | + # HACK: win32 |
| 59 | + tm.assert_series_equal(result1, main_frame.apply(wrapper, axis=1), |
| 60 | + check_dtype=False, |
| 61 | + check_less_precise=check_less_precise) |
| 62 | + else: |
| 63 | + skipna_wrapper = alternative |
| 64 | + |
| 65 | + result0 = f(axis=0) |
| 66 | + result1 = f(axis=1) |
| 67 | + tm.assert_series_equal(result0, main_frame.apply(skipna_wrapper), |
| 68 | + check_dtype=check_dtype, |
| 69 | + check_less_precise=check_less_precise) |
| 70 | + if name in ['sum', 'prod']: |
| 71 | + expected = main_frame.apply(skipna_wrapper, axis=1) |
| 72 | + tm.assert_series_equal(result1, expected, check_dtype=False, |
| 73 | + check_less_precise=check_less_precise) |
| 74 | + |
| 75 | + # check dtypes |
| 76 | + if check_dtype: |
| 77 | + lcd_dtype = main_frame.values.dtype |
| 78 | + assert lcd_dtype == result0.dtype |
| 79 | + assert lcd_dtype == result1.dtype |
| 80 | + |
| 81 | + # bad axis |
| 82 | + tm.assert_raises_regex(ValueError, 'No axis named 2', f, axis=2) |
| 83 | + # make sure works on mixed-type frame |
| 84 | + getattr(float_string_frame, name)(axis=0) |
| 85 | + getattr(float_string_frame, name)(axis=1) |
| 86 | + |
| 87 | + if has_numeric_only: |
| 88 | + getattr(float_string_frame, name)(axis=0, numeric_only=True) |
| 89 | + getattr(float_string_frame, name)(axis=1, numeric_only=True) |
| 90 | + getattr(float_frame, name)(axis=0, numeric_only=False) |
| 91 | + getattr(float_frame, name)(axis=1, numeric_only=False) |
| 92 | + |
| 93 | + # all NA case |
| 94 | + if has_skipna: |
| 95 | + all_na = float_frame * np.NaN |
| 96 | + r0 = getattr(all_na, name)(axis=0) |
| 97 | + r1 = getattr(all_na, name)(axis=1) |
70 | 98 | if name in ['sum', 'prod']:
|
71 |
| - expected = main_frame.apply(skipna_wrapper, axis=1) |
72 |
| - tm.assert_series_equal(result1, expected, check_dtype=False, |
73 |
| - check_less_precise=check_less_precise) |
74 |
| - |
75 |
| - # check dtypes |
76 |
| - if check_dtype: |
77 |
| - lcd_dtype = main_frame.values.dtype |
78 |
| - assert lcd_dtype == result0.dtype |
79 |
| - assert lcd_dtype == result1.dtype |
80 |
| - |
81 |
| - # bad axis |
82 |
| - tm.assert_raises_regex(ValueError, 'No axis named 2', f, axis=2) |
83 |
| - # make sure works on mixed-type frame |
84 |
| - getattr(float_string_frame, name)(axis=0) |
85 |
| - getattr(float_string_frame, name)(axis=1) |
86 |
| - |
87 |
| - if has_numeric_only: |
88 |
| - getattr(float_string_frame, name)(axis=0, numeric_only=True) |
89 |
| - getattr(float_string_frame, name)(axis=1, numeric_only=True) |
90 |
| - getattr(float_frame, name)(axis=0, numeric_only=False) |
91 |
| - getattr(float_frame, name)(axis=1, numeric_only=False) |
92 |
| - |
93 |
| - # all NA case |
94 |
| - if has_skipna: |
95 |
| - all_na = float_frame * np.NaN |
96 |
| - r0 = getattr(all_na, name)(axis=0) |
97 |
| - r1 = getattr(all_na, name)(axis=1) |
98 |
| - if name in ['sum', 'prod']: |
99 |
| - unit = int(name == 'prod') |
100 |
| - expected = pd.Series(unit, index=r0.index, dtype=r0.dtype) |
101 |
| - tm.assert_series_equal(r0, expected) |
102 |
| - expected = pd.Series(unit, index=r1.index, dtype=r1.dtype) |
103 |
| - tm.assert_series_equal(r1, expected) |
104 |
| - |
105 |
| - |
106 |
| - def _check_bool_op(self, name, alternative, frame, float_string_frame, |
107 |
| - has_skipna=True, has_bool_only=False): |
108 |
| - |
109 |
| - f = getattr(frame, name) |
110 |
| - |
111 |
| - if has_skipna: |
112 |
| - def skipna_wrapper(x): |
113 |
| - nona = x.dropna().values |
114 |
| - return alternative(nona) |
115 |
| - |
116 |
| - def wrapper(x): |
117 |
| - return alternative(x.values) |
118 |
| - |
119 |
| - result0 = f(axis=0, skipna=False) |
120 |
| - result1 = f(axis=1, skipna=False) |
121 |
| - tm.assert_series_equal(result0, frame.apply(wrapper)) |
122 |
| - tm.assert_series_equal(result1, frame.apply(wrapper, axis=1), |
123 |
| - check_dtype=False) # HACK: win32 |
| 99 | + unit = int(name == 'prod') |
| 100 | + expected = pd.Series(unit, index=r0.index, dtype=r0.dtype) |
| 101 | + tm.assert_series_equal(r0, expected) |
| 102 | + expected = pd.Series(unit, index=r1.index, dtype=r1.dtype) |
| 103 | + tm.assert_series_equal(r1, expected) |
| 104 | + |
| 105 | + |
| 106 | +def _check_bool_op(self, name, alternative, frame, float_string_frame, |
| 107 | + has_skipna=True, has_bool_only=False): |
| 108 | + |
| 109 | + f = getattr(frame, name) |
| 110 | + |
| 111 | + if has_skipna: |
| 112 | + def skipna_wrapper(x): |
| 113 | + nona = x.dropna().values |
| 114 | + return alternative(nona) |
| 115 | + |
| 116 | + def wrapper(x): |
| 117 | + return alternative(x.values) |
| 118 | + |
| 119 | + result0 = f(axis=0, skipna=False) |
| 120 | + result1 = f(axis=1, skipna=False) |
| 121 | + tm.assert_series_equal(result0, frame.apply(wrapper)) |
| 122 | + tm.assert_series_equal(result1, frame.apply(wrapper, axis=1), |
| 123 | + check_dtype=False) # HACK: win32 |
| 124 | + else: |
| 125 | + skipna_wrapper = alternative |
| 126 | + wrapper = alternative |
| 127 | + |
| 128 | + result0 = f(axis=0) |
| 129 | + result1 = f(axis=1) |
| 130 | + tm.assert_series_equal(result0, frame.apply(skipna_wrapper)) |
| 131 | + tm.assert_series_equal(result1, frame.apply(skipna_wrapper, axis=1), |
| 132 | + check_dtype=False) |
| 133 | + |
| 134 | + # bad axis |
| 135 | + pytest.raises(ValueError, f, axis=2) |
| 136 | + |
| 137 | + # make sure works on mixed-type frame |
| 138 | + mixed = float_string_frame |
| 139 | + mixed['_bool_'] = np.random.randn(len(mixed)) > 0 |
| 140 | + getattr(mixed, name)(axis=0) |
| 141 | + getattr(mixed, name)(axis=1) |
| 142 | + |
| 143 | + class NonzeroFail(object): |
| 144 | + |
| 145 | + def __nonzero__(self): |
| 146 | + raise ValueError |
| 147 | + |
| 148 | + mixed['_nonzero_fail_'] = NonzeroFail() |
| 149 | + |
| 150 | + if has_bool_only: |
| 151 | + getattr(mixed, name)(axis=0, bool_only=True) |
| 152 | + getattr(mixed, name)(axis=1, bool_only=True) |
| 153 | + getattr(frame, name)(axis=0, bool_only=False) |
| 154 | + getattr(frame, name)(axis=1, bool_only=False) |
| 155 | + |
| 156 | + # all NA case |
| 157 | + if has_skipna: |
| 158 | + all_na = frame * np.NaN |
| 159 | + r0 = getattr(all_na, name)(axis=0) |
| 160 | + r1 = getattr(all_na, name)(axis=1) |
| 161 | + if name == 'any': |
| 162 | + assert not r0.any() |
| 163 | + assert not r1.any() |
124 | 164 | else:
|
125 |
| - skipna_wrapper = alternative |
126 |
| - wrapper = alternative |
127 |
| - |
128 |
| - result0 = f(axis=0) |
129 |
| - result1 = f(axis=1) |
130 |
| - tm.assert_series_equal(result0, frame.apply(skipna_wrapper)) |
131 |
| - tm.assert_series_equal(result1, frame.apply(skipna_wrapper, axis=1), |
132 |
| - check_dtype=False) |
133 |
| - |
134 |
| - # bad axis |
135 |
| - pytest.raises(ValueError, f, axis=2) |
136 |
| - |
137 |
| - # make sure works on mixed-type frame |
138 |
| - mixed = float_string_frame |
139 |
| - mixed['_bool_'] = np.random.randn(len(mixed)) > 0 |
140 |
| - getattr(mixed, name)(axis=0) |
141 |
| - getattr(mixed, name)(axis=1) |
142 |
| - |
143 |
| - class NonzeroFail(object): |
144 |
| - |
145 |
| - def __nonzero__(self): |
146 |
| - raise ValueError |
147 |
| - |
148 |
| - mixed['_nonzero_fail_'] = NonzeroFail() |
149 |
| - |
150 |
| - if has_bool_only: |
151 |
| - getattr(mixed, name)(axis=0, bool_only=True) |
152 |
| - getattr(mixed, name)(axis=1, bool_only=True) |
153 |
| - getattr(frame, name)(axis=0, bool_only=False) |
154 |
| - getattr(frame, name)(axis=1, bool_only=False) |
155 |
| - |
156 |
| - # all NA case |
157 |
| - if has_skipna: |
158 |
| - all_na = frame * np.NaN |
159 |
| - r0 = getattr(all_na, name)(axis=0) |
160 |
| - r1 = getattr(all_na, name)(axis=1) |
161 |
| - if name == 'any': |
162 |
| - assert not r0.any() |
163 |
| - assert not r1.any() |
164 |
| - else: |
165 |
| - assert r0.all() |
166 |
| - assert r1.all() |
| 165 | + assert r0.all() |
| 166 | + assert r1.all() |
167 | 167 |
|
168 | 168 |
|
169 | 169 | class TestDataFrameAnalytics():
|
|
0 commit comments