-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
65 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
def sort_values_first_koalas(dataframe, by_cols, cols, ascending=True): | ||
for col in cols: | ||
dataframe_min_max = dataframe.groupby(by_cols, as_index=False)[col] | ||
dataframe_min_max = dataframe_min_max.min() if ascending else dataframe_min_max.max() | ||
dataframe_min_max = ( | ||
dataframe_min_max.min() if ascending else dataframe_min_max.max() | ||
) | ||
dataframe = dataframe.merge(dataframe_min_max, on=[*by_cols, col], how="right") | ||
return dataframe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,68 @@ | ||
import pandas as pd | ||
from eds_scikit.utils.sort_first_koalas import sort_values_first_koalas | ||
from numpy import array | ||
import pytest | ||
from numpy import array | ||
|
||
from eds_scikit.utils import framework | ||
from eds_scikit.utils.sort_first_koalas import sort_values_first_koalas | ||
from eds_scikit.utils.test_utils import assert_equal_no_order | ||
|
||
data = { | ||
'A': array(['X', 'Y', 'X', 'Y', 'Y', 'Z', 'X', 'Z', 'X', 'X', 'X', 'Z', 'Y', 'Z', 'Z', 'X', 'Y', 'Y', 'Y', 'Y'], dtype='<U1'), | ||
'B': array([9, 5, 4, 1, 4, 6, 1, 3, 4, 9, 2, 4, 4, 4, 8, 1, 2, 1, 5, 8]), | ||
'C': array([4, 3, 8, 3, 1, 1, 5, 6, 6, 7, 9, 5, 2, 5, 9, 2, 2, 8, 4, 7]), | ||
'D': array([8, 3, 1, 4, 6, 5, 5, 7, 5, 5, 4, 5, 5, 9, 5, 4, 8, 6, 6, 1]), | ||
'E': array([2, 6, 4, 1, 6, 1, 2, 3, 5, 3, 1, 4, 3, 1, 8, 6, 1, 3, 8, 3]) | ||
"A": array( | ||
[ | ||
"X", | ||
"Y", | ||
"X", | ||
"Y", | ||
"Y", | ||
"Z", | ||
"X", | ||
"Z", | ||
"X", | ||
"X", | ||
"X", | ||
"Z", | ||
"Y", | ||
"Z", | ||
"Z", | ||
"X", | ||
"Y", | ||
"Y", | ||
"Y", | ||
"Y", | ||
], | ||
dtype="<U1", | ||
), | ||
"B": array([9, 5, 4, 1, 4, 6, 1, 3, 4, 9, 2, 4, 4, 4, 8, 1, 2, 1, 5, 8]), | ||
"C": array([4, 3, 8, 3, 1, 1, 5, 6, 6, 7, 9, 5, 2, 5, 9, 2, 2, 8, 4, 7]), | ||
"D": array([8, 3, 1, 4, 6, 5, 5, 7, 5, 5, 4, 5, 5, 9, 5, 4, 8, 6, 6, 1]), | ||
"E": array([2, 6, 4, 1, 6, 1, 2, 3, 5, 3, 1, 4, 3, 1, 8, 6, 1, 3, 8, 3]), | ||
} | ||
|
||
all_inputs = [ | ||
pd.DataFrame(data) | ||
] | ||
all_inputs = [pd.DataFrame(data)] | ||
|
||
all_params = dict(ascending=[True, False], cols=["A", ["A", "B"]], by_cols=[["B", "C", "D"], ["C", "D"]]) | ||
all_params = dict( | ||
ascending=[True, False], | ||
cols=["A", ["A", "B"]], | ||
by_cols=[["B", "C", "D"], ["C", "D"]], | ||
) | ||
all_params = pd.DataFrame(all_params).to_dict("records") | ||
|
||
|
||
@pytest.mark.parametrize("module", ["pandas", "koalas"]) | ||
@pytest.mark.parametrize( | ||
"params, inputs", | ||
[(params, inputs) for params, inputs in zip(all_params, all_inputs)], | ||
) | ||
def test_visit_merging(module, params, inputs): | ||
print(params) | ||
expected_results = inputs.sort_values(params["cols"]).groupby(params["by_cols"]).first().reset_index() | ||
expected_results = ( | ||
inputs.sort_values(params["cols"]) | ||
.groupby(params["by_cols"]) | ||
.first() | ||
.reset_index() | ||
) | ||
inputs = framework.to(module, inputs) | ||
results = sort_values_first_koalas(inputs, **params) | ||
results = framework.pandas(results) | ||
|
||
assert_equal_no_order( | ||
results, expected_results | ||
) | ||
assert_equal_no_order(results, expected_results) |