Skip to content

Commit 4172282

Browse files
committed
update raedme
1 parent b699f7d commit 4172282

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

README.md

+15-16
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,40 @@ There are three steps to writing dataframe-agnostic code using Narwhals:
4444
Here's an example of a dataframe agnostic function:
4545

4646
```python
47-
from typing import TypeVar
47+
from typing import Any
4848
import pandas as pd
4949
import polars as pl
5050

51-
from narwhals import translate_frame, get_namespace, to_native
52-
53-
AnyDataFrame = TypeVar("AnyDataFrame")
51+
import narwhals as nw
5452

5553

5654
def my_agnostic_function(
57-
suppliers_native: AnyDataFrame,
58-
parts_native: AnyDataFrame,
59-
) -> AnyDataFrame:
60-
suppliers = translate_frame(suppliers_native)
61-
parts = translate_frame(parts_native)
62-
pl = get_namespace(suppliers)
55+
suppliers_native,
56+
parts_native,
57+
):
58+
suppliers = nw.DataFrame(suppliers_native)
59+
parts = nw.DataFrame(parts_native)
6360

6461
result = (
6562
suppliers.join(parts, left_on="city", right_on="city")
6663
.filter(
67-
pl.col("color").is_in(["Red", "Green"]),
68-
pl.col("weight") > 14,
64+
nw.col("color").is_in(["Red", "Green"]),
65+
nw.col("weight") > 14,
6966
)
7067
.group_by("s", "p")
7168
.agg(
72-
weight_mean=pl.col("weight").mean(),
73-
weight_max=pl.col("weight").max(),
69+
weight_mean=nw.col("weight").mean(),
70+
weight_max=nw.col("weight").max(),
7471
)
75-
)
76-
return to_native(result)
72+
).with_columns(nw.col("weight_max").cast(nw.Int64))
73+
return nw.to_native(result)
74+
7775
```
7876
You can pass in a pandas or Polars dataframe, the output will be the same!
7977
Let's try it out:
8078

8179
```python
80+
8281
suppliers = {
8382
"s": ["S1", "S2", "S3", "S4", "S5"],
8483
"sname": ["Smith", "Jones", "Blake", "Clark", "Adams"],

0 commit comments

Comments
 (0)