Skip to content

Type Casting string to date does not for list of dicts #12900

@AdrianHaylerQC

Description

@AdrianHaylerQC

Checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

import polars as pl
pl.DataFrame([{'test':'2023-11-08'}], schema={'test':pl.Date})

Log output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ahayler/miniforge3/lib/python3.10/os.py", line 685, in __setitem__
    value = self.encodevalue(value)
  File "/Users/ahayler/miniforge3/lib/python3.10/os.py", line 757, in encode
    raise TypeError("str expected, not %s" % type(value).__name__)
TypeError: str expected, not int
>>> os.environ["POLARS_VERBOSE"]="1"
>>> pl.DataFrame([{'test':'2023-11-08'}], schema={'test':pl.Date})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ahayler/miniforge3/lib/python3.10/site-packages/polars/dataframe/frame.py", line 368, in __init__
    self._df = sequence_to_pydf(
  File "/Users/ahayler/miniforge3/lib/python3.10/site-packages/polars/utils/_construction.py", line 940, in sequence_to_pydf
    return _sequence_to_pydf_dispatcher(
  File "/Users/ahayler/miniforge3/lib/python3.10/functools.py", line 889, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "/Users/ahayler/miniforge3/lib/python3.10/site-packages/polars/utils/_construction.py", line 1159, in _sequence_of_dict_to_pydf
    pydf = PyDataFrame.read_dicts(
polars.exceptions.ComputeError: could not append value: "2023-11-08" of type: str to the builder; make sure that all rows have the same schema or consider increasing `infer_schema_length`

it might also be that a value overflows the data-type's capacity

Issue description

This issue occurs when I try to parse strings as dates into a polars DataFrame from a list of dicts. Removing the list wrapper from the code above creates the expected DataFrame.

Expected behavior

A one row, one column dataframe with '2023-11-08' in column 'test' of dtype pl.Date.

Installed versions

Details
--------Version info---------
Polars:               0.19.19
Index type:           UInt32
Platform:             macOS-13.2.1-arm64-arm-64bit
Python:               3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:41:52) [Clang 15.0.7 ]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          <not installed>
connectorx:           <not installed>
deltalake:            <not installed>
fsspec:               <not installed>
gevent:               <not installed>
matplotlib:           <not installed>
numpy:                <not installed>
openpyxl:             <not installed>
pandas:               <not installed>
pyarrow:              <not installed>
pydantic:             <not installed>
pyiceberg:            <not installed>
pyxlsb:               <not installed>
sqlalchemy:           <not installed>
xlsx2csv:             <not installed>
xlsxwriter:           <not installed>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds triageAwaiting prioritization by a maintainerpythonRelated to Python Polars

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions