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

schema_overrides in pl.read_csv_batched requires full schema instead of partial overrides #19685

Closed
2 tasks done
aruasouza opened this issue Nov 7, 2024 · 0 comments · Fixed by #19755
Closed
2 tasks done
Assignees
Labels
accepted Ready for implementation bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@aruasouza
Copy link

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
import numpy as np

df = pl.DataFrame({'a':np.random.rand(100),'b':np.random.rand(100)})
df.write_csv('data.csv')

reader = pl.read_csv_batched('data.csv',schema_overrides={'a':pl.String})
batches = reader.next_batches(1)

Log output

---------------------------------------------------------------------------
ComputeError                              Traceback (most recent call last)
Cell In[31], line 5
      2 df.write_csv('data.csv')
      4 reader = pl.read_csv_batched('data.csv',schema_overrides={'a':pl.String})
----> 5 batches = reader.next_batches(1)

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\polars\io\csv\batched_reader.py:133, in BatchedCsvReader.next_batches(self, n)
    109 def next_batches(self, n: int) -> list[DataFrame] | None:
    110     """
    111     Read `n` batches from the reader.
    112 
   (...)
    131     list of DataFrames
    132     """
--> 133     if (batches := self._reader.next_batches(n)) is not None:
    134         if self.new_columns:
    135             return [
    136                 _update_columns(wrap_df(df), self.new_columns) for df in batches
    137             ]

ComputeError: found more fields than defined in 'Schema'

Consider setting 'truncate_ragged_lines=True'.

Issue description

The schema_overrides parameter in pl.read_csv_batched currently requires the complete schema definition. If only a partial schema is provided (as intended for overrides), it raises an error identical to the one raised by pl.read_csv when its schema parameter is not fully specified.

Expected behavior

According to the documentation, schema_overrides should accept any number of columns, allowing users to specify only the columns they wish to override. This matches the behavior of schema_overrides in pl.read_csv, which does not require the entire schema to function.

Installed versions

--------Version info---------
Polars:              1.12.0
Index type:          UInt32
Platform:            Windows-10-10.0.19045-SP0
Python:              3.12.7 (tags/v3.12.7:0b05ead, Oct  1 2024, 03:06:41) [MSC v.1941 64 bit (AMD64)]
LTS CPU:             False

----Optional dependencies----
adbc_driver_manager  <not installed>
altair               
<not installed>
cloudpickle          <not installed>
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               <not installed>
gevent               <not installed>
great_tables         <not installed>
matplotlib           3.9.2
nest_asyncio         1.6.0
numpy                1.26.4
openpyxl             <not installed>
pandas               2.2.3
pyarrow              17.0.0
pydantic             <not installed>
pyiceberg            <not installed>
sqlalchemy           <not installed>
torch                <not installed>
xlsx2csv             <not installed>
xlsxwriter           <not installed>
@aruasouza aruasouza added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Nov 7, 2024
@ritchie46 ritchie46 self-assigned this Nov 13, 2024
@c-peters c-peters added the accepted Ready for implementation label Nov 18, 2024
@c-peters c-peters added this to Backlog Nov 18, 2024
@c-peters c-peters moved this to Done in Backlog Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants