Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rust, python): implement missing conversion to python time object #5152

Merged
merged 1 commit into from
Oct 11, 2022
Merged

fix(rust, python): implement missing conversion to python time object #5152

merged 1 commit into from
Oct 11, 2022

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Oct 10, 2022

The AnyValue::Time match implementation for into_py was returning the underlying integer representation; this PR ensures that we now create proper python time objects instead.

Was most apparent in calls to DataFrame.rows() for any frame containing time cols. (Interestingly Series.to_list() was fine, as the Wrap implementation for TimeChunked handles it correctly).

Example

from datetime import datetime
import polars as pl

df = pl.DataFrame({
    "dtm": [
        datetime(2020,1,2,10,30,45),
        datetime(2022,3,4,23,59,59),
    ]
}).with_column(
    pl.col("dtm").cast(pl.Time).alias("tm")
)
# ┌─────────────────────┬──────────┐
# │ dtm                 ┆ tm       │
# │ ---                 ┆ ---      │
# │ datetime[μs]        ┆ time     │
# ╞═════════════════════╪══════════╡
# │ 2020-01-02 10:30:45 ┆ 10:30:45 │
# ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ 2022-03-04 23:59:59 ┆ 23:59:59 │
# └─────────────────────┴──────────┘

Before: (raw integers)

df.rows()
# [(datetime(2020,1,2,10,30,45), 37845000000000),
#  (datetime(2022,3,4,23,59,59), 86399000000000),]

After: (time values)

df.rows()
# [(datetime(2020,1,2,10,30,45), time(10,30,45)), 
#  (datetime(2022,3,4,23,59,59), time(23,59,59)),]

Note on updated unit tests

Quite a lot of the unit tests weren't actually validating the result; they were validating the shape of the result (which is how this one didn't get spotted earlier). I've updated most of the temporal unit tests such that they also check the returned values as well.

For example: test_filter_time would have caught this, but was written as...

assert df.filter(pl.col("t") < pl.lit(time(11, 0))).shape[0] == 3

I've now updated it (and some others) to be more explicit, eg:

assert df.filter(pl.col("t") < pl.lit(time(11, 0))).rows() == expected_times

@ritchie46
Copy link
Member

Great, thanks!

@ritchie46 ritchie46 merged commit 4e79ae8 into pola-rs:master Oct 11, 2022
@ritchie46 ritchie46 changed the title fix(rust): implement missing conversion to python time object fix(rust, python): implement missing conversion to python time object Oct 11, 2022
@github-actions github-actions bot added the python Related to Python Polars label Oct 11, 2022
@alexander-beedie alexander-beedie deleted the py-time-object-conversion branch October 11, 2022 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants