-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from MarcoGorelli/redesign
redesign
- Loading branch information
Showing
27 changed files
with
1,191 additions
and
1,338 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,29 @@ | ||
# ruff: noqa | ||
from typing import Any | ||
import polars as pl | ||
|
||
import narwhals as nw | ||
|
||
|
||
def func(df_raw: Any) -> Any: | ||
df = nw.DataFrame(df_raw) | ||
res = df.with_columns( | ||
d=nw.col("a") + 1, | ||
e=nw.col("a") + nw.col("b"), | ||
) | ||
res = res.group_by(["a"]).agg( | ||
nw.col("b").sum(), | ||
d=nw.col("c").sum(), | ||
# e=nw.len(), | ||
) | ||
return nw.to_native(res) | ||
|
||
|
||
import pandas as pd | ||
|
||
df = pd.DataFrame({"a": [1, 1, 3], "b": [4, 5, 6], "c": [7, 8, 9]}) | ||
print(func(df)) | ||
df = pl.DataFrame({"a": [1, 1, 3], "b": [4, 5, 6], "c": [7, 8, 9]}) | ||
print(func(df)) | ||
df = pl.LazyFrame({"a": [1, 1, 3], "b": [4, 5, 6], "c": [7, 8, 9]}) | ||
print(func(df).collect()) |
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,31 @@ | ||
# Design | ||
|
||
Let's do this differently. | ||
|
||
Here's what I'd like to get to: | ||
|
||
import narwhals as nw | ||
from narwhals.translate import ( | ||
translate_frame, | ||
translate_series, | ||
to_native, | ||
) | ||
|
||
dfpd = ... | ||
df = nw.DataFrame(df_any) | ||
|
||
df = df.with_columns(c = nw.col('a') + nw.col('b')) | ||
|
||
result = to_native(df) | ||
|
||
--- | ||
|
||
we need to just have a single class. can't have all this nonsense... | ||
|
||
then, we don't even need a spec... | ||
|
||
we can still define entrypoints though? | ||
|
||
--- | ||
|
||
where should extract native happen? |
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,66 @@ | ||
# ruff: noqa | ||
# type: ignore | ||
from typing import Any | ||
import pandas as pd | ||
import polars as pl | ||
|
||
import narwhals as nw | ||
|
||
|
||
def my_agnostic_function( | ||
suppliers_native, | ||
parts_native, | ||
): | ||
suppliers = nw.DataFrame(suppliers_native) | ||
parts = nw.DataFrame(parts_native) | ||
|
||
result = ( | ||
suppliers.join(parts, left_on="city", right_on="city") | ||
.filter( | ||
nw.col("color").is_in(["Red", "Green"]), | ||
nw.col("weight") > 14, | ||
) | ||
.group_by("s", "p") | ||
.agg( | ||
weight_mean=nw.col("weight").mean(), | ||
weight_max=nw.col("weight").max(), | ||
) | ||
).with_columns(nw.col("weight_max").cast(nw.Int64)) | ||
return nw.to_native(result) | ||
|
||
|
||
suppliers = { | ||
"s": ["S1", "S2", "S3", "S4", "S5"], | ||
"sname": ["Smith", "Jones", "Blake", "Clark", "Adams"], | ||
"status": [20, 10, 30, 20, 30], | ||
"city": ["London", "Paris", "Paris", "London", "Athens"], | ||
} | ||
parts = { | ||
"p": ["P1", "P2", "P3", "P4", "P5", "P6"], | ||
"pname": ["Nut", "Bolt", "Screw", "Screw", "Cam", "Cog"], | ||
"color": ["Red", "Green", "Blue", "Red", "Blue", "Red"], | ||
"weight": [12.0, 17.0, 17.0, 14.0, 12.0, 19.0], | ||
"city": ["London", "Paris", "Oslo", "London", "Paris", "London"], | ||
} | ||
|
||
print("pandas output:") | ||
print( | ||
my_agnostic_function( | ||
pd.DataFrame(suppliers), | ||
pd.DataFrame(parts), | ||
) | ||
) | ||
print("\nPolars output:") | ||
print( | ||
my_agnostic_function( | ||
pl.DataFrame(suppliers), | ||
pl.DataFrame(parts), | ||
) | ||
) | ||
print("\nPolars lazy output:") | ||
print( | ||
my_agnostic_function( | ||
pl.LazyFrame(suppliers), | ||
pl.LazyFrame(parts), | ||
).collect() | ||
) |
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
Oops, something went wrong.