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

columns parameter from polars.read_csv not working on empty csv files #16606

Closed
2 tasks done
rhipnox opened this issue May 30, 2024 · 3 comments · Fixed by #16739
Closed
2 tasks done

columns parameter from polars.read_csv not working on empty csv files #16606

rhipnox opened this issue May 30, 2024 · 3 comments · Fixed by #16739
Assignees
Labels
accepted Ready for implementation needs triage Awaiting prioritization by a maintainer P-low Priority: low python Related to Python Polars

Comments

@rhipnox
Copy link

rhipnox commented May 30, 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 csv

with open("empty_csv.csv", "w", newline="") as file:
    writer = csv.writer(file)
    field = ["x", "y", "z", "t"]
    writer.writerow(field)

with open("not_empty_csv.csv", "w", newline="") as file:
    writer = csv.writer(file)
    field = ["x", "y", "z", "t"]
    writer.writerow(field)
    writer.writerow(["a", "1", "d"])
    writer.writerow(["b", "2", "e"])
    writer.writerow(["c", "3", "f"])

not_empty_data = pl.read_csv(
    "not_empty_csv.csv",
    columns=["x", "y"],
    new_columns=["nx", "ny"],
    dtypes=[pl.String, pl.Int64],
).with_columns(pl.lit("q").alias("q"))

empty_data = pl.read_csv(
    "empty_csv.csv",
    columns=["x", "y"],
    new_columns=["nx", "ny"],
    dtypes=[pl.String, pl.Int64],
).with_columns(pl.lit("q").alias("q"))

Log output

file < 128 rows, no statistics determined
no. of chunks: 1 processed by: 1 threads.

Issue description

While reading an empty csv file, when attempting filter the columns with the columns parameter, it returns all columns.

Expected behavior

#sample code:
import csv

with open("empty_csv.csv", "w", newline="") as file:
    writer = csv.writer(file)
    field = ["x", "y", "z", "t"]
    writer.writerow(field)

with open("not_empty_csv.csv", "w", newline="") as file:
    writer = csv.writer(file)
    field = ["x", "y", "z", "t"]
    writer.writerow(field)
    writer.writerow(["a", "1", "d"])
    writer.writerow(["b", "2", "e"])
    writer.writerow(["c", "3", "f"])

not_empty_data = pl.read_csv(
    "not_empty_csv.csv",
    columns=["x", "y"],
    new_columns=["nx", "ny"],
    dtypes=[pl.String, pl.Int64],
).with_columns(pl.lit("q").alias("q"))

empty_data = pl.read_csv(
    "empty_csv.csv",
    columns=["x", "y"],
    new_columns=["nx", "ny"],
    dtypes=[pl.String, pl.Int64],
).with_columns(pl.lit("q").alias("q"))


"""not_empty_data returns:
>>> not_empty_data 
shape: (3, 3)
┌─────┬─────┬─────┐
│ nx  ┆ ny  ┆ q   │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ str │
╞═════╪═════╪═════╡
│ a   ┆ 1   ┆ q   │
│ b   ┆ 2   ┆ q   │
│ c   ┆ 3   ┆ q   │
└─────┴─────┴─────┘

empty_data returns:
>>> empty_data 
shape: (0, 5)
┌─────┬─────┬─────┬─────┬─────┐
│ nx  ┆ ny  ┆ z   ┆ t   ┆ q   │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ str ┆ str ┆ str │
╞═════╪═════╪═════╪═════╪═════╡
└─────┴─────┴─────┴─────┴─────┘
"""

Installed versions

--------Version info---------
Polars:               0.20.30
Index type:           UInt32
Platform:             Windows-11-10.0.22631-SP0
Python:               3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)]
@rhipnox rhipnox added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels May 30, 2024
@cmdlineluser
Copy link
Contributor

Can reproduce.

There seems to be some problem in general as scan_csv also "fails" to select just a subset of columns:

pl.scan_csv("empty_csv.csv").select("x", "y").collect()
# shape: (0, 4)
# ┌─────┬─────┬─────┬─────┐
# │ x   ┆ y   ┆ z   ┆ t   │ # <- ???
# │ --- ┆ --- ┆ --- ┆ --- │
# │ str ┆ str ┆ str ┆ str │
# ╞═════╪═════╪═════╪═════╡
# └─────┴─────┴─────┴─────┘

@ritchie46
Copy link
Member

@nameexhaustion can you take this one?

@nameexhaustion
Copy link
Collaborator

Will take a look

@nameexhaustion nameexhaustion self-assigned this May 31, 2024
@ritchie46 ritchie46 added P-low Priority: low and removed bug Something isn't working labels May 31, 2024
@c-peters c-peters added the accepted Ready for implementation label Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation needs triage Awaiting prioritization by a maintainer P-low Priority: low python Related to Python Polars
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants