-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: split namespaces out from expr and series (#1782)
- Loading branch information
1 parent
0c98b60
commit 07402c6
Showing
11 changed files
with
5,054 additions
and
4,938 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from typing import Generic | ||
from typing import TypeVar | ||
|
||
if TYPE_CHECKING: | ||
from typing_extensions import Self | ||
|
||
from narwhals.expr import Expr | ||
|
||
ExprT = TypeVar("ExprT", bound="Expr") | ||
|
||
|
||
class ExprCatNamespace(Generic[ExprT]): | ||
def __init__(self: Self, expr: ExprT) -> None: | ||
self._expr = expr | ||
|
||
def get_categories(self: Self) -> ExprT: | ||
"""Get unique categories from column. | ||
Returns: | ||
A new expression. | ||
Examples: | ||
Let's create some dataframes: | ||
>>> import pandas as pd | ||
>>> import polars as pl | ||
>>> import pyarrow as pa | ||
>>> import narwhals as nw | ||
>>> from narwhals.typing import IntoFrameT | ||
>>> | ||
>>> data = {"fruits": ["apple", "mango", "mango"]} | ||
>>> df_pd = pd.DataFrame(data, dtype="category") | ||
>>> df_pl = pl.DataFrame(data, schema={"fruits": pl.Categorical}) | ||
We define a dataframe-agnostic function to get unique categories | ||
from column 'fruits': | ||
>>> def agnostic_cat_get_categories(df_native: IntoFrameT) -> IntoFrameT: | ||
... df = nw.from_native(df_native) | ||
... return df.select(nw.col("fruits").cat.get_categories()).to_native() | ||
We can then pass any supported library such as pandas or Polars to | ||
`agnostic_cat_get_categories`: | ||
>>> agnostic_cat_get_categories(df_pd) | ||
fruits | ||
0 apple | ||
1 mango | ||
>>> agnostic_cat_get_categories(df_pl) | ||
shape: (2, 1) | ||
┌────────┐ | ||
│ fruits │ | ||
│ --- │ | ||
│ str │ | ||
╞════════╡ | ||
│ apple │ | ||
│ mango │ | ||
└────────┘ | ||
""" | ||
return self._expr.__class__( | ||
lambda plx: self._expr._to_compliant_expr(plx).cat.get_categories() | ||
) |
Oops, something went wrong.