Skip to content

Commit

Permalink
use anonymousexprerror
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jan 17, 2025
1 parent a2dbf98 commit fd3ee52
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 81 deletions.
55 changes: 14 additions & 41 deletions narwhals/_duckdb/expr_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import TYPE_CHECKING
from typing import Callable
from narwhals.exceptions import AnonymousExprError

if TYPE_CHECKING:
from typing_extensions import Self
Expand All @@ -15,15 +16,9 @@ def __init__(self: Self, expr: DuckDBExpr) -> None:

def keep(self: Self) -> DuckDBExpr:
root_names = self._compliant_expr._root_names

if root_names is None:
msg = (
"Anonymous expressions are not supported in `.name.keep`.\n"
"Instead of `nw.all()`, try using a named expression, such as "
"`nw.col('a', 'b')`\n"
)
raise ValueError(msg)

msg = ".name.keep"
raise AnonymousExprError.from_expr_name(msg)
return self._compliant_expr.__class__(
lambda df: [
expr.alias(name)
Expand All @@ -41,14 +36,9 @@ def keep(self: Self) -> DuckDBExpr:

def map(self: Self, function: Callable[[str], str]) -> DuckDBExpr:
root_names = self._compliant_expr._root_names

if root_names is None:
msg = (
"Anonymous expressions are not supported in `.name.map`.\n"
"Instead of `nw.all()`, try using a named expression, such as "
"`nw.col('a', 'b')`\n"
)
raise ValueError(msg)
msg = ".name.map"
raise AnonymousExprError.from_expr_name(msg)

output_names = [function(str(name)) for name in root_names]

Expand All @@ -70,12 +60,8 @@ def map(self: Self, function: Callable[[str], str]) -> DuckDBExpr:
def prefix(self: Self, prefix: str) -> DuckDBExpr:
root_names = self._compliant_expr._root_names
if root_names is None:
msg = (
"Anonymous expressions are not supported in `.name.prefix`.\n"
"Instead of `nw.all()`, try using a named expression, such as "
"`nw.col('a', 'b')`\n"
)
raise ValueError(msg)
msg = ".name.prefix"
raise AnonymousExprError.from_expr_name(msg)

output_names = [prefix + str(name) for name in root_names]
return self._compliant_expr.__class__(
Expand All @@ -96,12 +82,8 @@ def prefix(self: Self, prefix: str) -> DuckDBExpr:
def suffix(self: Self, suffix: str) -> DuckDBExpr:
root_names = self._compliant_expr._root_names
if root_names is None:
msg = (
"Anonymous expressions are not supported in `.name.suffix`.\n"
"Instead of `nw.all()`, try using a named expression, such as "
"`nw.col('a', 'b')`\n"
)
raise ValueError(msg)
msg = ".name.suffix"
raise AnonymousExprError.from_expr_name(msg)

output_names = [str(name) + suffix for name in root_names]

Expand All @@ -122,14 +104,10 @@ def suffix(self: Self, suffix: str) -> DuckDBExpr:

def to_lowercase(self: Self) -> DuckDBExpr:
root_names = self._compliant_expr._root_names

if root_names is None:
msg = (
"Anonymous expressions are not supported in `.name.to_lowercase`.\n"
"Instead of `nw.all()`, try using a named expression, such as "
"`nw.col('a', 'b')`\n"
)
raise ValueError(msg)
msg = ".name.to_lowercase"
raise AnonymousExprError.from_expr_name(msg)

output_names = [str(name).lower() for name in root_names]

return self._compliant_expr.__class__(
Expand All @@ -149,14 +127,9 @@ def to_lowercase(self: Self) -> DuckDBExpr:

def to_uppercase(self: Self) -> DuckDBExpr:
root_names = self._compliant_expr._root_names

if root_names is None:
msg = (
"Anonymous expressions are not supported in `.name.to_uppercase`.\n"
"Instead of `nw.all()`, try using a named expression, such as "
"`nw.col('a', 'b')`\n"
)
raise ValueError(msg)
msg = ".name.to_uppercase"
raise AnonymousExprError.from_expr_name(msg)
output_names = [str(name).upper() for name in root_names]

return self._compliant_expr.__class__(
Expand Down
53 changes: 13 additions & 40 deletions narwhals/_spark_like/expr_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import TYPE_CHECKING
from typing import Callable
from narwhals.exceptions import AnonymousExprError

if TYPE_CHECKING:
from typing_extensions import Self
Expand All @@ -15,14 +16,9 @@ def __init__(self: Self, expr: SparkLikeExpr) -> None:

def keep(self: Self) -> SparkLikeExpr:
root_names = self._compliant_expr._root_names

if root_names is None:
msg = (
"Anonymous expressions are not supported in `.name.keep`.\n"
"Instead of `nw.all()`, try using a named expression, such as "
"`nw.col('a', 'b')`\n"
)
raise ValueError(msg)
msg = ".name.keep"
raise AnonymousExprError.from_expr_name(msg)

return self._compliant_expr.__class__(
lambda df: [
Expand All @@ -41,14 +37,9 @@ def keep(self: Self) -> SparkLikeExpr:

def map(self: Self, function: Callable[[str], str]) -> SparkLikeExpr:
root_names = self._compliant_expr._root_names

if root_names is None:
msg = (
"Anonymous expressions are not supported in `.name.map`.\n"
"Instead of `nw.all()`, try using a named expression, such as "
"`nw.col('a', 'b')`\n"
)
raise ValueError(msg)
msg = ".name.map"
raise AnonymousExprError.from_expr_name(msg)

output_names = [function(str(name)) for name in root_names]

Expand All @@ -70,12 +61,8 @@ def map(self: Self, function: Callable[[str], str]) -> SparkLikeExpr:
def prefix(self: Self, prefix: str) -> SparkLikeExpr:
root_names = self._compliant_expr._root_names
if root_names is None:
msg = (
"Anonymous expressions are not supported in `.name.prefix`.\n"
"Instead of `nw.all()`, try using a named expression, such as "
"`nw.col('a', 'b')`\n"
)
raise ValueError(msg)
msg = ".name.prefix"
raise AnonymousExprError.from_expr_name(msg)

output_names = [prefix + str(name) for name in root_names]
return self._compliant_expr.__class__(
Expand All @@ -96,12 +83,8 @@ def prefix(self: Self, prefix: str) -> SparkLikeExpr:
def suffix(self: Self, suffix: str) -> SparkLikeExpr:
root_names = self._compliant_expr._root_names
if root_names is None:
msg = (
"Anonymous expressions are not supported in `.name.suffix`.\n"
"Instead of `nw.all()`, try using a named expression, such as "
"`nw.col('a', 'b')`\n"
)
raise ValueError(msg)
msg = ".name.suffix"
raise AnonymousExprError.from_expr_name(msg)

output_names = [str(name) + suffix for name in root_names]

Expand All @@ -122,14 +105,9 @@ def suffix(self: Self, suffix: str) -> SparkLikeExpr:

def to_lowercase(self: Self) -> SparkLikeExpr:
root_names = self._compliant_expr._root_names

if root_names is None:
msg = (
"Anonymous expressions are not supported in `.name.to_lowercase`.\n"
"Instead of `nw.all()`, try using a named expression, such as "
"`nw.col('a', 'b')`\n"
)
raise ValueError(msg)
msg = ".name.to_lowercase"
raise AnonymousExprError.from_expr_name(msg)
output_names = [str(name).lower() for name in root_names]

return self._compliant_expr.__class__(
Expand All @@ -149,14 +127,9 @@ def to_lowercase(self: Self) -> SparkLikeExpr:

def to_uppercase(self: Self) -> SparkLikeExpr:
root_names = self._compliant_expr._root_names

if root_names is None:
msg = (
"Anonymous expressions are not supported in `.name.to_uppercase`.\n"
"Instead of `nw.all()`, try using a named expression, such as "
"`nw.col('a', 'b')`\n"
)
raise ValueError(msg)
msg = ".name.to_uppercase"
raise AnonymousExprError.from_expr_name(msg)
output_names = [str(name).upper() for name in root_names]

return self._compliant_expr.__class__(
Expand Down

0 comments on commit fd3ee52

Please sign in to comment.