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

Imperfect behavior with scan_csv() on zstd compressed file with new_columns param #19916

Open
2 tasks done
kscott-1 opened this issue Nov 21, 2024 · 0 comments
Open
2 tasks done
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@kscott-1
Copy link

kscott-1 commented Nov 21, 2024

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 numpy as np
import polars as pl
import zstandard as zstd

#n_rows = 6 # works
n_rows = 7 # (or higher) doesn't work

df = pl.DataFrame(
    {
        "foo": np.random.randint(1, 10, n_rows),
        "bar": np.random.randint(1, 10, n_rows),
        "baz": np.random.randint(1, 10, n_rows),
        "qux": np.random.randint(1, 10, n_rows),
        "quux": np.random.randint(1, 10, n_rows),
        "corge": np.random.randint(1, 10, n_rows),
    }
)

txtdf = df.write_csv(None, separator="\t", include_header=False)
with open("foobar.txt.zst", "wb") as zfile:
    zfile.write(zstd.ZstdCompressor().compress(txtdf.encode()))

pl.scan_csv(
    "foobar.txt.zst",
    separator="\t",
    has_header=False,
    new_columns=["foo", "bar", "baz", "qux", "quux", "corge"],
).collect()

Log output

Traceback (most recent call last):
  File "[...]", line 22, in <module>
    pl.scan_csv(
    ~~~~~~~~~~~^
        "foobar.txt.zst",
        ^^^^^^^^^^^^^^^^^
    ...<2 lines>...
        new_columns=["foo", "bar", "baz", "qux", "quux", "corge"],
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ).collect()
    ^
  File "[...]/lib/python3.13/site-packages/polars/_utils/deprecation.py", line 92, in wrapper
    return function(*args, **kwargs)
  File "[...]/lib/python3.13/site-packages/polars/_utils/deprecation.py", line 92, in wrapper
    return function(*args, **kwargs)
  File "[...]/lib/python3.13/site-packages/polars/_utils/deprecation.py", line 92, in wrapper
    return function(*args, **kwargs)
  File "[...]/lib/python3.13/site-packages/polars/io/csv/functions.py", line 1296, in scan_csv
    return _scan_csv_impl(
        source,
    ...<30 lines>...
        include_file_paths=include_file_paths,
    )
  File "[...]/lib/python3.13/site-packages/polars/io/csv/functions.py", line 1394, in _scan_csv_impl
    pylf = PyLazyFrame.new_from_csv(
        source,
    ...<30 lines>...
        include_file_paths=include_file_paths,
    )
polars.exceptions.ShapeError: The length of the new names list should be equal to or less than the original column length

Issue description

When scanning zstd compressed text files, there is strange behavior on whether or not it is possible to define a new set of column names for a headerless file without error. This is related to #17841 which seems to have brought forth the support that didn't exist for compressed files in scan_csv() previously.

Expected behavior

A list of column names should populate a lazy frame during scanning when the number of columns is equal to the number of elements in the list.

Installed versions

--------Version info---------
Polars:              1.14.0
Index type:          UInt32
Platform:            macOS-15.1-arm64-arm-64bit-Mach-O
Python:              3.13.0 (main, Oct  7 2024, 05:02:14) [Clang 16.0.0 (clang-1600.0.26.3)]
LTS CPU:             False

----Optional dependencies----
adbc_driver_manager  <not installed>
altair               <not installed>
boto3                <not installed>
cloudpickle          <not installed>
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               <not installed>
gevent               <not installed>
google.auth          <not installed>
great_tables         <not installed>
matplotlib           <not installed>
nest_asyncio         <not installed>
numpy                2.1.3
openpyxl             <not installed>
pandas               <not installed>
pyarrow              <not installed>
pydantic             <not installed>
pyiceberg            <not installed>
sqlalchemy           <not installed>
torch                <not installed>
xlsx2csv             <not installed>
xlsxwriter           <not installed>
@kscott-1 kscott-1 added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars
Projects
None yet
Development

No branches or pull requests

1 participant