Skip to content

Consider supporting pathlib.Path within Dask Arrays .to_zarr(...) method #8006

Closed

Description

@asmith26

Not sure if this should be raised in maybe the zarr repo, and I've looked through previous issues and couldn't find anything regarding this - it's feels like the Dask Array .to_zarr(...) method could support pathlib.Path (directly, i.e. without having to turn it into a str):

Dask Version: 2021.7.2

>>> import pathlib, dask.array as da,  numpy as np
>>> da.from_array(np.array([1])).to_zarr("file.zarr")  # works
>>> da.from_array(np.array([1])).to_zarr(pathlib.Path("file.zarr"))  # yields:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File ".../lib/python3.8/site-packages/dask/array/core.py", line 2649, in to_zarr
    return to_zarr(self, *args, **kwargs)
  File ".../lib/python3.8/site-packages/dask/array/core.py", line 3381, in to_zarr
    z = zarr.create(
  File ".../lib/python3.8/site-packages/zarr/creation.py", line 136, in create
    init_array(store, shape=shape, chunks=chunks, dtype=dtype, compressor=compressor,
  File ".../lib/python3.8/site-packages/zarr/storage.py", line 352, in init_array
    _init_array_metadata(store, shape=shape, chunks=chunks, dtype=dtype,
  File ".../lib/python3.8/site-packages/zarr/storage.py", line 382, in _init_array_metadata
    elif contains_array(store, path):
  File ".../lib/python3.8/site-packages/zarr/storage.py", line 96, in contains_array
    return key in store
TypeError: argument of type 'PosixPath' is not iterable

In particular I've noticed that Dask DataFrame .to_parquet(...) supports this:

>>> import pathlib, dask.dataframe as dd, pandas as pd
>>> dd.from_pandas(pd.DataFrame({"a": [1]}), npartitions=1).to_parquet(pathlib.Path("file.parquet"))  # works

Many thanks for these awesome libs!

Activity

  1. jrbourbeau commented on Aug 10, 2021

    @jrbourbeau
    Member

    Thanks for raising an issue @asmith26! The error you're running into looks similar to zarr-developers/zarr-python#601. It looks like zarr itself doesn't have full support for pathlib.Path objects yet, but that it is currently in the works (xref zarr-developers/zarr-python#768). I agree it would be reasonable for Dask to support them

    cc @martindurant

  2. martindurant commented on Aug 10, 2021

    @martindurant
    Member

    fsspec handles Path objects in a few places (e.g., fsspec.open), but not generally, because there is no consensus on how to represent non-local paths. I think zarr will defer to fsspec only if the path is URL-like (containing "://" or "::" - but probably not knowing to convert to string first), and fsspec.get_mapper does not support Path.

    Do you think a Path is implicitly local?

  3. asmith26 commented on Aug 11, 2021

    @asmith26
    ContributorAuthor

    Thanks for all the answers.

    Do you think a Path is implicitly local?

    That's a good question - I had a little Google and would consider pathlib.Path to be intended only for local paths. In particular:

    That said, I'm certainly no expert on pathlib.Path, so all thoughts/corrections very welcome.

  4. martindurant commented on Aug 11, 2021

    @martindurant
    Member

    I encourage you to comment at drivendataorg/cloudpathlib#96 to push support for pathlib with fsspec. That would solve this kind of issue, without dask's explicit involvement, for local and remote paths both.

  5. clbarnes commented on Aug 17, 2021

    @clbarnes

    It looks like zarr itself doesn't have full support for pathlib.Path objects yet, but that it is currently in the works (xref zarr-developers/zarr-python#768)

    That PR is now merged; zarr will accept pathlib.Path in its convenience methods and store constructors (although it does so by normalising to str) from the next release.

  6. clbarnes commented on Sep 1, 2021

    @clbarnes
  7. jsignell commented on Sep 1, 2021

    @jsignell
    Member

    I just reran the original reproducer with zarr=2.9.0 and it worked as expected, so I think we can safely close this.

  8. jamestwebber commented on Sep 21, 2021

    @jamestwebber

    I could create a new issue it's so close to this one: while to_zarr works as expected, from_zarr still raises the error mentioned in the original post. It'd be great if it worked both ways!

    Just tested with zarr=2.10 and dask=2021.09.0

  9. jrbourbeau commented on Sep 21, 2021

    @jrbourbeau
    Member

    A new issue, or a PR, for from_zarr would be very welcome : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions