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

expanduser implementation for geopandas #292

Closed
ollie-bell opened this issue Oct 2, 2024 · 2 comments · Fixed by #293
Closed

expanduser implementation for geopandas #292

ollie-bell opened this issue Oct 2, 2024 · 2 comments · Fixed by #293
Labels
bug 🐛 Something isn't working

Comments

@ollie-bell
Copy link
Contributor

ollie-bell commented Oct 2, 2024

GeoPandas read_file and to_file attempts a path.expanduser() which raises NotImplementedError when path is a UPath.

Is it intended that UPath.expanduser must raise NotImplementedError, or is it a matter of not getting round to it yet? For context I have had this issue when attempting to read/write on GCS.

In the meantime I am using a workaround to read/write via a io.BytesIO buffer, e.g.:

# write
with io.BytesIO() as buffer:
  gdf.to_file(buffer, driver="GeoJSON")
  buffer.seek(0)
  path.write_bytes(buffer.read())

# read
with io.BytesIO(path.read_bytes()) as buffer:
  gdf = gpd.read_file(buffer)
@ap--
Copy link
Collaborator

ap-- commented Oct 3, 2024

Hi @ollie-bell

Making expanduser a noop is worth considering. Would you be interested in making a PR?

  1. You would have to change the base test here:
    def test_expanduser(self):
    with pytest.raises(NotImplementedError):
    self.path.expanduser()
  2. And then change the default implementation to just return self.

Regarding the code example you provided as a workaround for now, is there any reason why the following does not work?

pth = UPath(...)

# write
with pth.open(mode="wb") as buffer:
  gdf.to_file(buffer, driver="GeoJSON")

# read
with pth.open(mode="rb") as buffer:
  gdf = gpd.read_file(buffer)

Cheers,
Andreas 😃

@ap-- ap-- added the bug 🐛 Something isn't working label Oct 3, 2024
@ollie-bell
Copy link
Contributor Author

ollie-bell commented Oct 3, 2024

Yeh sure I can look at doing a PR for expanduser!

On the second part, actually yes the read does work! The write does not though, pyogrio (GeoPandas default io backend) gives an error:

NotImplementedError: writing to an open file handle is not yet supported; instead, write to a BytesIO instance and then read bytes from that to write to the file handle

... in fact they suggest doing what I did for writing:

writing the dataset to BytesIO and then writing those bytes to the file handle (regular file handle via open, an fsspec opened file handle, or an open handle within a ZipFile),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants