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

feat(rust, python): let "ambiguous" take "null" value #11606

Closed
wants to merge 1 commit into from

Conversation

MarcoGorelli
Copy link
Collaborator

@MarcoGorelli MarcoGorelli commented Oct 9, 2023

Suggested by @stinodego here, but also:

Demo:

In [7]: df = pl.DataFrame({
   ...:     'ts': pl.datetime_range(datetime(2020, 10, 25), datetime(2020, 10, 25, 6), '1h', eager=True),
   ...:     })

In [8]: df
Out[8]: 
shape: (7, 1)
┌─────────────────────┐
│ ts                  │
│ ---                 │
│ datetime[μs]        │
╞═════════════════════╡
│ 2020-10-25 00:00:00 │
│ 2020-10-25 01:00:00 │
│ 2020-10-25 02:00:00 │
│ 2020-10-25 03:00:00 │
│ 2020-10-25 04:00:00 │
│ 2020-10-25 05:00:00 │
│ 2020-10-25 06:00:00 │
└─────────────────────┘

In [9]: df.with_columns(
   ...:     ts_replaced = pl.col('ts').dt.replace_time_zone('Europe/London', ambiguous='null')
   ...: )
Out[9]: 
shape: (7, 2)
┌─────────────────────┬─────────────────────────────┐
│ tsts_replaced                 │
│ ------                         │
│ datetime[μs]        ┆ datetime[μs, Europe/London] │
╞═════════════════════╪═════════════════════════════╡
│ 2020-10-25 00:00:002020-10-25 00:00:00 BST     │
│ 2020-10-25 01:00:00null                        │
│ 2020-10-25 02:00:002020-10-25 02:00:00 GMT     │
│ 2020-10-25 03:00:002020-10-25 03:00:00 GMT     │
│ 2020-10-25 04:00:002020-10-25 04:00:00 GMT     │
│ 2020-10-25 05:00:002020-10-25 05:00:00 GMT     │
│ 2020-10-25 06:00:002020-10-25 06:00:00 GMT     │
└─────────────────────┴─────────────────────────────┘

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Oct 9, 2023
Comment on lines -46 to +59
Some(ambiguous) => datetime.0.try_apply(|timestamp| {
let ndt = timestamp_to_datetime(timestamp);
Ok(datetime_to_timestamp(convert_to_naive_local(
&from_tz, &to_tz, ndt, ambiguous,
)?))
}),
Some(ambiguous) => {
let iter = datetime.0.downcast_iter().map(|arr| {
let element_iter = arr.iter().map(|timestamp_opt| match timestamp_opt {
Some(timestamp) => {
let ndt = timestamp_to_datetime(*timestamp);
let res = convert_to_naive_local(&from_tz, &to_tz, ndt, ambiguous)?;
Ok::<_, PolarsError>(res.map(datetime_to_timestamp))
},
None => Ok(None),
});
element_iter.try_collect_arr()
});
ChunkedArray::try_from_chunk_iter(datetime.0.name(), iter)
},
Copy link
Collaborator Author

@MarcoGorelli MarcoGorelli Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I'm seeing a slight perf regression here. small, but looks real?

https://www.kaggle.com/code/marcogorelli/polars-timing?scriptVersionId=145820321

here:

  • min: 34.58047724000062
  • max: 34.82023904700054
  • avg: 34.7417660610001 +/- 0.02811013567654737

main:

  • min: 33.56916854799965
  • max: 34.206052164999164
  • avg: 33.85647739228547 +/- 0.08204153267322174

I don't know, is there a way to do this that might preserve performance?

@MarcoGorelli MarcoGorelli marked this pull request as ready for review October 10, 2023 15:58
@MarcoGorelli MarcoGorelli marked this pull request as draft October 13, 2023 02:49
@MarcoGorelli
Copy link
Collaborator Author

closing as I don't think extra complexity is warranted here, can revisit if there's demand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant