-
-
Notifications
You must be signed in to change notification settings - Fork 132
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
Respect copy=False
in astype
#328
Conversation
Previously this would make a copy anyway.
Codecov Report
@@ Coverage Diff @@
## master #328 +/- ##
==========================================
- Coverage 93.98% 93.94% -0.04%
==========================================
Files 19 19
Lines 2327 2329 +2
==========================================
+ Hits 2187 2188 +1
- Misses 140 141 +1 |
sparse/_coo/core.py
Outdated
@@ -2186,6 +2186,9 @@ def astype(self, dtype, copy=True): | |||
:obj:`COO.elemwise`: Apply an arbitrary element-wise function to one or two | |||
arguments. | |||
""" | |||
# this matches numpy's behavior | |||
if self.dtype == dtype and copy == False: |
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.
Comparing dtypes can be tricky. To be safe:
if not copy and self.dtype.type == np.dtype(dtype).type:
return self
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.
Your suggestion doesn't match the behavior of regular numpy arrays.
Worse, it does the wrong thing for reversed-endian types.
The one thing your approach does ensure is that intc
, int_
, and longlong
are respected by identity - but numpy doesn't do that all over the place.
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.
Good point. I'll amend my recommendation to only coerce the user input, then:
if not copy and self.dtype == np.dtype(dtype):
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.
That coercion happens automatically anyway, and is probably faster at the C level where it doesn't need a python function call.
Thanks! |
Thank you, @eric-wieser! |
Previously this would make a copy anyway.