Skip to content

Commit

Permalink
Propagate strictness to from_dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Mar 27, 2024
1 parent e67dfc4 commit c6636a5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
16 changes: 12 additions & 4 deletions py-polars/polars/_utils/construction/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ def _sequence_of_sequence_to_pydf(
if unpack_nested:
dicts = [nt_unpack(d) for d in data]
pydf = PyDataFrame.from_dicts(
dicts, infer_schema_length=infer_schema_length
dicts, strict=strict, infer_schema_length=infer_schema_length
)
else:
pydf = PyDataFrame.from_rows(
Expand Down Expand Up @@ -675,6 +675,7 @@ def _sequence_of_dict_to_pydf(
data,
dicts_schema,
schema_overrides,
strict=strict,
infer_schema_length=infer_schema_length,
)

Expand Down Expand Up @@ -774,7 +775,9 @@ def _sequence_of_dataclasses_to_pydf(
)
if unpack_nested:
dicts = [asdict(md) for md in data]
pydf = PyDataFrame.from_dicts(dicts, infer_schema_length=infer_schema_length)
pydf = PyDataFrame.from_dicts(
dicts, strict=strict, infer_schema_length=infer_schema_length
)
else:
rows = [astuple(dc) for dc in data]
pydf = PyDataFrame.from_rows(
Expand Down Expand Up @@ -823,7 +826,9 @@ def _sequence_of_pydantic_models_to_pydf(
if old_pydantic
else [md.model_dump(mode="python") for md in data]
)
pydf = PyDataFrame.from_dicts(dicts, infer_schema_length=infer_schema_length)
pydf = PyDataFrame.from_dicts(
dicts, strict=strict, infer_schema_length=infer_schema_length
)

elif len(model_fields) > 50:
# 'from_rows' is the faster codepath for models with a lot of fields...
Expand All @@ -836,7 +841,10 @@ def _sequence_of_pydantic_models_to_pydf(
# ...and 'from_dicts' is faster otherwise
dicts = [md.__dict__ for md in data]
pydf = PyDataFrame.from_dicts(
dicts, schema=overrides, infer_schema_length=infer_schema_length
dicts,
schema=overrides,
strict=strict,
infer_schema_length=infer_schema_length,
)

if overrides:
Expand Down
11 changes: 5 additions & 6 deletions py-polars/src/dataframe/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ impl PyDataFrame {
}

#[staticmethod]
#[pyo3(signature = (data, schema=None, schema_overrides=None, infer_schema_length=None))]
#[pyo3(signature = (data, schema=None, schema_overrides=None, strict=true, infer_schema_length=None))]
pub fn from_dicts(
py: Python,
data: &PyAny,
schema: Option<Wrap<Schema>>,
schema_overrides: Option<Wrap<Schema>>,
strict: bool,
infer_schema_length: Option<usize>,
) -> PyResult<Self> {
let schema = schema.map(|wrap| wrap.0);
let schema_overrides = schema_overrides.map(|wrap| wrap.0);

let names = get_schema_names(data, schema.as_ref(), infer_schema_length)?;
let rows = dicts_to_rows(data, &names)?;
let rows = dicts_to_rows(data, &names, strict)?;

let schema = schema.or_else(|| {
Some(columns_names_to_empty_schema(
Expand Down Expand Up @@ -134,7 +135,7 @@ where
Schema::from_iter(fields)
}

fn dicts_to_rows<'a>(data: &'a PyAny, names: &'a [String]) -> PyResult<Vec<Row<'a>>> {
fn dicts_to_rows<'a>(data: &'a PyAny, names: &'a [String], strict: bool) -> PyResult<Vec<Row<'a>>> {
let len = data.len()?;
let mut rows = Vec::with_capacity(len);
for d in data.iter()? {
Expand All @@ -145,9 +146,7 @@ fn dicts_to_rows<'a>(data: &'a PyAny, names: &'a [String]) -> PyResult<Vec<Row<'
for k in names.iter() {
let val = match d.get_item(k)? {
None => AnyValue::Null,
// TODO: Propagate strictness here.
// https://github.com/pola-rs/polars/issues/14427
Some(val) => py_object_to_any_value(val, false)?,
Some(val) => py_object_to_any_value(val, strict)?,
};
row.push(val)
}
Expand Down

0 comments on commit c6636a5

Please sign in to comment.