Skip to content

Commit dec07e7

Browse files
committed
feat: add extract function as an alias for date_part
1 parent 54e5e0d commit dec07e7

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

python/datafusion/functions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
"empty",
129129
"encode",
130130
"ends_with",
131+
"extract",
131132
"exp",
132133
"factorial",
133134
"find_in_set",
@@ -994,6 +995,14 @@ def date_part(part: Expr, date: Expr) -> Expr:
994995
return Expr(f.date_part(part.expr, date.expr))
995996

996997

998+
def extract(part: Expr, date: Expr) -> Expr:
999+
"""Extracts a subfield from the date.
1000+
1001+
This is an alias for :py:func:`date_part`.
1002+
"""
1003+
return date_part(part, date)
1004+
1005+
9971006
def date_trunc(part: Expr, date: Expr) -> Expr:
9981007
"""Truncates the date to a specified level of precision."""
9991008
return Expr(f.date_trunc(part.expr, date.expr))

python/tests/test_functions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ def test_temporal_functions(df):
866866
f.to_timestamp_seconds(literal("2023-09-07 05:06:14.523952")),
867867
f.to_timestamp_millis(literal("2023-09-07 05:06:14.523952")),
868868
f.to_timestamp_micros(literal("2023-09-07 05:06:14.523952")),
869+
f.extract(literal("day"), column("d")),
869870
)
870871
result = df.collect()
871872
assert len(result) == 1
@@ -903,6 +904,7 @@ def test_temporal_functions(df):
903904
assert result.column(9) == pa.array(
904905
[datetime(2023, 9, 7, 5, 6, 14, 523952)] * 3, type=pa.timestamp("us")
905906
)
907+
assert result.column(10) == pa.array([31, 26, 2], type=pa.float64())
906908

907909

908910
def test_case(df):

0 commit comments

Comments
 (0)