5454 from os import PathLike
5555 from typing import Any , Literal
5656
57+ from zarr .storage import StoreLike
58+
5759 from ..compat import Index1D
5860 from ..typing import ArrayDataStructureType
5961 from .aligned_mapping import AxisArraysView , LayersView , PairwiseArraysView
@@ -205,7 +207,7 @@ def __init__(
205207 raw : Mapping [str , Any ] | None = None ,
206208 dtype : np .dtype | type | str | None = None ,
207209 shape : tuple [int , int ] | None = None ,
208- filename : PathLike | None = None ,
210+ filename : PathLike [ str ] | str | None = None ,
209211 filemode : Literal ["r" , "r+" ] | None = None ,
210212 asview : bool = False ,
211213 * ,
@@ -947,7 +949,7 @@ def filename(self) -> Path | None:
947949 return self .file .filename
948950
949951 @filename .setter
950- def filename (self , filename : PathLike | None ):
952+ def filename (self , filename : PathLike [ str ] | str | None ):
951953 # convert early for later comparison
952954 filename = None if filename is None else Path (filename )
953955 # change from backing-mode back to full loading into memory
@@ -1423,7 +1425,7 @@ def to_memory(self, copy=False) -> AnnData:
14231425
14241426 return AnnData (** new )
14251427
1426- def copy (self , filename : PathLike | None = None ) -> AnnData :
1428+ def copy (self , filename : PathLike [ str ] | str | None = None ) -> AnnData :
14271429 """Full copy, optionally on disk."""
14281430 if not self .isbacked :
14291431 if self .is_view and self ._has_X ():
@@ -1787,10 +1789,12 @@ def _check_dimensions(self, key=None):
17871789
17881790 def write_h5ad (
17891791 self ,
1790- filename : PathLike | None = None ,
1792+ filename : PathLike [ str ] | str | None = None ,
17911793 compression : Literal ["gzip" , "lzf" ] | None = None ,
17921794 compression_opts : int | Any = None ,
17931795 as_dense : Sequence [str ] = (),
1796+ * ,
1797+ convert_strings_to_categoricals : bool = True ,
17941798 ):
17951799 """\
17961800 Write `.h5ad`-formatted hdf5 file.
@@ -1811,6 +1815,8 @@ def write_h5ad(
18111815 ----------
18121816 filename
18131817 Filename of data file. Defaults to backing file.
1818+ convert_strings_to_categoricals
1819+ Convert string columns to categorical.
18141820 compression
18151821 For [`lzf`, `gzip`], see the h5py :ref:`dataset_compression`.
18161822
@@ -1865,6 +1871,7 @@ def write_h5ad(
18651871 write_h5ad (
18661872 Path (filename ),
18671873 self ,
1874+ convert_strings_to_categoricals = convert_strings_to_categoricals ,
18681875 compression = compression ,
18691876 compression_opts = compression_opts ,
18701877 as_dense = as_dense ,
@@ -1875,7 +1882,9 @@ def write_h5ad(
18751882
18761883 write = write_h5ad # a shortcut and backwards compat
18771884
1878- def write_csvs (self , dirname : PathLike , skip_data : bool = True , sep : str = "," ):
1885+ def write_csvs (
1886+ self , dirname : PathLike [str ] | str , skip_data : bool = True , sep : str = ","
1887+ ):
18791888 """\
18801889 Write annotation to `.csv` files.
18811890
@@ -1895,7 +1904,7 @@ def write_csvs(self, dirname: PathLike, skip_data: bool = True, sep: str = ","):
18951904
18961905 write_csvs (dirname , self , skip_data = skip_data , sep = sep )
18971906
1898- def write_loom (self , filename : PathLike , write_obsm_varm : bool = False ):
1907+ def write_loom (self , filename : PathLike [ str ] | str , write_obsm_varm : bool = False ):
18991908 """\
19001909 Write `.loom`-formatted hdf5 file.
19011910
@@ -1910,8 +1919,10 @@ def write_loom(self, filename: PathLike, write_obsm_varm: bool = False):
19101919
19111920 def write_zarr (
19121921 self ,
1913- store : MutableMapping | PathLike ,
1922+ store : StoreLike ,
19141923 chunks : bool | int | tuple [int , ...] | None = None ,
1924+ * ,
1925+ convert_strings_to_categoricals : bool = True ,
19151926 ):
19161927 """\
19171928 Write a hierarchical Zarr array store.
@@ -1922,10 +1933,17 @@ def write_zarr(
19221933 The filename, a :class:`~typing.MutableMapping`, or a Zarr storage class.
19231934 chunks
19241935 Chunk shape.
1936+ convert_strings_to_categoricals
1937+ Convert string columns to categorical.
19251938 """
19261939 from ..io import write_zarr
19271940
1928- write_zarr (store , self , chunks = chunks )
1941+ write_zarr (
1942+ store ,
1943+ self ,
1944+ chunks = chunks ,
1945+ convert_strings_to_categoricals = convert_strings_to_categoricals ,
1946+ )
19291947
19301948 def chunked_X (self , chunk_size : int | None = None ):
19311949 """\
@@ -2064,10 +2082,10 @@ def _infer_shape_for_axis(
20642082 return elem .shape [0 ]
20652083 for elem , id in zip ([layers , xxxm , xxxp ], ["layers" , "xxxm" , "xxxp" ]):
20662084 if elem is not None :
2067- elem = cast (Mapping , elem )
2085+ elem = cast (" Mapping" , elem )
20682086 for sub_elem in elem .values ():
20692087 if hasattr (sub_elem , "shape" ):
2070- size = cast (int , sub_elem .shape [axis if id == "layers" else 0 ])
2088+ size = cast (" int" , sub_elem .shape [axis if id == "layers" else 0 ])
20712089 return size
20722090 return None
20732091
0 commit comments