Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Mar 17, 2024
1 parent 8895fcd commit eb2ccb0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
14 changes: 6 additions & 8 deletions docs/basics/complete_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ call `.collect()` on the result if they want to materialise its values.
Unlike the `transform` method, `fit` cannot stay lazy, as we need to compute concrete values
for the means and standard deviations.

To be able to get `Series` out of our `DataFrame`, we'll need the `DataFrame` to be an
eager one, as Polars doesn't have a concept of lazy `Series`.
To do that, when we instantiate our `narwhals.DataFrame`, we pass `features=['eager']`,
which lets us access eager-only features.
To be able to get `Series` out of our `DataFrame`, we'll need to use `narwhals.DataFrame` (as opposed to
`narwhals.LazyFrame`), as Polars doesn't have a concept of lazy `Series`.

```python
import narwhals as nw

class StandardScalar:
def fit(self, df):
df = nw.DataFrame(df, features=['eager'])
df = nw.DataFrame(df)
self._means = {df[col].mean() for col in df.columns}
self._std_devs = {df[col].std() for col in df.columns}
```
Expand All @@ -67,12 +65,12 @@ import narwhals as nw

class StandardScaler:
def fit(self, df):
df = nw.DataFrame(df, features=["eager"])
df = nw.DataFrame(df)
self._means = {col: df[col].mean() for col in df.columns}
self._std_devs = {col: df[col].std() for col in df.columns}

def transform(self, df):
df = nw.DataFrame(df)
df = nw.LazyFrame(df)
df = df.with_columns(
(nw.col(col) - self._means[col]) / self._std_devs[col]
for col in df.columns
Expand All @@ -81,7 +79,7 @@ class StandardScaler:
```

Next, let's try running it. Notice how, as `transform` doesn't use
`features=['lazy']`, we can pass a `polars.LazyFrame` to it without issues!
any eager-only features, so we can make it completely lazy!

=== "pandas"
```python exec="true" source="material-block" result="python" session="tute-ex1"
Expand Down
4 changes: 2 additions & 2 deletions t.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import narwhals as nw

df_raw = pd.DataFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]})
df = nw.DataFrame(df_raw, features=["lazy"])
df = nw.LazyFrame(df_raw)
df_raw_2 = pd.DataFrame({"a": [1, 3], "c": [7, 9]})
df2 = nw.DataFrame(df_raw_2, features=["lazy"])
df2 = nw.LazyFrame(df_raw_2)

result = df.sort("a", "b")
print(nw.to_native(result))
Expand Down

0 comments on commit eb2ccb0

Please sign in to comment.