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: Respect strictness in list constructor #18853

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crates/polars-core/src/series/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,14 @@ fn any_values_to_list(
Some(b.clone())
} else {
match b.cast(inner_type) {
Ok(out) => Some(out),
Ok(out) => {
if out.null_count() != b.null_count() {
valid = !strict;
}
Some(out)
},
Err(_) => {
valid = !strict;
Some(Series::full_null(b.name().clone(), b.len(), inner_type))
},
}
Expand Down
28 changes: 14 additions & 14 deletions crates/polars-python/src/series/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,19 @@ init_method_opt!(new_opt_i64, Int64Type, i64);
init_method_opt!(new_opt_f32, Float32Type, f32);
init_method_opt!(new_opt_f64, Float64Type, f64);

fn convert_to_avs<'a>(values: &'a Bound<'a, PyAny>, strict: bool) -> PyResult<Vec<AnyValue<'a>>> {
values
.iter()?
.map(|v| py_object_to_any_value(&(v?).as_borrowed(), strict))
.collect()
}

#[pymethods]
impl PySeries {
#[staticmethod]
fn new_from_any_values(name: &str, values: &Bound<PyAny>, strict: bool) -> PyResult<Self> {
let any_values_result = values
.iter()?
.map(|v| py_object_to_any_value(&(v?).as_borrowed(), strict))
.collect::<PyResult<Vec<AnyValue>>>();
let result = any_values_result.and_then(|avs| {
let avs = convert_to_avs(values, strict);
let result = avs.and_then(|avs| {
let s = Series::from_any_values(name.into(), avs.as_slice(), strict).map_err(|e| {
PyTypeError::new_err(format!(
"{e}\n\nHint: Try setting `strict=False` to allow passing data with mixed types."
Expand Down Expand Up @@ -211,17 +215,13 @@ impl PySeries {
dtype: Wrap<DataType>,
strict: bool,
) -> PyResult<Self> {
let any_values = values
.iter()?
.map(|v| py_object_to_any_value(&(v?).as_borrowed(), strict))
.collect::<PyResult<Vec<AnyValue>>>()?;
let s =
Series::from_any_values_and_dtype(name.into(), any_values.as_slice(), &dtype.0, strict)
.map_err(|e| {
PyTypeError::new_err(format!(
let avs = convert_to_avs(values, strict)?;
let s = Series::from_any_values_and_dtype(name.into(), avs.as_slice(), &dtype.0, strict)
.map_err(|e| {
PyTypeError::new_err(format!(
"{e}\n\nHint: Try setting `strict=False` to allow passing data with mixed types."
))
})?;
})?;
Ok(s.into())
}

Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/unit/constructors/test_strictness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest

import polars as pl


def test_list_constructor_strictness() -> None:
with pytest.raises(TypeError, match="setting `strict=False`"):
pl.Series([[1], ["two"]], strict=True)