Skip to content

ENH: Add Polars engine to read_csv (#61813) #61983

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,24 @@ def _read(
kwds["parse_dates"] = False
else:
kwds["parse_dates"] = True
# 🆕 Handle engine='polars'
if kwds.get("engine") == "polars":
try:
import polars as pl
except ImportError:
raise ImportError(
"Polars is not installed. Please install it with 'pip install polars'."
)

pl_args = {}
if "nrows" in kwds:
pl_args["n_rows"] = kwds["nrows"]
if "encoding" in kwds:
pl_args["encoding"] = kwds["encoding"]

df = pl.read_csv(filepath_or_buffer, **pl_args)
return df.to_pandas()


# Extract some of the arguments (pass chunksize on).
iterator = kwds.get("iterator", False)
Expand Down Expand Up @@ -1791,6 +1809,9 @@ def _refine_defaults_read(
kwds["delimiter"] = delimiter

if engine is not None:
if engine not in ["c", "python", "pyarrow", "polars"]:
raise ValueError(f"Unknown engine: {engine}")
kwds["engine"] = engine
kwds["engine_specified"] = True
else:
kwds["engine"] = "c"
Expand Down
12 changes: 12 additions & 0 deletions pandas/io/parsers/test_read_csv_polars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest

def test_read_csv_with_polars(tmp_path):
pl = pytest.importorskip("polars")
pd = pytest.importorskip("pandas")

file = tmp_path / "data.csv"
file.write_text("a,b\n1,2\n3,4")

df = pd.read_csv(file, engine="polars")
assert df.shape == (2, 2)
assert list(df.columns) == ["a", "b"]
Loading