You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by ti...@apache.org on 2025/04/27 14:14:59 UTC
(datafusion-python) branch main updated: Improve col class access using __getattr__
This is an automated email from the ASF dual-hosted git repository.
timsaucer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-python.git
The following commit(s) were added to refs/heads/main by this push:
new 00dea113 Improve col class access using __getattr__
00dea113 is described below
commit 00dea113eb85d54b758eb3451ea448c7b9263c1c
Author: deanm0000 <37...@users.noreply.github.com>
AuthorDate: Sun Apr 27 10:14:54 2025 -0400
Improve col class access using __getattr__
Co-authored-by: Tim Saucer <ti...@gmail.com>
---
python/datafusion/__init__.py | 12 ++----------
python/datafusion/col.py | 45 +++++++++++++++++++++++++++++++++++++++++++
python/tests/test_expr.py | 23 ++++++++++++++++++++++
3 files changed, 70 insertions(+), 10 deletions(-)
diff --git a/python/datafusion/__init__.py b/python/datafusion/__init__.py
index 60d0d61b..15ceefbd 100644
--- a/python/datafusion/__init__.py
+++ b/python/datafusion/__init__.py
@@ -26,6 +26,8 @@ try:
except ImportError:
import importlib_metadata
+from datafusion.col import col, column
+
from . import functions, object_store, substrait, unparser
# The following imports are okay to remain as opaque to the user.
@@ -95,16 +97,6 @@ __all__ = [
]
-def column(value: str) -> Expr:
- """Create a column expression."""
- return Expr.column(value)
-
-
-def col(value: str) -> Expr:
- """Create a column expression."""
- return Expr.column(value)
-
-
def literal(value) -> Expr:
"""Create a literal expression."""
return Expr.literal(value)
diff --git a/python/datafusion/col.py b/python/datafusion/col.py
new file mode 100644
index 00000000..1141dc09
--- /dev/null
+++ b/python/datafusion/col.py
@@ -0,0 +1,45 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Col class."""
+
+from datafusion.expr import Expr
+
+
+class Col:
+ """Create a column expression.
+
+ This helper class allows an extra syntax of creating columns using the __getattr__
+ method.
+ """
+
+ def __call__(self, value: str) -> Expr:
+ """Create a column expression."""
+ return Expr.column(value)
+
+ def __getattr__(self, value: str) -> Expr:
+ """Create a column using attribute syntax."""
+ # For autocomplete to work with IPython
+ if value.startswith("__wrapped__"):
+ return getattr(type(self), value)
+
+ return Expr.column(value)
+
+
+col: Col = Col()
+column: Col = Col()
+__all__ = ["col", "column"]
diff --git a/python/tests/test_expr.py b/python/tests/test_expr.py
index dcf75f02..3651b60d 100644
--- a/python/tests/test_expr.py
+++ b/python/tests/test_expr.py
@@ -249,6 +249,29 @@ def test_fill_null(df):
assert result.column(2) == pa.array([1234, 1234, 8])
+def test_col_getattr():
+ ctx = SessionContext()
+ data = {
+ "array_values": [[1, 2, 3], [4, 5], [6], []],
+ "struct_values": [
+ {"name": "Alice", "age": 15},
+ {"name": "Bob", "age": 14},
+ {"name": "Charlie", "age": 13},
+ {"name": None, "age": 12},
+ ],
+ }
+ df = ctx.from_pydict(data, name="table1")
+
+ names = df.select(col.struct_values["name"].alias("name")).collect()
+ names = [r.as_py() for rs in names for r in rs["name"]]
+
+ array_values = df.select(col.array_values[1].alias("value")).collect()
+ array_values = [r.as_py() for rs in array_values for r in rs["value"]]
+
+ assert names == ["Alice", "Bob", "Charlie", None]
+ assert array_values == [2, 5, None, None]
+
+
def test_alias_with_metadata(df):
df = df.select(col("a").alias("b", {"key": "value"}))
assert df.schema().field("b").metadata == {b"key": b"value"}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org