-
-
Notifications
You must be signed in to change notification settings - Fork 11k
ENH: Allow dtype objects to be indexed with multiple fields at once #10417
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2104353
ENH: Allow dtype objects to be indexing with multiple fields at once
eric-wieser 90f710b
MAINT: Reuse code from dtype.__getitem__ in mapping.c:_get_field_view
eric-wieser 32a5702
Merge branch 'master' into dtype-lookup-sequence
seberg 85537e9
Merge branch 'master' into dtype-lookup-sequence
eric-wieser 0f01fd4
fixup, add missing whitespace
seberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3370,6 +3370,117 @@ _subscript_by_index(PyArray_Descr *self, Py_ssize_t i) | |
return ret; | ||
} | ||
|
||
static npy_bool | ||
_is_list_of_strings(PyObject *obj) | ||
{ | ||
int seqlen, i; | ||
if (!PyList_CheckExact(obj)) { | ||
return NPY_FALSE; | ||
} | ||
seqlen = PyList_GET_SIZE(obj); | ||
for (i = 0; i < seqlen; i++) { | ||
PyObject *item = PyList_GET_ITEM(obj, i); | ||
if (!PyBaseString_Check(item)) { | ||
return NPY_FALSE; | ||
} | ||
} | ||
|
||
return NPY_TRUE; | ||
} | ||
|
||
NPY_NO_EXPORT PyArray_Descr * | ||
arraydescr_field_subset_view(PyArray_Descr *self, PyObject *ind) | ||
{ | ||
int seqlen, i; | ||
PyObject *fields = NULL; | ||
PyObject *names = NULL; | ||
PyArray_Descr *view_dtype; | ||
|
||
seqlen = PySequence_Size(ind); | ||
if (seqlen == -1) { | ||
return NULL; | ||
} | ||
|
||
fields = PyDict_New(); | ||
if (fields == NULL) { | ||
goto fail; | ||
} | ||
names = PyTuple_New(seqlen); | ||
if (names == NULL) { | ||
goto fail; | ||
} | ||
|
||
for (i = 0; i < seqlen; i++) { | ||
PyObject *name; | ||
PyObject *tup; | ||
|
||
name = PySequence_GetItem(ind, i); | ||
if (name == NULL) { | ||
goto fail; | ||
} | ||
|
||
/* Let the names tuple steal a reference now, so we don't need to | ||
* decref name if an error occurs further on. | ||
*/ | ||
PyTuple_SET_ITEM(names, i, name); | ||
|
||
tup = PyDict_GetItem(self->fields, name); | ||
if (tup == NULL) { | ||
PyErr_SetObject(PyExc_KeyError, name); | ||
seberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
goto fail; | ||
} | ||
|
||
/* disallow use of titles as index */ | ||
if (PyTuple_Size(tup) == 3) { | ||
PyObject *title = PyTuple_GET_ITEM(tup, 2); | ||
int titlecmp = PyObject_RichCompareBool(title, name, Py_EQ); | ||
if (titlecmp < 0) { | ||
goto fail; | ||
} | ||
if (titlecmp == 1) { | ||
/* if title == name, we were given a title, not a field name */ | ||
PyErr_SetString(PyExc_KeyError, | ||
"cannot use field titles in multi-field index"); | ||
goto fail; | ||
} | ||
if (PyDict_SetItem(fields, title, tup) < 0) { | ||
goto fail; | ||
} | ||
} | ||
/* disallow duplicate field indices */ | ||
if (PyDict_Contains(fields, name)) { | ||
PyObject *msg = NULL; | ||
PyObject *fmt = PyUString_FromString( | ||
"duplicate field of name {!r}"); | ||
if (fmt != NULL) { | ||
msg = PyObject_CallMethod(fmt, "format", "O", name); | ||
Py_DECREF(fmt); | ||
} | ||
PyErr_SetObject(PyExc_ValueError, msg); | ||
Py_XDECREF(msg); | ||
goto fail; | ||
} | ||
if (PyDict_SetItem(fields, name, tup) < 0) { | ||
goto fail; | ||
} | ||
} | ||
|
||
view_dtype = PyArray_DescrNewFromType(NPY_VOID); | ||
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. For a later commit, but: This should probably just copy the original dtype. |
||
if (view_dtype == NULL) { | ||
goto fail; | ||
} | ||
view_dtype->elsize = self->elsize; | ||
view_dtype->names = names; | ||
view_dtype->fields = fields; | ||
view_dtype->flags = self->flags; | ||
return view_dtype; | ||
|
||
fail: | ||
Py_XDECREF(fields); | ||
Py_XDECREF(names); | ||
return NULL; | ||
} | ||
|
||
static PyObject * | ||
descr_subscript(PyArray_Descr *self, PyObject *op) | ||
{ | ||
|
@@ -3380,14 +3491,18 @@ descr_subscript(PyArray_Descr *self, PyObject *op) | |
if (PyBaseString_Check(op)) { | ||
return _subscript_by_name(self, op); | ||
} | ||
else if (_is_list_of_strings(op)) { | ||
return (PyObject *)arraydescr_field_subset_view(self, op); | ||
} | ||
else { | ||
8000 td> | Py_ssize_t i = PyArray_PyIntAsIntp(op); | |
if (error_converting(i)) { | ||
/* if converting to an int gives a type error, adjust the message */ | ||
PyObject *err = PyErr_Occurred(); | ||
if (PyErr_GivenExceptionMatches(err, PyExc_TypeError)) { | ||
PyErr_SetString(PyExc_TypeError, | ||
"Field key must be an integer, string, or unicode."); | ||
"Field key must be an integer field offset, " | ||
"single field name, or list of field names."); | ||
} | ||
return NULL; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.