-
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
5 changed files
with
114 additions
and
25 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
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,30 @@ | ||
from typing import List | ||
|
||
from eds_scikit.utils.typing import DataFrame | ||
|
||
|
||
def sort_values_first( | ||
df: DataFrame, by_cols: List[str], cols: List[str], ascending: bool = False | ||
): | ||
""" | ||
Replace dataframe.sort_value(cols).groupby(by_cols).first() | ||
Parameters | ||
---------- | ||
df : DataFrame | ||
by_cols : List[str] | ||
columns to groupby | ||
cols : List[str] | ||
columns to sort | ||
ascending : bool | ||
""" | ||
|
||
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,52 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
from databricks import koalas as ks | ||
|
||
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 | ||
|
||
# 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) |
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