Skip to content

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

Merged
merged 8 commits into from
Feb 23, 2021
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
12 changes: 11 additions & 1 deletion include/pybind11/numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,21 @@ class dtype : public object {
return detail::array_descriptor_proxy(m_ptr)->names != nullptr;
}

/// Single-character type code.
/// Single-character code for dtype's kind.
/// For example, floating point types are 'f' and integral types are 'i'.
char kind() const {
return detail::array_descriptor_proxy(m_ptr)->kind;
}

/// Single-character for dtype's type.
/// For example, ``float`` is 'f', ``double`` 'd', ``int`` 'i', and ``long`` 'd'.
char char_() const {
// Note: The signature, `dtype::char_` follows the naming of NumPy's
// public Python API (i.e., ``dtype.char``), rather than its internal
// C API (``PyArray_Descr::type``).
return detail::array_descriptor_proxy(m_ptr)->type;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 dtype (i.e., dtype.char), rather than NumPy's internal C API (PyArray_Descr::type), for the name?

See also https://numpy.org/devdocs/reference/c-api/types-and-structures.html#c.PyArray_Descr for the root cause of the type vs. char confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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")
Expand Down
20 changes: 20 additions & 0 deletions tests/test_numpy_dtypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@ TEST_SUBMODULE(numpy_dtypes, m) {
});

// test_dtype
std::vector<const char *> dtype_names{
"byte", "short", "intc", "int_", "longlong",
"ubyte", "ushort", "uintc", "uint", "ulonglong",
"half", "single", "double", "longdouble",
"csingle", "cdouble", "clongdouble",
"bool_", "datetime64", "timedelta64", "object_"
};

m.def("print_dtypes", []() {
py::list l;
for (const py::handle &d : {
Expand All @@ -376,6 +384,18 @@ TEST_SUBMODULE(numpy_dtypes, m) {
return l;
});
m.def("test_dtype_ctors", &test_dtype_ctors);
m.def("test_dtype_kind", [dtype_names]() {
py::list list;
for (auto& dt_name : dtype_names)
list.append(py::dtype(dt_name).kind());
return list;
});
m.def("test_dtype_char_", [dtype_names]() {
py::list list;
for (auto& dt_name : dtype_names)
list.append(py::dtype(dt_name).char_());
return list;
});
m.def("test_dtype_methods", []() {
py::list list;
auto dt1 = py::dtype::of<int32_t>();
Expand Down
3 changes: 3 additions & 0 deletions tests/test_numpy_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ def test_dtype(simple_dtype):
np.zeros(1, m.trailing_padding_dtype())
)

assert m.test_dtype_kind() == list("iiiiiuuuuuffffcccbMmO")
assert m.test_dtype_char_() == list("bhilqBHILQefdgFDG?MmO")


def test_recarray(simple_dtype, packed_dtype):
elements = [(False, 0, 0.0, -0.0), (True, 1, 1.5, -2.5), (False, 2, 3.0, -5.0)]
Expand Down