-
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
53 additions
and
37 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from eds_scikit.utils.typing import DataFrame | ||
from typing import List | ||
|
||
def sort_values_first(df : DataFrame, | ||
by_cols : List[str], | ||
cols : List[str], | ||
ascending : bool = False): | ||
return df.groupby(by_cols).apply(lambda group: group.sort_values(by=cols, ascending=[ascending for i in cols]).head(1)).reset_index(drop=True) |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import pandas as pd | ||
import pytest | ||
from eds_scikit.utils import framework | ||
from eds_scikit.utils.sort_values_first import sort_values_first | ||
from eds_scikit.utils.test_utils import assert_equal_no_order | ||
|
||
from databricks import koalas as ks | ||
import numpy as np | ||
|
||
# Create a DataFrame | ||
np.random.seed(0) | ||
size=10000 | ||
data = { | ||
'A': np.random.choice(['X', 'Y', 'Z'], size), | ||
'B': np.random.randint(1, 5, size), | ||
'C': np.random.randint(1, 5, size), | ||
'D': np.random.randint(1, 5, size), | ||
'E': np.random.randint(1, 5, size) | ||
} | ||
|
||
inputs = pd.DataFrame(data) | ||
inputs.loc[0, 'B'] = 0 | ||
inputs.loc[0, 'C'] = 4 | ||
|
||
@pytest.mark.parametrize( | ||
"module", | ||
["pandas", "koalas"], | ||
) | ||
def test_sort_values_first(module): | ||
|
||
inputs_fr = framework.to(module, inputs) | ||
|
||
computed = framework.pandas(sort_values_first(inputs_fr, ["A"], ["B", "C"], ascending=True)) | ||
expected = inputs.sort_values(["B", "C"], ascending=True).groupby("A", as_index=False).first() | ||
assert_equal_no_order(computed, expected) | ||
|
||
computed = framework.pandas(sort_values_first(inputs_fr, ["A"], ["B", "C"], ascending=False)) | ||
expected = inputs.sort_values(["B", "C"], ascending=False).groupby("A", as_index=False).first() | ||
assert_equal_no_order(computed, expected) | ||
|