Skip to content

Commit

Permalink
fix(python): fix from_dict schema_inference=0 (#5948)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Dec 30, 2022
1 parent 11a2e14 commit 24d4209
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
3 changes: 1 addition & 2 deletions py-polars/polars/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
TYPE_CHECKING,
Any,
Callable,
Dict,
ForwardRef,
Mapping,
Optional,
Expand Down Expand Up @@ -86,7 +85,7 @@ def get_args(tp: Any) -> Any:
Mapping[str, Union[PolarsDataType, PythonDataType]],
Sequence[Tuple[str, Union[PolarsDataType, PythonDataType, None]]],
]
Schema: TypeAlias = Dict[str, PolarsDataType]
Schema: TypeAlias = Mapping[str, PolarsDataType]

DTYPE_TEMPORAL_UNITS: frozenset[TimeUnit] = frozenset(["ns", "us", "ms"])

Expand Down
5 changes: 3 additions & 2 deletions py-polars/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ impl PyDataFrame {
infer_schema_length: Option<usize>,
schema_overwrite: Option<Wrap<Schema>>,
) -> PyResult<Self> {
let (rows, mut names) = dicts_to_rows(dicts, infer_schema_length.unwrap_or(50))?;
let infer_schema_length = std::cmp::max(infer_schema_length.unwrap_or(50), 1);
let (rows, mut names) = dicts_to_rows(dicts, infer_schema_length)?;

// ensure the new names are used
if let Some(schema) = &schema_overwrite {
Expand All @@ -451,7 +452,7 @@ impl PyDataFrame {
}
let mut pydf = Self::finish_from_rows(
rows,
infer_schema_length,
Some(infer_schema_length),
schema_overwrite.map(|wrap| wrap.0),
)?;

Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/unit/test_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ def test_from_dicts() -> None:
assert df.schema == {"a": pl.Int64, "b": pl.Int64}


def test_from_dict_no_inference() -> None:
schema = {"a": pl.Utf8}
data = [{"a": "aa"}]
pl.from_dicts(data, schema=schema, infer_schema_length=0)


def test_from_dicts_struct() -> None:
data = [{"a": {"b": 1, "c": 2}, "d": 5}, {"a": {"b": 3, "c": 4}, "d": 6}]
df = pl.from_dicts(data)
Expand Down

0 comments on commit 24d4209

Please sign in to comment.