Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e9b771b

Browse filesBrowse files
committed
ENH: Performance improvement for astype
1 parent 75a2a3a commit e9b771b
Copy full SHA for e9b771b

File tree

Expand file treeCollapse file tree

1 file changed

+17
-1
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+17
-1
lines changed

‎numpy/_core/src/multiarray/scalartypes.c.src

Copy file name to clipboardExpand all lines: numpy/_core/src/multiarray/scalartypes.c.src
+17-1Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2131,7 +2131,7 @@ gentype_byteswap(PyObject *self, PyObject *args, PyObject *kwds)
21312131
*
21322132
* #name = take, getfield, put, repeat, tofile, mean, trace, diagonal, clip,
21332133
* std, var, sum, cumsum, prod, cumprod, compress, sort, argsort,
2134-
* round, argmax, argmin, max, min, any, all, astype, resize,
2134+
* round, argmax, argmin, max, min, any, all, resize,
21352135
* reshape, choose, tostring, tobytes, copy, searchsorted, view,
21362136
* flatten, ravel, squeeze#
21372137
*/
@@ -2142,6 +2142,22 @@ gentype_@name@(PyObject *self, PyObject *args, PyObject *kwds)
21422142
}
21432143
/**end repeat**/
21442144

2145+
static PyObject *
2146+
gentype_astype(PyObject *self, PyObject *args, PyObject *kwds)
2147+
{
2148+
if ((kwds == NULL) && (PyTuple_Size(args)==1)) {
2149+
// no keyword arguments, single argument is the dtype
2150+
PyArray_Descr *dtype = PyArray_DescrFromScalar(self);
2151+
assert(dtype != NULL);
2152+
int same_dtype = ( (PyObject *)dtype == PyTuple_GET_ITEM(args, 0));
2153+
Py_DECREF(dtype);
2154+
if (same_dtype) {
2155+
// dtypes are equal, scalars are immutable so no copy is needed
2156+
return Py_NewRef(self);
2157+
}
2158+
}
2159+
return gentype_generic_method(self, args, kwds, "astype");
2160+
}
21452161

21462162
/**begin repeat
21472163
* #name = integer, floating#

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.