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

ENH: Add storage_options to read_parquet #2107

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 41 additions & 28 deletions geopandas/io/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,44 @@ def _arrow_to_geopandas(table):
return GeoDataFrame(df, geometry=geometry)


def _get_filesystem_path(path, filesystem=None, storage_options=None):
"""
Get the filesystem and path for a given filesystem and path.

If the filesystem is not None then it's just returned as is.
"""
import pyarrow

if (
isinstance(path, str)
and storage_options is None
and filesystem is None
and LooseVersion(pyarrow.__version__) >= "5.0.0"
):
# Use the native pyarrow filesystem if possible.
try:
from pyarrow.fs import FileSystem

filesystem, path = FileSystem.from_uri(path)
except Exception:
# fallback to use get_handle / fsspec for filesystems
# that pyarrow doesn't support
pass

if _is_fsspec_url(path) and filesystem is None:
fsspec = import_optional_dependency(
"fsspec", extra="fsspec is requred for 'storage_options'."
)
filesystem, path = fsspec.core.url_to_fs(path, **(storage_options or {}))

if filesystem is None and storage_options:
raise ValueError(
"Cannot provide 'storage_options' with non-fsspec path '{}'".format(path)
)

return filesystem, path


def _read_parquet(path, columns=None, storage_options=None, **kwargs):
"""
Load a Parquet object from the file path, returning a GeoDataFrame.
Expand Down Expand Up @@ -410,34 +448,9 @@ def _read_parquet(path, columns=None, storage_options=None, **kwargs):
# TODO(https://github.com/pandas-dev/pandas/pull/41194): see if pandas
# adds filesystem as a keyword and match that.
filesystem = kwargs.pop("filesystem", None)
import pyarrow

if (
isinstance(path, str)
and storage_options is None
and filesystem is None
and LooseVersion(pyarrow.__version__) >= "5.0.0"
):
# Use the native pyarrow filesystem if possible.
try:
from pyarrow.fs import FileSystem

filesystem, path = FileSystem.from_uri(path)
except Exception:
# fallback to use get_handle / fsspec for filesystems
# that pyarrow doesn't support
pass

if _is_fsspec_url(path) and filesystem is None:
fsspec = import_optional_dependency(
"fsspec", extra="fsspec is requred for 'storage_options'."
)
filesystem, path = fsspec.core.url_to_fs(path, **(storage_options or {}))

if filesystem is None and storage_options:
raise ValueError(
"Cannot provide 'storage_options' with non-fsspec path '{}'".format(path)
)
filesystem, path = _get_filesystem_path(
path, filesystem=filesystem, storage_options=storage_options
)

kwargs["use_pandas_metadata"] = True
table = parquet.read_table(path, columns=columns, filesystem=filesystem, **kwargs)
Expand Down
10 changes: 10 additions & 0 deletions geopandas/io/tests/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
_create_metadata,
_decode_metadata,
_encode_metadata,
_get_filesystem_path,
_validate_dataframe,
_validate_metadata,
METADATA_VERSION,
Expand Down Expand Up @@ -531,3 +532,12 @@ def test_non_fsspec_url_with_storage_options_raises():
with pytest.raises(ValueError, match="storage_options"):
test_dataset = "naturalearth_lowres"
read_parquet(get_path(test_dataset), storage_options={"foo": "bar"})


@pytest.mark.skipif(
pyarrow.__version__ < LooseVersion("5.0.0"),
reason="pyarrow.fs requires pyarrow>=5.0.0",
jorisvandenbossche marked this conversation as resolved.
Show resolved Hide resolved
)
def test_prefers_pyarrow_fs():
filesystem, _ = _get_filesystem_path("file:///data.parquet")
assert isinstance(filesystem, pyarrow.fs.LocalFileSystem)