-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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: single-element .dt.time() and .dt.date() should always preserve sortedness #13808
Conversation
0c08585
to
bca2e72
Compare
TimeUnit::Nanoseconds => Ok((self.0.as_ref() % NS_IN_DAY) | ||
.cast(&Int64) | ||
.unwrap() | ||
TimeUnit::Nanoseconds => Ok(self | ||
.apply_values(|v| v % NS_IN_DAY) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this shortens the code - but also ends up hitting
polars/crates/polars-core/src/chunked_array/ops/chunkops.rs
Lines 102 to 104 in 2b43fc1
if self.length <= 1 { | |
self.set_sorted_flag(IsSorted::Ascending) | |
} |
bca2e72
to
5a4a380
Compare
Honestly? I'd say add an pub fn set_sorted_flag(&mut self, sorted: IsSorted) {
if self.len() <= 1 {
self.bit_settings.insert(Settings::SORTED_ASC);
self.bit_settings.insert(Settings::SORTED_DSC);
return;
}
// ...
} Then we can benefit from this in many more places. |
5a4a380
to
46c78ac
Compare
thanks - even simpler, could be to just do the early return in |
assert pl.DataFrame({"x": [None]})["x"].flags["SORTED_ASC"] is False | ||
assert pl.DataFrame({"x": [None] * 2})["x"].flags["SORTED_ASC"] is False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a single element would now always be sorted, so I'm adding an extra one to try to preserve the spirit of the test
@@ -189,6 +189,9 @@ impl Series { | |||
} | |||
|
|||
pub fn is_sorted_flag(&self) -> IsSorted { | |||
if self.len() <= 1 { | |||
return IsSorted::Ascending; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically it is sorted both Ascending and Descending at the same time 🤔 it would be some refactoring to get that done though...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stinodego Yes I've brought up this limitation of the current sorted flag to Ritchie before. You can also run into this for larger arrays, [0, 0, 0, 0, 0]
is sorted both ascending and descending.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we replace this from compute_len
to here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expressions don't necessarily go down that path, e.g. the original implementation of casting Datetime
to Time
didn't (see #13812), so this just seemed simpler
closes #13806