Skip to content

Commit 94ecdb5

Browse files
committed
add missing module
1 parent 1ae5e63 commit 94ecdb5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/zarr/strings.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import Any
2+
from warnings import warn
3+
4+
import numpy as np
5+
6+
try:
7+
STRING_DTYPE = np.dtype("T")
8+
NUMPY_SUPPORTS_VLEN_STRING = True
9+
except TypeError:
10+
STRING_DTYPE = np.dtype("object")
11+
NUMPY_SUPPORTS_VLEN_STRING = False
12+
13+
14+
def cast_to_string_dtype(
15+
data: np.ndarray[Any, np.dtype[Any]], safe: bool = False
16+
) -> np.ndarray[Any, np.dtype[Any]]:
17+
if np.issubdtype(data.dtype, np.str_):
18+
return data
19+
if np.issubdtype(data.dtype, np.object_):
20+
if NUMPY_SUPPORTS_VLEN_STRING:
21+
try:
22+
# cast to variable-length string dtype, fail if object contains non-string data
23+
# mypy says "error: Unexpected keyword argument "coerce" for "StringDType" [call-arg]"
24+
return data.astype(np.dtypes.StringDType(coerce=False), copy=False) # type: ignore[call-arg]
25+
except ValueError as e:
26+
raise ValueError("Cannot cast object dtype to string dtype") from e
27+
else:
28+
out = data.astype(np.str_)
29+
if not safe:
30+
warn(
31+
f"Casted object dtype to string dtype {out.dtype}. To avoid this warning, "
32+
"cast the data to a string dtype before passing to Zarr or upgrade to NumPy >= 2.",
33+
stacklevel=2,
34+
)
35+
return out
36+
raise ValueError(f"Cannot cast dtype {data.dtype} to string dtype")

0 commit comments

Comments
 (0)