Skip to content

Commit

Permalink
Panic if a Duration string has multiple minus signs (#4358)
Browse files Browse the repository at this point in the history
  • Loading branch information
matteosantama committed Aug 11, 2022
1 parent 9ccfaf3 commit 0ee4b20
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
8 changes: 8 additions & 0 deletions polars/polars-time/src/windows/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ impl Duration {
///
/// # Panics if given str is incorrect
pub fn parse(duration: &str) -> Self {
let num_minus_signs = duration.matches('-').count();
if num_minus_signs > 1 {
panic!("a Duration string can only have a single minus sign")
}
if (num_minus_signs > 0) & !duration.starts_with('-') {
panic!("only a single minus sign is allowed, at the front of the string")
}

let mut nsecs = 0;
let mut months = 0;
let mut iter = duration.char_indices();
Expand Down
5 changes: 3 additions & 2 deletions py-polars/polars/internals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7042,8 +7042,9 @@ def offset_by(self, by: str) -> Expr:
"""
Offset this date by a relative time offset.
This differs from `pl.col("foo") + timedelta` in that it can
take months and leap years into account
This differs from ``pl.col("foo") + timedelta`` in that it can
take months and leap years into account. Note that only a single minus
sign is allowed in the ``by`` string, as the first character.
Parameters
----------
Expand Down
5 changes: 3 additions & 2 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5843,8 +5843,9 @@ def offset_by(self, by: str) -> Series:
"""
Offset this date by a relative time offset.
This differs from `pl.col("foo") + timedelta` in that it can
take months and leap years into account
This differs from ``pl.col("foo") + timedelta`` in that it can
take months and leap years into account. Note that only a single minus
sign is allowed in the ``by`` string, as the first character.
Parameters
----------
Expand Down

0 comments on commit 0ee4b20

Please sign in to comment.