Skip to content

Commit

Permalink
fix conversion to rust from pw.Duration (#5519)
Browse files Browse the repository at this point in the history
* fix conversion to rust from duration

* tests

* changelog

* Update public/pathway/CHANGELOG.md

Co-authored-by: Michał Bartoszkiewicz <[email protected]>

---------

Co-authored-by: Michał Bartoszkiewicz <[email protected]>
GitOrigin-RevId: a5eec69638f424e08da143bd190b114fd7a39b77
  • Loading branch information
2 people authored and Manul from Pathway committed Jan 29, 2024
1 parent 3933fae commit 32042a5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]

### Fixed
- Returning `pw.Duration` from UDFs or using them as constant values no longer results in errors.

## [0.7.10] - 2024-01-26

### Added
Expand Down
53 changes: 53 additions & 0 deletions python/pathway/tests/expressions/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,3 +1014,56 @@ def test_weekday(is_naive: bool) -> None:
table_pw = table.select(txt=table.ts.dt.weekday())
table_pd = table_from_pandas(df_new)
assert_table_equality(table_pw, table_pd)


def test_pathway_duration():
t = table_from_markdown(
"""
value
1
"""
)

@pw.udf
def to_duration(a) -> pw.Duration:
return pw.Duration(days=a)

result = t.select(value=to_duration(pw.this.value))
assert_table_equality(
result, table_from_pandas(pd.DataFrame({"value": [pd.Timedelta(days=1)]}))
)


def test_pathway_datetimes():
@pw.udf
def to_naive(year, month, day) -> pw.DateTimeNaive:
return pw.DateTimeNaive(year=year, month=month, day=day)

@pw.udf
def to_utc(year, month, day) -> pw.DateTimeUtc:
return pw.DateTimeUtc(year=year, month=month, day=day, tz=tz.UTC)

t = table_from_markdown(
"""
year | month | day
2023 | 8 | 12
"""
)

result = t.select(value=to_naive(pw.this.year, pw.this.month, pw.this.day))
assert_table_equality(
result,
table_from_pandas(
pd.DataFrame({"value": [pd.Timestamp(year=2023, month=8, day=12)]})
),
)

result = t.select(value=to_utc(pw.this.year, pw.this.month, pw.this.day))
assert_table_equality(
result,
table_from_pandas(
pd.DataFrame(
{"value": [pd.Timestamp(year=2023, month=8, day=12, tz=tz.UTC)]}
)
),
)
4 changes: 2 additions & 2 deletions src/python_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ impl<'source> FromPyObject<'source> for Value {
return value_from_python_datetime(ob);
} else if type_name == "timedelta" {
return value_from_python_timedelta(ob);
} else if type_name == "Timestamp" {
} else if matches!(type_name, "Timestamp" | "DateTimeNaive" | "DateTimeUtc") {
return value_from_pandas_timestamp(ob);
} else if type_name == "Timedelta" {
} else if matches!(type_name, "Timedelta" | "Duration") {
return value_from_pandas_timedelta(ob);
} else if type_name == "Json" {
return value_json_from_py_any(ob.getattr("value")?);
Expand Down

0 comments on commit 32042a5

Please sign in to comment.