-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Dtype kind vs char #2864
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
Dtype kind vs char #2864
Changes from 3 commits
df1be99
b59299d
1400520
f455222
400ffea
f3ba900
14c9f7d
46701ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -507,11 +507,16 @@ class dtype : public object { | |
return detail::array_descriptor_proxy(m_ptr)->names != nullptr; | ||
} | ||
|
||
/// Single-character type code. | ||
/// Single-character for dtype's kind (ex: float and double are 'f' or int and long int are 'i') | ||
char kind() const { | ||
return detail::array_descriptor_proxy(m_ptr)->kind; | ||
} | ||
|
||
/// Single-character for dtype's type (ex: float is 'f' and double 'd') | ||
char char_() const { | ||
return detail::array_descriptor_proxy(m_ptr)->type; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth adding a comment that we're following the Python API of See also https://numpy.org/devdocs/reference/c-api/types-and-structures.html#c.PyArray_Descr for the root cause of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay ! |
||
} | ||
|
||
private: | ||
static object _dtype_from_pep3118() { | ||
static PyObject *obj = module_::import("numpy.core._internal") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -251,6 +251,38 @@ py::list test_dtype_ctors() { | |
return list; | ||
} | ||
|
||
py::list test_dtype_kind() { | ||
py::list list; | ||
for (auto& dt : { | ||
py::dtype("bool8"), // bool | ||
py::dtype("int16"), // short | ||
py::dtype("int32"), // int | ||
py::dtype("int64"), // long int | ||
py::dtype("float32"), // float | ||
py::dtype("float64"), // double | ||
py::dtype("float128") // long double | ||
}) { | ||
list.append(dt.kind()); | ||
} | ||
return list; | ||
} | ||
|
||
py::list test_dtype_type() { | ||
py::list list; | ||
for (auto& dt : { | ||
py::dtype("bool8"), // bool | ||
py::dtype("int16"), // short | ||
py::dtype("int32"), // int | ||
py::dtype("int64"), // long int | ||
py::dtype("float32"), // float | ||
py::dtype("float64"), // double | ||
py::dtype("float128") // long double | ||
|
||
}) { | ||
list.append(dt.char_()); | ||
} | ||
return list; | ||
} | ||
|
||
struct A {}; | ||
struct B {}; | ||
|
||
|
@@ -376,6 +408,8 @@ TEST_SUBMODULE(numpy_dtypes, m) { | |
return l; | ||
}); | ||
m.def("test_dtype_ctors", &test_dtype_ctors); | ||
m.def("test_dtype_kind", &test_dtype_kind); | ||
m.def("test_dtype_type", &test_dtype_type); | ||
m.def("test_dtype_methods", []() { | ||
py::list list; | ||
auto dt1 = py::dtype::of<int32_t>(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"float and double" could be "floating point types", and "int and long" could be "integral types". But this works as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay !