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(python): No longer error when schema_overrides contains nonexistent columns #15528

Merged
merged 2 commits into from
Apr 8, 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
11 changes: 5 additions & 6 deletions py-polars/src/dataframe/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn finish_from_rows(
infer_schema_length: Option<usize>,
) -> PyResult<PyDataFrame> {
let mut schema = if let Some(mut schema) = schema {
resolve_schema_overrides(&mut schema, schema_overrides)?;
resolve_schema_overrides(&mut schema, schema_overrides);
update_schema_from_rows(&mut schema, &rows, infer_schema_length)?;
schema
} else {
Expand Down Expand Up @@ -105,15 +105,14 @@ fn update_schema_from_rows(
}

/// Override the data type of certain schema fields.
fn resolve_schema_overrides(schema: &mut Schema, schema_overrides: Option<Schema>) -> PyResult<()> {
///
/// Overrides for nonexistent columns are ignored.
fn resolve_schema_overrides(schema: &mut Schema, schema_overrides: Option<Schema>) {
if let Some(overrides) = schema_overrides {
for (name, dtype) in overrides.into_iter() {
schema.set_dtype(name.as_str(), dtype).ok_or_else(|| {
polars_err!(SchemaMismatch: "nonexistent column specified in `schema_overrides`: {name}")
}).map_err(PyPolarsErr::from)?;
schema.set_dtype(name.as_str(), dtype);
}
}
Ok(())
}

/// Erase precision/scale information from Decimal types.
Expand Down
12 changes: 10 additions & 2 deletions py-polars/tests/unit/constructors/test_dataframe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import sys
from collections import OrderedDict
from typing import Any

import pytest
Expand Down Expand Up @@ -123,9 +124,16 @@ def test_df_init_from_series_strict() -> None:
assert df["a"].dtype == pl.UInt8


# https://github.com/pola-rs/polars/issues/15471
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# https://github.com/pola-rs/polars/issues/15471

def test_df_init_rows_overrides_non_existing() -> None:
ritchie46 marked this conversation as resolved.
Show resolved Hide resolved
with pytest.raises(pl.SchemaError, match="nonexistent column"):
pl.DataFrame([{"a": 1, "b": 2}], schema_overrides={"c": pl.Int8})
df = pl.DataFrame([{"a": 1}], schema_overrides={"a": pl.Int8(), "b": pl.Boolean()})
assert df.schema == OrderedDict({"a": pl.Int8})

df = pl.DataFrame(
[{"a": 3, "b": 1.0}],
schema_overrides={"a": pl.Int8, "c": pl.Utf8},
)
assert df.schema == OrderedDict({"a": pl.Int8, "b": pl.Float64})


# https://github.com/pola-rs/polars/issues/15245
Expand Down
Loading