-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Add Arrow CSV Reader #43072
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
Merged
Merged
ENH: Add Arrow CSV Reader #43072
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
dfd83f8
ENH: Add pyarrow csv reader
lithomas1 33c2ba6
merge master
lithomas1 ba0cc8b
address review comments
lithomas1 b9d7f8b
fix some tests
lithomas1 b6db201
xfail/skip more
lithomas1 c9ce8ff
more test xfails/skips
lithomas1 fa77aaf
maybe green?
lithomas1 9ced2f2
green?
lithomas1 43495a5
Apply suggestions from code review
lithomas1 83d0c25
Merge branch 'master' into enh-arrow-csv
lithomas1 3a3352a
code review
lithomas1 3f0568f
fix typos
lithomas1 3114da5
Merge branch 'pandas-dev:master' into enh-arrow-csv
lithomas1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
from __future__ import annotations | ||
|
||
from pandas._typing import FilePathOrBuffer | ||
from pandas.compat._optional import import_optional_dependency | ||
|
||
from pandas.core.dtypes.inference import is_integer | ||
|
||
from pandas.core.frame import DataFrame | ||
|
||
from pandas.io.common import get_handle | ||
from pandas.io.parsers.base_parser import ParserBase | ||
|
||
|
||
class ArrowParserWrapper(ParserBase): | ||
""" | ||
Wrapper for the pyarrow engine for read_csv() | ||
""" | ||
|
||
def __init__(self, src: FilePathOrBuffer, **kwds): | ||
self.kwds = kwds | ||
self.src = src | ||
|
||
ParserBase.__init__(self, kwds) | ||
|
||
self._parse_kwds() | ||
|
||
def _parse_kwds(self): | ||
""" | ||
Validates keywords before passing to pyarrow. | ||
""" | ||
encoding: str | None = self.kwds.get("encoding") | ||
self.encoding = "utf-8" if encoding is None else encoding | ||
|
||
self.usecols, self.usecols_dtype = self._validate_usecols_arg( | ||
self.kwds["usecols"] | ||
) | ||
na_values = self.kwds["na_values"] | ||
if isinstance(na_values, dict): | ||
raise ValueError( | ||
"The pyarrow engine doesn't support passing a dict for na_values" | ||
) | ||
self.na_values = list(self.kwds["na_values"]) | ||
|
||
def _get_pyarrow_options(self): | ||
""" | ||
Rename some arguments to pass to pyarrow | ||
""" | ||
mapping = { | ||
"usecols": "include_columns", | ||
"na_values": "null_values", | ||
"escapechar": "escape_char", | ||
"skip_blank_lines": "ignore_empty_lines", | ||
} | ||
for pandas_name, pyarrow_name in mapping.items(): | ||
if pandas_name in self.kwds and self.kwds.get(pandas_name) is not None: | ||
self.kwds[pyarrow_name] = self.kwds.pop(pandas_name) | ||
|
||
self.parse_options = { | ||
option_name: option_value | ||
for option_name, option_value in self.kwds.items() | ||
if option_value is not None | ||
and option_name | ||
in ("delimiter", "quote_char", "escape_char", "ignore_empty_lines") | ||
} | ||
self.convert_options = { | ||
option_name: option_value | ||
for option_name, option_value in self.kwds.items() | ||
if option_value is not None | ||
and option_name | ||
in ("include_columns", "null_values", "true_values", "false_values") | ||
} | ||
self.read_options = { | ||
"autogenerate_column_names": self.header is None, | ||
"skip_rows": self.header | ||
if self.header is not None | ||
else self.kwds["skiprows"], | ||
} | ||
|
||
def _finalize_output(self, frame: DataFrame) -> DataFrame: | ||
""" | ||
Processes data read in based on kwargs. | ||
|
||
Parameters | ||
---------- | ||
frame: DataFrame | ||
The DataFrame to process. | ||
|
||
Returns | ||
------- | ||
DataFrame | ||
The processed DataFrame. | ||
""" | ||
num_cols = len(frame.columns) | ||
if self.header is None: | ||
if self.names is None: | ||
if self.prefix is not None: | ||
self.names = [f"{self.prefix}{i}" for i in range(num_cols)] | ||
elif self.header is None: | ||
self.names = range(num_cols) | ||
frame.columns = self.names | ||
# we only need the frame not the names | ||
frame.columns, frame = self._do_date_conversions(frame.columns, frame) | ||
if self.index_col is not None: | ||
for i, item in enumerate(self.index_col): | ||
if is_integer(item): | ||
self.index_col[i] = frame.columns[item] | ||
frame.set_index(self.index_col, drop=True, inplace=True) | ||
|
||
if self.kwds.get("dtype") is not None: | ||
frame = frame.astype(self.kwds.get("dtype")) | ||
return frame | ||
|
||
def read(self) -> DataFrame: | ||
""" | ||
Reads the contents of a CSV file into a DataFrame and | ||
processes it according to the kwargs passed in the | ||
constructor. | ||
|
||
Returns | ||
------- | ||
DataFrame | ||
The DataFrame created from the CSV file. | ||
""" | ||
pyarrow_csv = import_optional_dependency("pyarrow.csv") | ||
self._get_pyarrow_options() | ||
|
||
with get_handle( | ||
self.src, "rb", encoding=self.encoding, is_text=False | ||
) as handles: | ||
table = pyarrow_csv.read_csv( | ||
handles.handle, | ||
read_options=pyarrow_csv.ReadOptions(**self.read_options), | ||
parse_options=pyarrow_csv.ParseOptions(**self.parse_options), | ||
convert_options=pyarrow_csv.ConvertOptions(**self.convert_options), | ||
) | ||
|
||
frame = table.to_pandas() | ||
return self._finalize_output(frame) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.