Skip to content

Commit

Permalink
refactor[python]: Dispatch Series namespace methods to Expr using a d…
Browse files Browse the repository at this point in the history
…ecorator (#4423)
  • Loading branch information
stinodego committed Aug 26, 2022
1 parent 4d7640d commit a77665a
Show file tree
Hide file tree
Showing 16 changed files with 412 additions and 591 deletions.
2 changes: 2 additions & 0 deletions py-polars/polars/internals/expr/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
class ExprCatNameSpace:
"""Namespace for categorical related expressions."""

_accessor = "cat"

def __init__(self, expr: pli.Expr):
self._pyexpr = expr._pyexpr

Expand Down
2 changes: 2 additions & 0 deletions py-polars/polars/internals/expr/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
class ExprDateTimeNameSpace:
"""Namespace for datetime related expressions."""

_accessor = "dt"

def __init__(self, expr: pli.Expr):
self._pyexpr = expr._pyexpr

Expand Down
2 changes: 2 additions & 0 deletions py-polars/polars/internals/expr/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
class ExprListNameSpace:
"""Namespace for list related expressions."""

_accessor = "arr"

def __init__(self, expr: pli.Expr):
self._pyexpr = expr._pyexpr

Expand Down
2 changes: 2 additions & 0 deletions py-polars/polars/internals/expr/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
class ExprMetaNameSpace:
"""Namespace for expressions on a meta level."""

_accessor = "meta"

def __init__(self, expr: pli.Expr):
self._pyexpr = expr._pyexpr

Expand Down
2 changes: 2 additions & 0 deletions py-polars/polars/internals/expr/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
class ExprStringNameSpace:
"""Namespace for string related expressions."""

_accessor = "str"

def __init__(self, expr: pli.Expr):
self._pyexpr = expr._pyexpr

Expand Down
2 changes: 2 additions & 0 deletions py-polars/polars/internals/expr/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
class ExprStructNameSpace:
"""Namespace for struct related expressions."""

_accessor = "struct"

def __init__(self, expr: pli.Expr):
self._pyexpr = expr._pyexpr

Expand Down
10 changes: 7 additions & 3 deletions py-polars/polars/internals/series/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
from typing import TYPE_CHECKING

import polars.internals as pli
from polars.internals.series.utils import expr_dispatch

if TYPE_CHECKING:
from polars.internals.type_aliases import CategoricalOrdering
from polars.polars import PySeries


@expr_dispatch
class CatNameSpace:
"""Namespace for categorical related series."""

def __init__(self, s: pli.Series):
self._s = s
_accessor = "cat"

def __init__(self, series: pli.Series):
self._s: PySeries = series._s

def set_ordering(self, ordering: CategoricalOrdering) -> pli.Series:
"""
Expand Down Expand Up @@ -55,4 +60,3 @@ def set_ordering(self, ordering: CategoricalOrdering) -> pli.Series:
└──────┴──────┘
"""
return pli.select(pli.lit(self._s).cat.set_ordering(ordering)).to_series()

0 comments on commit a77665a

Please sign in to comment.