Skip to content

Commit

Permalink
feat[rust]: show problematic values in failed cast (#4803)
Browse files Browse the repository at this point in the history
  • Loading branch information
matteosantama committed Sep 12, 2022
1 parent af2d161 commit e0167a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 8 additions & 4 deletions polars/polars-core/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,14 +673,18 @@ impl Series {
pub fn strict_cast(&self, data_type: &DataType) -> Result<Series> {
let s = self.cast(data_type)?;
if self.null_count() != s.null_count() {
let failure_mask = !self.is_null() & s.is_null();
let failures = self.filter_threaded(&failure_mask, false)?.unique()?;
Err(PolarsError::ComputeError(
format!(
"strict conversion of cast from {:?} to {:?} failed. consider non-strict cast.\n
If you were trying to cast Utf8 to Date,Time,Datetime, consider using `strptime`",
"Strict conversion from {:?} to {:?} failed for values {}. \
If you were trying to cast Utf8 to Date, Time, or Datetime, \
consider using `strptime`.",
self.dtype(),
data_type
data_type,
failures.fmt_list(),
)
.into(),
.into(),
))
} else {
Ok(s)
Expand Down
4 changes: 4 additions & 0 deletions py-polars/tests/unit/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ def test_cast() -> None:
assert a.cast(pl.Datetime).dtype == pl.Datetime
assert a.cast(pl.Date).dtype == pl.Date

# display failed values, GH#4706
with pytest.raises(pl.ComputeError, match="foobar"):
pl.Series(["1", "2", "3", "4", "foobar"]).cast(int)


def test_to_python() -> None:
a = pl.Series("a", range(20))
Expand Down

0 comments on commit e0167a4

Please sign in to comment.