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

IO support for pandas datetime format #1708

Open
2 of 3 tasks
le-ander opened this issue Oct 9, 2024 · 1 comment
Open
2 of 3 tasks

IO support for pandas datetime format #1708

le-ander opened this issue Oct 9, 2024 · 1 comment

Comments

@le-ander
Copy link

le-ander commented Oct 9, 2024

Please make sure these conditions are met

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of anndata.
  • (optional) I have confirmed this bug exists on the master branch of anndata.

Report

Code:

adata.write("file.h5ad")

Traceback:

---------------------------------------------------------------------------
IORegistryError                           Traceback (most recent call last)
Cell In[13], line 1
----> 1 adata.write("data[/microbiome_layered.h5ad](http://localhost:8888/microbiome_layered.h5ad)")

File [~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_core/anndata.py:1863](http://localhost:8888/lab/tree/work/projects/hackathon_ehrapy/~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_core/anndata.py#line=1862), in AnnData.write_h5ad(self, filename, compression, compression_opts, as_dense)
   1860 if filename is None:
   1861     filename = self.filename
-> 1863 write_h5ad(
   1864     Path(filename),
   1865     self,
   1866     compression=compression,
   1867     compression_opts=compression_opts,
   1868     as_dense=as_dense,
   1869 )
   1871 if self.isbacked:
   1872     self.file.filename = filename

File [~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/h5ad.py:101](http://localhost:8888/lab/tree/work/projects/hackathon_ehrapy/~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/h5ad.py#line=100), in write_h5ad(filepath, adata, as_dense, dataset_kwargs, **kwargs)
     99 elif adata.raw is not None:
    100     write_elem(f, "raw", adata.raw, dataset_kwargs=dataset_kwargs)
--> 101 write_elem(f, "obs", adata.obs, dataset_kwargs=dataset_kwargs)
    102 write_elem(f, "var", adata.var, dataset_kwargs=dataset_kwargs)
    103 write_elem(f, "obsm", dict(adata.obsm), dataset_kwargs=dataset_kwargs)

File [~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/specs/registry.py:378](http://localhost:8888/lab/tree/work/projects/hackathon_ehrapy/~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/specs/registry.py#line=377), in write_elem(store, k, elem, dataset_kwargs)
    354 def write_elem(
    355     store: GroupStorageType,
    356     k: str,
   (...)
    359     dataset_kwargs: Mapping[str, Any] = MappingProxyType({}),
    360 ) -> None:
    361     """
    362     Write an element to a storage group using anndata encoding.
    363 
   (...)
    376         E.g. for zarr this would be `chunks`, `compressor`.
    377     """
--> 378     Writer(_REGISTRY).write_elem(store, k, elem, dataset_kwargs=dataset_kwargs)

File [~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/utils.py:247](http://localhost:8888/lab/tree/work/projects/hackathon_ehrapy/~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/utils.py#line=246), in report_write_key_on_error.<locals>.func_wrapper(*args, **kwargs)
    245     raise ValueError("No element found in args.")
    246 try:
--> 247     return func(*args, **kwargs)
    248 except Exception as e:
    249     path = _get_display_path(store)

File [~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/specs/registry.py:328](http://localhost:8888/lab/tree/work/projects/hackathon_ehrapy/~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/specs/registry.py#line=327), in Writer.write_elem(self, store, k, elem, dataset_kwargs, modifiers)
    325 write_func = self.find_write_func(dest_type, elem, modifiers)
    327 if self.callback is None:
--> 328     return write_func(store, k, elem, dataset_kwargs=dataset_kwargs)
    329 return self.callback(
    330     write_func,
    331     store,
   (...)
    335     iospec=self.registry.get_spec(elem),
    336 )

File [~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/specs/registry.py:71](http://localhost:8888/lab/tree/work/projects/hackathon_ehrapy/~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/specs/registry.py#line=70), in write_spec.<locals>.decorator.<locals>.wrapper(g, k, *args, **kwargs)
     69 @wraps(func)
     70 def wrapper(g: GroupStorageType, k: str, *args, **kwargs):
---> 71     result = func(g, k, *args, **kwargs)
     72     g[k].attrs.setdefault("encoding-type", spec.encoding_type)
     73     g[k].attrs.setdefault("encoding-version", spec.encoding_version)

File [~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/specs/methods.py:830](http://localhost:8888/lab/tree/work/projects/hackathon_ehrapy/~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/specs/methods.py#line=829), in write_dataframe(f, key, df, _writer, dataset_kwargs)
    825 _writer.write_elem(
    826     group, index_name, df.index._values, dataset_kwargs=dataset_kwargs
    827 )
    828 for colname, series in df.items():
    829     # TODO: this should write the "true" representation of the series (i.e. the underlying array or ndarray depending)
--> 830     _writer.write_elem(
    831         group, colname, series._values, dataset_kwargs=dataset_kwargs
    832     )

File [~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/utils.py:247](http://localhost:8888/lab/tree/work/projects/hackathon_ehrapy/~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/utils.py#line=246), in report_write_key_on_error.<locals>.func_wrapper(*args, **kwargs)
    245     raise ValueError("No element found in args.")
    246 try:
--> 247     return func(*args, **kwargs)
    248 except Exception as e:
    249     path = _get_display_path(store)

File [~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/specs/registry.py:325](http://localhost:8888/lab/tree/work/projects/hackathon_ehrapy/~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/specs/registry.py#line=324), in Writer.write_elem(self, store, k, elem, dataset_kwargs, modifiers)
    322 elif k in store:
    323     del store[k]
--> 325 write_func = self.find_write_func(dest_type, elem, modifiers)
    327 if self.callback is None:
    328     return write_func(store, k, elem, dataset_kwargs=dataset_kwargs)

File [~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/specs/registry.py:292](http://localhost:8888/lab/tree/work/projects/hackathon_ehrapy/~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/specs/registry.py#line=291), in Writer.find_write_func(self, dest_type, elem, modifiers)
    288         return self.registry.get_write(
    289             dest_type, pattern, modifiers, writer=self
    290         )
    291 # Raises IORegistryError
--> 292 return self.registry.get_write(dest_type, type(elem), modifiers, writer=self)

File [~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/specs/registry.py:131](http://localhost:8888/lab/tree/work/projects/hackathon_ehrapy/~/.pyenv/versions/3.12.3/envs/ehrapy-3.12.3/lib/python3.12/site-packages/anndata/_io/specs/registry.py#line=130), in IORegistry.get_write(self, dest_type, src_type, modifiers, writer)
    128     dest_type = h5py.Group
    130 if (dest_type, src_type, modifiers) not in self.write:
--> 131     raise IORegistryError._from_write_parts(dest_type, src_type, modifiers)
    132 internal = self.write[(dest_type, src_type, modifiers)]
    133 return partial(internal, _writer=writer)

IORegistryError: No method registered for writing <class 'pandas.core.arrays.datetimes.DatetimeArray'> into <class 'h5py._hl.group.Group'>
Error raised while writing key 'rounded_birthdate' of <class 'h5py._hl.group.Group'> to /obs

Versions

anndata             0.10.9
session_info        1.0.0
-----
cython_runtime      NA
dateutil            2.9.0.post0
h5py                3.12.1
natsort             8.4.0
numpy               1.26.4
packaging           24.1
pandas              2.2.3
pyarrow             17.0.0
pytz                2024.2
scipy               1.14.1
six                 1.16.0
-----
Python 3.12.3 (main, Oct  7 2024, 17:13:37) [Clang 14.0.0 (clang-1400.0.29.202)]
macOS-12.7.6-x86_64-i386-64bit
-----
Session information updated at 2024-10-09 12:10
@flying-sheep
Copy link
Member

Comment by @ilan-gold: xarray probably has something here

@ilan-gold ilan-gold self-assigned this Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants