8000 BUG: raise error trying to coerce object arrays containing timedelta64('NaT') to StringDType by charris · Pull Request #26049 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: raise error trying to coerce object arrays containing timedelta64('NaT') to StringDType #26049

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

Merged
merged 2 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions numpy/_core/src/multiarray/dtypemeta.c
Original file line number Diff line number Diff line change
Expand Up @@ -838,22 +838,13 @@ python_builtins_are_known_scalar_types(
* This is necessary only for python scalar classes which we discover
* as valid DTypes.
*/
if (pytype == &PyFloat_Type) {
return 1;
}
if (pytype == &PyLong_Type) {
return 1;
}
if (pytype == &PyBool_Type) {
return 1;
}
if (pytype == &PyComplex_Type) {
return 1;
}
if (pytype == &PyUnicode_Type) {
return 1;
}
if (pytype == &PyBytes_Type) {
if (pytype == &PyFloat_Type ||
pytype == &PyLong_Type ||
pytype == &PyBool_Type ||
pytype == &PyComplex_Type ||
pytype == &PyUnicode_Type ||
pytype == &PyBytes_Type)
{
return 1;
}
return 0;
Expand Down
104 changes: 26 additions & 78 deletions numpy/_core/src/multiarray/stringdtype/dtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,88 +554,36 @@ stringdtype_get_clear_loop(void *NPY_UNUSED(traverse_context),
}

static int
stringdtype_is_known_scalar_type(PyArray_DTypeMeta *NPY_UNUSED(cls),
stringdtype_is_known_scalar_type(PyArray_DTypeMeta *cls,
PyTypeObject *pytype)
{
if (pytype == &PyFloat_Type) {
if (python_builtins_are_known_scalar_types(cls, pytype)) {
return 1;
}
if (pytype == &PyLong_Type) {
return 1;
}
if (pytype == &PyBool_Type) {
return 1;
}
if (pytype == &PyComplex_Type) {
return 1;
}
if (pytype == &PyUnicode_Type) {
return 1;
}
if (pytype == &PyBytes_Type) {
return 1;
}
if (pytype == &PyBoolArrType_Type) {
return 1;
}
if (pytype == &PyByteArrType_Type) {
return 1;
}
if (pytype == &PyShortArrType_Type) {
return 1;
}
if (pytype == &PyIntArrType_Type) {
return 1;
}
if (pytype == &PyLongArrType_Type) {
return 1;
}
if (pytype == &PyLongLongArrType_Type) {
return 1;
}
if (pytype == &PyUByteArrType_Type) {
return 1;
}
if (pytype == &PyUShortArrType_Type) {
return 1;
}
if (pytype == &PyUIntArrType_Type) {
return 1;
}
if (pytype == &PyULongArrType_Type) {
return 1;
}
if (pytype == &PyULongLongArrType_Type) {
return 1;
}
if (pytype == &PyHalfArrType_Type) {
return 1;
}
if (pytype == &PyFloatArrType_Type) {
return 1;
}
if (pytype == &PyDoubleArrType_Type) {
return 1;
}
if (pytype == &PyLongDoubleArrType_Type) {
return 1;
}
if (pytype == &PyCFloatArrType_Type) {
return 1;
}
if (pytype == &PyCDoubleArrType_Type) {
return 1;
}
if (pytype == &PyCLongDoubleArrType_Type) {
return 1;
}
if (pytype == &PyIntpArrType_Type) {
return 1;
}
if (pytype == &PyUIntpArrType_Type) {
return 1;
}
if (pytype == &PyDatetimeArrType_Type) {
// accept every built-in numpy dtype
else if (pytype == &PyBoolArrType_Type ||
pytype == &PyByteArrType_Type ||
pytype == &PyShortArrType_Type ||
pytype == &PyIntArrType_Type ||
pytype == &PyLongArrType_Type ||
pytype == &PyLongLongArrType_Type ||
pytype == &PyUByteArrType_Type ||
pytype == &PyUShortArrType_Type ||
pytype == &PyUIntArrType_Type ||
pytype == &PyULongArrType_Type ||
pytype == &PyULongLongArrType_Type ||
pytype == &PyHalfArrType_Type ||
pytype == &PyFloatArrType_Type ||
pytype == &PyDoubleArrType_Type ||
pytype == &PyLongDoubleArrType_Type ||
pytype == &PyCFloatArrType_Type ||
pytype == &PyCDoubleArrType_Type ||
pytype == &PyCLongDoubleArrType_Type ||
pytype == &PyIntpArrType_Type ||
pytype == &PyUIntpArrType_Type ||
pytype == &PyDatetimeArrType_Type ||
pytype == &PyTimedeltaArrType_Type)
{
return 1;
}
return 0;
Expand Down
6 changes: 6 additions & 0 deletions numpy/_core/tests/test_stringdtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,12 @@ def test_nat_casts():
np.array([output_object]*arr.size, dtype=dtype))


def test_nat_conversion():
for nat in [np.datetime64("NaT", "s"), np.timedelta64("NaT", "s")]:
with pytest.raises(ValueError, match="string coercion is disabled"):
np.array(["a", nat], dtype=StringDType(coerce=False))


def test_growing_strings(dtype):
# growing a string leads to a heap allocation, this tests to make sure
# we do that bookkeeping correctly for all possible starting cases
Expand Down
0