-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Keep attrs in call to astype #2070
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
Changes from all commits
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 |
---|---|---|
|
@@ -302,6 +302,40 @@ def data(self, data): | |
"replacement data must match the Variable's shape") | ||
self._data = data | ||
|
||
def astype(self, dtype, casting = 'unsafe', copy = True): | ||
""" | ||
Copy of the Variable object, with data cast to a specified type. | ||
|
||
Parameters | ||
---------- | ||
dtype : str or dtype | ||
Typecode or data-type to which the array is cast. | ||
casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional | ||
Controls what kind of data casting may occur. Defaults to 'unsafe' | ||
for backwards compatibility. | ||
|
||
* 'no' means the data types should not be cast at all. | ||
* 'equiv' means only byte-order changes are allowed. | ||
* 'safe' means only casts which can preserve values are allowed. | ||
* 'same_kind' means only safe casts or casts within a kind, | ||
like float64 to float32, are allowed. | ||
* 'unsafe' means any data conversions may be done. | ||
copy : bool, optional | ||
By default, astype always returns a newly allocated array. If this | ||
is set to False and the `dtype` requirement is satisfied, the input | ||
array is returned instead of a copy. | ||
|
||
See also | ||
-------- | ||
np.ndarray.astype | ||
dask.array.Array.astype | ||
""" | ||
self.data = duck_array_ops.astype(self.data, | ||
dtype, | ||
casting = casting, | ||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
copy = copy) | ||
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. E251 unexpected spaces around keyword / parameter equals |
||
return self | ||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def load(self, **kwargs): | ||
"""Manually trigger loading of this variable's data from disk or a | ||
remote source into memory and return this variable. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1446,8 +1446,8 @@ def test_dataset_getitem(self): | |
|
||
def test_array_interface(self): | ||
assert_array_equal(np.asarray(self.dv), self.x) | ||
# test patched in methods | ||
assert_array_equal(self.dv.astype(float), self.v.astype(float)) | ||
# test patched in methods | ||
assert_array_equal(self.dv.argsort(), self.v.argsort()) | ||
assert_array_equal(self.dv.clip(2, 3), self.v.clip(2, 3)) | ||
# test ufuncs | ||
|
@@ -1458,6 +1458,10 @@ def test_array_interface(self): | |
bar = Variable(['x', 'y'], np.zeros((10, 20))) | ||
assert_equal(self.dv, np.maximum(self.dv, bar)) | ||
|
||
def test_astype_attrs(self): | ||
mda = self.mda.astype(bool) | ||
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. It would be good to test the datatype converting behavior of |
||
assert list(mda.attrs.items()) == list(self.mda.attrs.items()) | ||
|
||
def test_is_null(self): | ||
x = np.random.RandomState(42).randn(5, 6) | ||
x[x < 0] = np.nan | ||
|
Uh oh!
There was an error while loading. Please reload this page.