-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
[WIP] Add remote file io using fsspec. #33549
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,6 +158,23 @@ def urlopen(*args, **kwargs): | |
return urllib.request.urlopen(*args, **kwargs) | ||
|
||
|
||
def is_fsspec_url(url) -> bool: | ||
""" | ||
Returns true if fsspec is installed and the URL references a known | ||
fsspec filesystem. | ||
""" | ||
|
||
if not isinstance(url, str): | ||
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 don’t need this |
||
return False | ||
|
||
try: | ||
from fsspec.registry import known_implementations | ||
scheme = parse_url(url).scheme | ||
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. Perhaps |
||
return scheme != "file" and scheme in known_implementations | ||
except ImportError: | ||
return False | ||
|
||
|
||
def get_filepath_or_buffer( | ||
filepath_or_buffer: FilePathOrBuffer, | ||
encoding: Optional[str] = None, | ||
|
@@ -194,19 +211,26 @@ def get_filepath_or_buffer( | |
req.close() | ||
return reader, encoding, compression, True | ||
|
||
if is_s3_url(filepath_or_buffer): | ||
from pandas.io import s3 | ||
if is_fsspec_url(filepath_or_buffer): | ||
import 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. this can be |
||
scheme = parse_url(filepath_or_buffer).scheme | ||
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. These three lines can be done with |
||
filesystem = fsspec.filesystem(scheme) | ||
file_obj = filesystem.open(filepath_or_buffer, mode=mode or "rb") | ||
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. Do we need to pass through the encoding? to .open() You will potentially also fix this if you do so: #26124 :) 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. If using |
||
return file_obj, encoding, compression, True | ||
|
||
return s3.get_filepath_or_buffer( | ||
filepath_or_buffer, encoding=encoding, compression=compression, mode=mode | ||
) | ||
# if is_s3_url(filepath_or_buffer): | ||
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 can delete all these. |
||
# from pandas.io import s3 | ||
|
||
if is_gcs_url(filepath_or_buffer): | ||
from pandas.io import gcs | ||
# return s3.get_filepath_or_buffer( | ||
# filepath_or_buffer, encoding=encoding, compression=compression, mode=mode | ||
# ) | ||
|
||
return gcs.get_filepath_or_buffer( | ||
filepath_or_buffer, encoding=encoding, compression=compression, mode=mode | ||
) | ||
# if is_gcs_url(filepath_or_buffer): | ||
# from pandas.io import gcs | ||
|
||
# return gcs.get_filepath_or_buffer( | ||
# filepath_or_buffer, encoding=encoding, compression=compression, mode=mode | ||
# ) | ||
|
||
if isinstance(filepath_or_buffer, (str, bytes, mmap.mmap)): | ||
return _expand_user(filepath_or_buffer), None, compression, False | ||
|
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.
can u type url