-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
ENH: use native filesystem (if available) for read_parquet with pyarrow engine #51609
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
Changes from 30 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
89e6f9a
ENH: Add filesystem to read_parquet/to_parquet
mroeschke 06a2b18
Add to to_parquet
mroeschke 35d8cbb
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke 0b07437
Bump fsspec
mroeschke f57a195
fix import
mroeschke 6503a7e
Mock gcs to local for parquet
mroeschke 16a3da2
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke dd27604
Fix condidition, add whatsnew
mroeschke 4ce7da8
address tests, bump gcsfs
mroeschke 0b3bd7d
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke e1f8912
bump s3fs
mroeschke 49f2fc8
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke c5166b5
Fix doc issues
mroeschke 7ec7d75
Try without fsspec wrapper
mroeschke 1b9ab77
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke 4ce52cd
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke 553105c
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke c1161d3
Revert "Try without fsspec wrapper"
mroeschke ef7095a
Returns a tuple
mroeschke e6a61a9
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke 08f3a30
Don't wrap in fsspec, undo deps bump
mroeschke 7703f0f
Fix whatsnew
mroeschke e99d234
Add validations for filesystem
mroeschke f4ef416
Validate that mock filesystem is used
mroeschke 3f0e751
Undo install.rst
mroeschke 970a08f
Try this
mroeschke d79f29f
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke 29bce3b
Make global again?
mroeschke 440eeac
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke 0017ae5
Try this
mroeschke 4d3aff9
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke f6ac7ca
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke 31593f0
Address review
mroeschke 7f5dad3
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke 646dbad
Fix test
mroeschke 70f1e6c
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke 96ef2fb
Use localfilesystem correctly
mroeschke 42e614d
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke ae77a26
use absolute
mroeschke 91c9b93
Merge remote-tracking branch 'upstream/main' into enh/parquet/arrow_fs
mroeschke 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,12 +88,39 @@ def _get_path_or_handle( | |
]: | ||
"""File handling for PyArrow.""" | ||
path_or_handle = stringify_path(path) | ||
if fs is not None: | ||
pa_fs = import_optional_dependency("pyarrow.fs", errors="ignore") | ||
fsspec = import_optional_dependency("fsspec", errors="ignore") | ||
if pa_fs is None and fsspec is None: | ||
raise ValueError( | ||
f"filesystem must be a pyarrow or fsspec FileSystem, " | ||
f"not a {type(fs).__name__}" | ||
) | ||
elif (pa_fs is not None and not isinstance(fs, pa_fs.FileSystem)) and ( | ||
fsspec is not None and not isinstance(fs, fsspec.spec.AbstractFileSystem) | ||
): | ||
raise ValueError( | ||
f"filesystem must be a pyarrow or fsspec FileSystem, " | ||
f"not a {type(fs).__name__}" | ||
) | ||
elif pa_fs is not None and isinstance(fs, pa_fs.FileSystem) and storage_options: | ||
raise NotImplementedError( | ||
"storage_options not supported with a pyarrow Filesystem." | ||
) | ||
if is_fsspec_url(path_or_handle) and fs is None: | ||
fsspec = import_optional_dependency("fsspec") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could move this import a bit lower to where fsspec is used? (so we don't try to import it if we already have a pyarrow filesystem) |
||
if storage_options is None: | ||
pa = import_optional_dependency("pyarrow") | ||
pa_fs = import_optional_dependency("pyarrow.fs") | ||
|
||
fs, path_or_handle = fsspec.core.url_to_fs( | ||
path_or_handle, **(storage_options or {}) | ||
) | ||
try: | ||
fs, path_or_handle = pa_fs.FileSystem.from_uri(path) | ||
except (TypeError, pa.ArrowInvalid): | ||
pass | ||
if fs is None: | ||
fs, path_or_handle = fsspec.core.url_to_fs( | ||
path_or_handle, **(storage_options or {}) | ||
) | ||
elif storage_options and (not is_url(path_or_handle) or mode != "rb"): | ||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# can't write to a remote url | ||
# without making use of fsspec at the moment | ||
|
@@ -171,6 +198,7 @@ def write( | |
index: bool | None = None, | ||
storage_options: StorageOptions = None, | ||
partition_cols: list[str] | None = None, | ||
filesystem=None, | ||
**kwargs, | ||
) -> None: | ||
self.validate_dataframe(df) | ||
|
@@ -181,9 +209,9 @@ def write( | |
|
||
table = self.api.Table.from_pandas(df, **from_pandas_kwargs) | ||
|
||
path_or_handle, handles, kwargs["filesystem"] = _get_path_or_handle( | ||
path_or_handle, handles, filesystem = _get_path_or_handle( | ||
path, | ||
kwargs.pop("filesystem", None), | ||
filesystem, | ||
storage_options=storage_options, | ||
mode="wb", | ||
is_dir=partition_cols is not None, | ||
|
@@ -205,12 +233,17 @@ def write( | |
path_or_handle, | ||
compression=compression, | ||
partition_cols=partition_cols, | ||
filesystem=filesystem, | ||
**kwargs, | ||
) | ||
else: | ||
# write to single output file | ||
self.api.parquet.write_table( | ||
table, path_or_handle, compression=compression, **kwargs | ||
table, | ||
path_or_handle, | ||
compression=compression, | ||
filesystem=filesystem, | ||
**kwargs, | ||
) | ||
finally: | ||
if handles is not None: | ||
|
@@ -222,6 +255,7 @@ def read( | |
columns=None, | ||
use_nullable_dtypes: bool = False, | ||
storage_options: StorageOptions = None, | ||
filesystem=None, | ||
**kwargs, | ||
) -> DataFrame: | ||
kwargs["use_pandas_metadata"] = True | ||
|
@@ -239,15 +273,15 @@ def read( | |
if manager == "array": | ||
to_pandas_kwargs["split_blocks"] = True # type: ignore[assignment] | ||
|
||
path_or_handle, handles, kwargs["filesystem"] = _get_path_or_handle( | ||
path_or_handle, handles, filesystem = _get_path_or_handle( | ||
path, | ||
kwargs.pop("filesystem", None), | ||
filesystem, | ||
storage_options=storage_options, | ||
mode="rb", | ||
) | ||
try: | ||
pa_table = self.api.parquet.read_table( | ||
path_or_handle, columns=columns, **kwargs | ||
path_or_handle, columns=columns, filesystem=filesystem, **kwargs | ||
) | ||
if dtype_backend == "pandas": | ||
result = pa_table.to_pandas(**to_pandas_kwargs) | ||
|
@@ -282,6 +316,7 @@ def write( | |
index=None, | ||
partition_cols=None, | ||
storage_options: StorageOptions = None, | ||
filesystem=None, | ||
**kwargs, | ||
) -> None: | ||
self.validate_dataframe(df) | ||
|
@@ -297,6 +332,11 @@ def write( | |
if partition_cols is not None: | ||
kwargs["file_scheme"] = "hive" | ||
|
||
if filesystem is not None: | ||
raise NotImplementedError( | ||
"filesystem is not implemented for the fastparquet engine." | ||
) | ||
|
||
# cannot use get_handle as write() does not accept file buffers | ||
path = stringify_path(path) | ||
if is_fsspec_url(path): | ||
|
@@ -322,7 +362,12 @@ def write( | |
) | ||
|
||
def read( | ||
self, path, columns=None, storage_options: StorageOptions = None, **kwargs | ||
self, | ||
path, | ||
columns=None, | ||
storage_options: StorageOptions = None, | ||
filesystem=None, | ||
**kwargs, | ||
) -> DataFrame: | ||
parquet_kwargs: dict[str, Any] = {} | ||
use_nullable_dtypes = kwargs.pop("use_nullable_dtypes", False) | ||
|
@@ -334,6 +379,10 @@ def read( | |
"The 'use_nullable_dtypes' argument is not supported for the " | ||
"fastparquet engine" | ||
) | ||
if filesystem is not None: | ||
raise NotImplementedError( | ||
"filesystem is not implemented for the fastparquet engine." | ||
) | ||
path = stringify_path(path) | ||
handles = None | ||
if is_fsspec_url(path): | ||
|
@@ -373,6 +422,7 @@ def to_parquet( | |
index: bool | None = None, | ||
storage_options: StorageOptions = None, | ||
partition_cols: list[str] | None = None, | ||
filesystem: Any = None, | ||
**kwargs, | ||
) -> bytes | None: | ||
""" | ||
|
@@ -395,6 +445,12 @@ def to_parquet( | |
``io.parquet.engine`` is used. The default ``io.parquet.engine`` | ||
behavior is to try 'pyarrow', falling back to 'fastparquet' if | ||
'pyarrow' is unavailable. | ||
|
||
When using the ``'pyarrow'`` engine and no storage options are provided | ||
and a filesystem is implemented by both ``pyarrow.fs`` and ``fsspec`` | ||
(e.g. "s3://"), then the ``pyarrow.fs`` filesystem is attempted first. | ||
Use the filesystem keyword with an instantiated fsspec filesystem | ||
if you wish to use its implementation. | ||
compression : {{'snappy', 'gzip', 'brotli', 'lz4', 'zstd', None}}, | ||
default 'snappy'. Name of the compression to use. Use ``None`` | ||
for no compression. The supported compression methods actually | ||
|
@@ -417,6 +473,12 @@ def to_parquet( | |
|
||
.. versionadded:: 1.2.0 | ||
|
||
filesystem : fsspec or pyarrow filesystem, default None | ||
Filesystem object to use when reading the parquet file. Only implemented | ||
for ``engine="pyarrow"``. | ||
|
||
.. versionadded:: 2.1.0 | ||
|
||
kwargs | ||
Additional keyword arguments passed to the engine | ||
|
||
|
@@ -437,6 +499,7 @@ def to_parquet( | |
index=index, | ||
partition_cols=partition_cols, | ||
storage_options=storage_options, | ||
filesystem=filesystem, | ||
**kwargs, | ||
) | ||
|
||
|
@@ -454,6 +517,7 @@ def read_parquet( | |
columns: list[str] | None = None, | ||
storage_options: StorageOptions = None, | ||
use_nullable_dtypes: bool | lib.NoDefault = lib.no_default, | ||
filesystem: Any = None, | ||
**kwargs, | ||
) -> DataFrame: | ||
""" | ||
|
@@ -476,6 +540,12 @@ def read_parquet( | |
``io.parquet.engine`` is used. The default ``io.parquet.engine`` | ||
behavior is to try 'pyarrow', falling back to 'fastparquet' if | ||
'pyarrow' is unavailable. | ||
|
||
When using the ``'pyarrow'`` engine and no storage options are provided | ||
and a filesystem is implemented by both ``pyarrow.fs`` and ``fsspec`` | ||
(e.g. "s3://"), then the ``pyarrow.fs`` filesystem is attempted first. | ||
Use the filesystem keyword with an instantiated fsspec filesystem | ||
if you wish to use its implementation. | ||
columns : list, default=None | ||
If not None, only these columns will be read from the file. | ||
|
||
|
@@ -504,6 +574,12 @@ def read_parquet( | |
|
||
.. versionadded:: 2.0.0 | ||
|
||
filesystem : fsspec or pyarrow filesystem, default None | ||
Filesystem object to use when reading the parquet file. Only implemented | ||
for ``engine="pyarrow"``. | ||
|
||
.. versionadded:: 2.1.0 | ||
|
||
**kwargs | ||
Any additional kwargs are passed to the engine. | ||
|
||
|
@@ -524,5 +600,6 @@ def read_parquet( | |
columns=columns, | ||
storage_options=storage_options, | ||
use_nullable_dtypes=use_nullable_dtypes, | ||
filesystem=filesystem, | ||
**kwargs, | ||
) |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.