Skip to content

Commit

Permalink
chore: split namespaces out from expr and series (#1782)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli authored Jan 10, 2025
1 parent 0c98b60 commit 07402c6
Show file tree
Hide file tree
Showing 11 changed files with 5,054 additions and 4,938 deletions.
2,695 changes: 5 additions & 2,690 deletions narwhals/expr.py

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions narwhals/expr_cat.py
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()
)
Loading

0 comments on commit 07402c6

Please sign in to comment.