Skip to content

Commit 7af29e1

Browse files
committed
feat: add to_time function
1 parent d87c6e8 commit 7af29e1

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

python/datafusion/functions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,13 @@ def now() -> Expr:
10101010
return Expr(f.now())
10111011

10121012

1013+
def to_time(arg: Expr, *formatters: Expr) -> Expr:
1014+
if formatters is None:
1015+
return f.to_time(arg.expr)
1016+
formatters = [fmt.expr for fmt in formatters]
1017+
return Expr(f.to_time(arg.expr, *formatters))
1018+
1019+
10131020
def to_timestamp(arg: Expr, *formatters: Expr) -> Expr:
10141021
"""Converts a string and optional formats to a ``Timestamp`` in nanoseconds.
10151022

python/tests/test_functions.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
import math
18-
from datetime import datetime, timezone
18+
from datetime import datetime, timezone, time
1919

2020
import numpy as np
2121
import pyarrow as pa
@@ -952,6 +952,8 @@ def test_temporal_functions(df):
952952
f.to_timestamp_nanos(
953953
literal("2023-09-07 05:06:14.523952000"), literal("%Y-%m-%d %H:%M:%S.%f")
954954
),
955+
f.to_time(literal("12:30:45")),
956+
f.to_time(literal("12-30-45"), literal("%H-%M-%S")),
955957
)
956958
result = df.collect()
957959
assert len(result) == 1
@@ -1026,6 +1028,14 @@ def test_temporal_functions(df):
10261028
[datetime(2023, 9, 7, 5, 6, 14, 523952, tzinfo=DEFAULT_TZ)] * 3,
10271029
type=pa.timestamp("ns"),
10281030
)
1031+
assert result.column(17) == pa.array(
1032+
[time(12, 30, 45)] * 3,
1033+
type=pa.time64("ns"),
1034+
)
1035+
assert result.column(18) == pa.array(
1036+
[time(12, 30, 45)] * 3,
1037+
type=pa.time64("ns"),
1038+
)
10291039

10301040

10311041
def test_arrow_cast(df):

src/functions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ expr_fn!(
601601
"Converts the number to its equivalent hexadecimal representation."
602602
);
603603
expr_fn!(now);
604+
expr_fn_vec!(to_time);
604605
expr_fn_vec!(to_timestamp);
605606
expr_fn_vec!(to_timestamp_millis);
606607
expr_fn_vec!(to_timestamp_nanos);
@@ -1045,6 +1046,7 @@ pub(crate) fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
10451046
m.add_wrapped(wrap_pyfunction!(tan))?;
10461047
m.add_wrapped(wrap_pyfunction!(tanh))?;
10471048
m.add_wrapped(wrap_pyfunction!(to_hex))?;
1049+
m.add_wrapped(wrap_pyfunction!(to_time))?;
10481050
m.add_wrapped(wrap_pyfunction!(to_timestamp))?;
10491051
m.add_wrapped(wrap_pyfunction!(to_timestamp_millis))?;
10501052
m.add_wrapped(wrap_pyfunction!(to_timestamp_nanos))?;

0 commit comments

Comments
 (0)