Skip to content

Commit 79bfa3b

Browse files
authored
Merge pull request #72 from ngoldbaum/add-more-casts
Add round-trip casts between string and (u)int dtypes
2 parents b9c9cac + f0393e5 commit 79bfa3b

File tree

8 files changed

+477
-14
lines changed

8 files changed

+477
-14
lines changed

asciidtype/asciidtype/src/asciidtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ PyInit__asciidtype_main(void)
2222
return NULL;
2323
}
2424

25-
if (import_experimental_dtype_api(11) < 0) {
25+
if (import_experimental_dtype_api(12) < 0) {
2626
return NULL;
2727
}
2828

metadatadtype/metadatadtype/src/metadatadtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PyInit__metadatadtype_main(void)
2121
if (_import_array() < 0) {
2222
return NULL;
2323
}
24-
if (import_experimental_dtype_api(11) < 0) {
24+
if (import_experimental_dtype_api(12) < 0) {
2525
return NULL;
2626
}
2727

mpfdtype/mpfdtype/src/mpfdtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ PyInit__mpfdtype_main(void)
2222
if (_import_array() < 0) {
2323
return NULL;
2424
}
25-
if (import_experimental_dtype_api(11) < 0) {
25+
if (import_experimental_dtype_api(12) < 0) {
2626
return NULL;
2727
}
2828

quaddtype/quaddtype/src/quaddtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ PyInit__quaddtype_main(void)
2323
return NULL;
2424

2525
// Fail to init if the experimental DType API version 5 isn't supported
26-
if (import_experimental_dtype_api(11) < 0) {
26+
if (import_experimental_dtype_api(12) < 0) {
2727
PyErr_SetString(PyExc_ImportError,
2828
"Error encountered importing the experimental dtype API.");
2929
return NULL;

stringdtype/stringdtype/src/casts.c

Lines changed: 430 additions & 7 deletions
Large diffs are not rendered by default.

stringdtype/stringdtype/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ PyInit__main(void)
9292
if (_import_array() < 0) {
9393
return NULL;
9494
}
95-
if (import_experimental_dtype_api(11) < 0) {
95+
if (import_experimental_dtype_api(12) < 0) {
9696
return NULL;
9797
}
9898

stringdtype/tests/test_stringdtype.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,54 @@ def test_arrfuncs_zeros(dtype, arrfunc, expected):
350350
[["", "world"], [False, True], True, False],
351351
],
352352
)
353-
def test_bool_cast(dtype, strings, cast_answer, any_answer, all_answer):
353+
def test_cast_to_bool(dtype, strings, cast_answer, any_answer, all_answer):
354354
sarr = np.array(strings, dtype=dtype)
355355
np.testing.assert_array_equal(sarr.astype("bool"), cast_answer)
356356

357357
assert np.any(sarr) == any_answer
358358
assert np.all(sarr) == all_answer
359359

360360

361+
@pytest.mark.parametrize(
362+
("strings", "cast_answer"),
363+
[
364+
[[True, True], ["True", "True"]],
365+
[[False, False], ["False", "False"]],
366+
[[True, False], ["True", "False"]],
367+
[[False, True], ["False", "True"]],
368+
],
369+
)
370+
def test_cast_from_bool(dtype, strings, cast_answer):
371+
barr = np.array(strings, dtype=bool)
372+
np.testing.assert_array_equal(
373+
barr.astype(dtype), np.array(cast_answer, dtype=dtype)
374+
)
375+
376+
377+
@pytest.mark.parametrize("bitsize", [8, 16, 32, 64])
378+
@pytest.mark.parametrize("signed", [True, False])
379+
def test_integer_casts(dtype, bitsize, signed):
380+
idtype = f"int{bitsize}"
381+
if signed:
382+
inp = [-(2**p - 1) for p in reversed(range(bitsize - 1))]
383+
inp += [2**p - 1 for p in range(1, bitsize - 1)]
384+
else:
385+
idtype = "u" + idtype
386+
inp = [2**p - 1 for p in range(bitsize)]
387+
ainp = np.array(inp, dtype=idtype)
388+
np.testing.assert_array_equal(ainp, ainp.astype(dtype).astype(idtype))
389+
390+
with pytest.raises(TypeError):
391+
ainp.astype(dtype, casting="safe")
392+
393+
with pytest.raises(TypeError):
394+
ainp.astype(dtype).astype(idtype, casting="safe")
395+
396+
oob = [str(2**bitsize), str(-(2**bitsize))]
397+
with pytest.raises(OverflowError):
398+
np.array(oob, dtype=dtype).astype(idtype)
399+
400+
361401
def test_take(dtype, string_list):
362402
sarr = np.array(string_list, dtype=dtype)
363403
out = np.empty(len(string_list), dtype=dtype)

unytdtype/unytdtype/src/unytdtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PyInit__unytdtype_main(void)
2121
if (_import_array() < 0) {
2222
return NULL;
2323
}
24-
if (import_experimental_dtype_api(11) < 0) {
24+
if (import_experimental_dtype_api(12) < 0) {
2525
return NULL;
2626
}
2727

0 commit comments

Comments
 (0)