-
Notifications
You must be signed in to change notification settings - Fork 647
/
test_reduce.py
441 lines (373 loc) · 14.7 KB
/
test_reduce.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# Licensed to Modin Development Team under one or more contributor license agreements.
# See the NOTICE file distributed with this work for additional information regarding
# copyright ownership. The Modin Development Team licenses this file to you under the
# Apache License, Version 2.0 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.
import pytest
import numpy as np
import pandas
import matplotlib
import modin.pandas as pd
from modin.pandas.test.utils import (
df_equals,
arg_keys,
test_data,
test_data_values,
test_data_keys,
axis_keys,
axis_values,
bool_arg_keys,
bool_arg_values,
int_arg_keys,
int_arg_values,
eval_general,
create_test_dfs,
generate_multiindex,
test_data_diff_dtype,
df_equals_with_non_stable_indices,
test_data_large_categorical_dataframe,
default_to_pandas_ignore_string,
)
from modin.config import NPartitions
NPartitions.put(4)
# Force matplotlib to not use any Xwindows backend.
matplotlib.use("Agg")
# Our configuration in pytest.ini requires that we explicitly catch all
# instances of defaulting to pandas, but some test modules, like this one,
# have too many such instances.
pytestmark = pytest.mark.filterwarnings(default_to_pandas_ignore_string)
@pytest.mark.parametrize("method", ["all", "any"])
@pytest.mark.parametrize("is_transposed", [False, True])
@pytest.mark.parametrize(
"skipna", bool_arg_values, ids=arg_keys("skipna", bool_arg_keys)
)
@pytest.mark.parametrize("axis", axis_values, ids=axis_keys)
@pytest.mark.parametrize("data", [test_data["float_nan_data"]])
def test_all_any(data, axis, skipna, is_transposed, method):
eval_general(
*create_test_dfs(data),
lambda df: getattr((df.T if is_transposed else df), method)(
axis=axis, skipna=skipna, bool_only=None
),
)
@pytest.mark.parametrize("method", ["all", "any"])
@pytest.mark.parametrize(
"bool_only", bool_arg_values, ids=arg_keys("bool_only", bool_arg_keys)
)
def test_all_any_specific(bool_only, method):
eval_general(
*create_test_dfs(test_data_diff_dtype),
lambda df: getattr(df, method)(bool_only=bool_only),
)
@pytest.mark.parametrize("method", ["all", "any"])
@pytest.mark.parametrize("level", [-1, 0, 1])
@pytest.mark.parametrize("axis", [0, 1])
@pytest.mark.parametrize("data", [test_data["int_data"]])
def test_all_any_level(data, axis, level, method):
modin_df, pandas_df = pd.DataFrame(data), pandas.DataFrame(data)
if axis == 0:
new_idx = generate_multiindex(len(modin_df.index))
modin_df.index = new_idx
pandas_df.index = new_idx
else:
new_col = generate_multiindex(len(modin_df.columns))
modin_df.columns = new_col
pandas_df.columns = new_col
eval_general(
modin_df,
pandas_df,
lambda df: getattr(df, method)(axis=axis, level=level),
)
@pytest.mark.parametrize("axis", axis_values, ids=axis_keys)
@pytest.mark.parametrize(
"data", [test_data["float_nan_data"], test_data_large_categorical_dataframe]
)
def test_count(data, axis):
eval_general(
*create_test_dfs(data),
lambda df: df.count(axis=axis),
)
@pytest.mark.parametrize("numeric_only", [True, False, None])
def test_count_specific(numeric_only):
eval_general(
*create_test_dfs(test_data_diff_dtype),
lambda df: df.count(numeric_only=numeric_only),
)
@pytest.mark.parametrize("level", [-1, 0, 1])
@pytest.mark.parametrize("axis", [0, 1])
@pytest.mark.parametrize("data", [test_data["int_data"]])
def test_count_level(data, axis, level):
modin_df, pandas_df = pd.DataFrame(data), pandas.DataFrame(data)
if axis == 0:
new_idx = generate_multiindex(len(modin_df.index))
modin_df.index = new_idx
pandas_df.index = new_idx
else:
new_col = generate_multiindex(len(modin_df.columns))
modin_df.columns = new_col
pandas_df.columns = new_col
eval_general(
modin_df,
pandas_df,
lambda df: df.count(axis=axis, level=level),
)
@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
def test_count_dtypes(data):
modin_df, pandas_df = pd.DataFrame(data), pandas.DataFrame(data)
eval_general(
modin_df,
pandas_df,
lambda df: df.isna().count(axis=0),
)
@pytest.mark.parametrize("percentiles", [None, 0.10, 0.11, 0.44, 0.78, 0.99])
@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
def test_describe(data, percentiles):
eval_general(
*create_test_dfs(data),
lambda df: df.describe(percentiles=percentiles),
)
@pytest.mark.parametrize("has_numeric_column", [False, True])
@pytest.mark.parametrize("datetime_is_numeric", [True, False, None])
def test_2195(datetime_is_numeric, has_numeric_column):
data = {
"categorical": pd.Categorical(["d"] * 10**2),
"date": [np.datetime64("2000-01-01")] * 10**2,
}
if has_numeric_column:
data.update({"numeric": [5] * 10**2})
modin_df, pandas_df = pd.DataFrame(data), pandas.DataFrame(data)
eval_general(
modin_df,
pandas_df,
lambda df: df.describe(datetime_is_numeric=datetime_is_numeric),
)
# Issue: https://github.com/modin-project/modin/issues/4641
def test_describe_column_partition_has_different_index():
pandas_df = pandas.DataFrame(test_data["int_data"])
# We add a string column to test the case where partitions with mixed data
# types have different 'describe' rows, which causes an index mismatch.
pandas_df["string_column"] = "abc"
modin_df = pd.DataFrame(pandas_df)
eval_general(modin_df, pandas_df, lambda df: df.describe(include="all"))
@pytest.mark.parametrize(
"exclude,include",
[
([np.float64], None),
(np.float64, None),
(None, [np.timedelta64, np.datetime64, np.object, np.bool]),
(None, "all"),
(None, np.number),
],
)
def test_describe_specific(exclude, include):
eval_general(
*create_test_dfs(test_data_diff_dtype),
lambda df: df.drop("str_col", axis=1).describe(
exclude=exclude, include=include
),
)
@pytest.mark.parametrize("data", [test_data["int_data"]])
def test_describe_str(data):
modin_df = pd.DataFrame(data).applymap(str)
pandas_df = pandas.DataFrame(data).applymap(str)
try:
df_equals(modin_df.describe(), pandas_df.describe())
except AssertionError:
# We have to do this because we choose the highest count slightly differently
# than pandas. Because there is no true guarantee which one will be first,
# If they don't match, make sure that the `freq` is the same at least.
df_equals(
modin_df.describe().loc[["count", "unique", "freq"]],
pandas_df.describe().loc[["count", "unique", "freq"]],
)
def test_describe_dtypes():
data = {
"col1": list("abc"),
"col2": list("abc"),
"col3": list("abc"),
"col4": [1, 2, 3],
}
eval_general(*create_test_dfs(data), lambda df: df.describe())
@pytest.mark.parametrize("method", ["idxmin", "idxmax"])
@pytest.mark.parametrize("is_transposed", [False, True])
@pytest.mark.parametrize(
"skipna", bool_arg_values, ids=arg_keys("skipna", bool_arg_keys)
)
@pytest.mark.parametrize("axis", axis_values, ids=axis_keys)
@pytest.mark.parametrize("data", [test_data["float_nan_data"]])
def test_idxmin_idxmax(data, axis, skipna, is_transposed, method):
eval_general(
*create_test_dfs(data),
lambda df: getattr((df.T if is_transposed else df), method)(
axis=axis, skipna=skipna
),
)
@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
def test_last_valid_index(data):
modin_df, pandas_df = pd.DataFrame(data), pandas.DataFrame(data)
assert modin_df.last_valid_index() == pandas_df.last_valid_index()
@pytest.mark.parametrize("index", bool_arg_values, ids=arg_keys("index", bool_arg_keys))
@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
def test_memory_usage(data, index):
eval_general(*create_test_dfs(data), lambda df: df.memory_usage(index=index))
@pytest.mark.parametrize("method", ["min", "max", "mean"])
@pytest.mark.parametrize("is_transposed", [False, True])
@pytest.mark.parametrize(
"numeric_only", bool_arg_values, ids=arg_keys("numeric_only", bool_arg_keys)
)
@pytest.mark.parametrize(
"skipna", bool_arg_values, ids=arg_keys("skipna", bool_arg_keys)
)
@pytest.mark.parametrize("axis", axis_values, ids=axis_keys)
@pytest.mark.parametrize("data", [test_data["float_nan_data"]])
def test_min_max_mean(data, axis, skipna, numeric_only, is_transposed, method):
eval_general(
*create_test_dfs(data),
lambda df: getattr((df.T if is_transposed else df), method)(
axis=axis, skipna=skipna, numeric_only=numeric_only
),
)
@pytest.mark.parametrize("method", ["prod", "product"])
@pytest.mark.parametrize("is_transposed", [False, True])
@pytest.mark.parametrize(
"skipna", bool_arg_values, ids=arg_keys("skipna", bool_arg_keys)
)
@pytest.mark.parametrize("axis", axis_values, ids=axis_keys)
@pytest.mark.parametrize("data", [test_data["float_nan_data"]])
def test_prod(
data,
axis,
skipna,
is_transposed,
method,
):
eval_general(
*create_test_dfs(data),
lambda df, *args, **kwargs: getattr(df.T if is_transposed else df, method)(
axis=axis,
skipna=skipna,
),
)
# test for issue #1953
arrays = [["1", "1", "2", "2"], ["1", "2", "3", "4"]]
modin_df = pd.DataFrame(
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], index=arrays
)
pandas_df = pandas.DataFrame(
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], index=arrays
)
modin_result = modin_df.prod(level=0)
pandas_result = pandas_df.prod(level=0)
df_equals(modin_result, pandas_result)
@pytest.mark.parametrize("is_transposed", [False, True])
@pytest.mark.parametrize(
"skipna", bool_arg_values, ids=arg_keys("skipna", bool_arg_keys)
)
@pytest.mark.parametrize("axis", axis_values, ids=axis_keys)
@pytest.mark.parametrize("data", [test_data["float_nan_data"]])
def test_sum(data, axis, skipna, is_transposed):
eval_general(
*create_test_dfs(data),
lambda df: (df.T if is_transposed else df).sum(
axis=axis,
skipna=skipna,
),
)
# test for issue #1953
arrays = [["1", "1", "2", "2"], ["1", "2", "3", "4"]]
modin_df = pd.DataFrame(
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], index=arrays
)
pandas_df = pandas.DataFrame(
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], index=arrays
)
modin_result = modin_df.sum(level=0)
pandas_result = pandas_df.sum(level=0)
df_equals(modin_result, pandas_result)
@pytest.mark.parametrize("fn", ["prod, sum"])
@pytest.mark.parametrize(
"numeric_only", bool_arg_values, ids=arg_keys("numeric_only", bool_arg_keys)
)
@pytest.mark.parametrize(
"min_count", int_arg_values, ids=arg_keys("min_count", int_arg_keys)
)
def test_sum_prod_specific(fn, min_count, numeric_only):
eval_general(
*create_test_dfs(test_data_diff_dtype),
lambda df: getattr(df, fn)(min_count=min_count, numeric_only=numeric_only),
)
@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
def test_sum_single_column(data):
modin_df = pd.DataFrame(data).iloc[:, [0]]
pandas_df = pandas.DataFrame(data).iloc[:, [0]]
df_equals(modin_df.sum(), pandas_df.sum())
df_equals(modin_df.sum(axis=1), pandas_df.sum(axis=1))
@pytest.mark.parametrize(
"fn", ["max", "min", "median", "mean", "skew", "kurt", "sem", "std", "var"]
)
@pytest.mark.parametrize("axis", [0, 1])
@pytest.mark.parametrize(
"numeric_only", bool_arg_values, ids=arg_keys("numeric_only", bool_arg_keys)
)
def test_reduce_specific(fn, numeric_only, axis):
eval_general(
*create_test_dfs(test_data_diff_dtype),
lambda df: getattr(df, fn)(numeric_only=numeric_only, axis=axis),
)
@pytest.mark.parametrize("subset_len", [1, 2])
@pytest.mark.parametrize("sort", bool_arg_values, ids=bool_arg_keys)
@pytest.mark.parametrize("normalize", bool_arg_values, ids=bool_arg_keys)
@pytest.mark.parametrize("dropna", bool_arg_values, ids=bool_arg_keys)
@pytest.mark.parametrize("ascending", bool_arg_values, ids=bool_arg_keys)
def test_value_counts(subset_len, sort, normalize, dropna, ascending):
def comparator(md_res, pd_res):
if subset_len == 1:
# 'pandas.DataFrame.value_counts' always returns frames with MultiIndex,
# even when 'subset_len == 1' it returns MultiIndex with 'nlevels == 1'.
# This behavior is expensive to mimic, so Modin 'value_counts' returns frame
# with non-multi index in that case. That's why we flatten indices here.
assert md_res.index.nlevels == pd_res.index.nlevels == 1
for df in [md_res, pd_res]:
df.index = df.index.get_level_values(0)
if sort:
# We sort indices for the result because of:
# https://github.com/modin-project/modin/issues/1650
df_equals_with_non_stable_indices(md_res, pd_res)
else:
df_equals(md_res.sort_index(), pd_res.sort_index())
data = test_data_values[0]
md_df, pd_df = create_test_dfs(data)
# We're picking columns with different index signs to involve columns from different partitions
subset = [pd_df.columns[-i if i % 2 else i] for i in range(subset_len)]
eval_general(
md_df,
pd_df,
lambda df: df.value_counts(
subset=subset,
sort=sort,
normalize=normalize,
dropna=dropna,
ascending=ascending,
),
comparator=comparator,
# Modin's `sort_values` does not validate `ascending` type and so
# does not raise an exception when it isn't a bool, when pandas do so,
# visit modin-issue#3388 for more info.
check_exception_type=None if sort and ascending is None else True,
)
def test_value_counts_categorical():
# from issue #3571
data = np.array(["a"] * 50000 + ["b"] * 10000 + ["c"] * 1000)
random_state = np.random.RandomState(seed=42)
random_state.shuffle(data)
eval_general(
*create_test_dfs({"col1": data, "col2": data}, dtype="category"),
lambda df: df.value_counts(),
)