-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
to_dict without data #2659
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
to_dict without data #2659
Changes from 3 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 |
---|---|---|
|
@@ -1759,7 +1759,7 @@ def to_netcdf(self, *args, **kwargs): | |
|
||
return dataset.to_netcdf(*args, **kwargs) | ||
|
||
def to_dict(self): | ||
def to_dict(self, data=True): | ||
""" | ||
Convert this xarray.DataArray into a dictionary following xarray | ||
naming conventions. | ||
|
@@ -1768,22 +1768,20 @@ def to_dict(self): | |
Useful for coverting to json. To avoid datetime incompatibility | ||
use decode_times=False kwarg in xarrray.open_dataset. | ||
|
||
Parameters | ||
---------- | ||
data : bool, optional | ||
Whether to include the actual data in the dictionary. When set to | ||
False, returns just the schema. | ||
|
||
See also | ||
-------- | ||
DataArray.from_dict | ||
""" | ||
d = {'coords': {}, 'attrs': decode_numpy_dict_values(self.attrs), | ||
'dims': self.dims} | ||
|
||
d = self.variable.to_dict(data=data) | ||
d.update({'coords': {}, 'name': self.name}) | ||
for k in self.coords: | ||
data = ensure_us_time_resolution(self[k].values).tolist() | ||
d['coords'].update({ | ||
k: {'data': data, | ||
'dims': self[k].dims, | ||
'attrs': decode_numpy_dict_values(self[k].attrs)}}) | ||
|
||
d.update({'data': ensure_us_time_resolution(self.values).tolist(), | ||
'name': self.name}) | ||
d['coords'].update({k: self.coords[k].variable.to_dict(data=data)}) | ||
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. This is only adding one item, so just using indexing for assignment seems cleaner: |
||
return d | ||
|
||
@classmethod | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2909,6 +2909,13 @@ def test_to_and_from_dict(self): | |
ValueError, "cannot convert dict without the key 'data'"): | ||
DataArray.from_dict(d) | ||
|
||
# check the data=False option | ||
expected_no_data = {**expected} | ||
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. I guess this is a fancy Py3 way of doing a copy? :) I would lean towards the more explicit |
||
del expected_no_data['data'] | ||
del expected_no_data['coords']['x']['data'] | ||
actual_no_data = array.to_dict(data=False) | ||
assert expected_no_data == actual_no_data | ||
|
||
def test_to_and_from_dict_with_time_dim(self): | ||
x = np.random.randn(10, 3) | ||
t = pd.date_range('20130101', periods=10) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3045,11 +3045,20 @@ def test_to_and_from_dict(self): | |
# check roundtrip | ||
assert_identical(ds, Dataset.from_dict(actual)) | ||
|
||
# check the data=False option | ||
expected_no_data = {**expected} | ||
print(expected_no_data) | ||
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. There is a forgotten |
||
del expected_no_data['coords']['t']['data'] | ||
del expected_no_data['data_vars']['a']['data'] | ||
del expected_no_data['data_vars']['b']['data'] | ||
actual_no_data = ds.to_dict(data=False) | ||
assert expected_no_data == actual_no_data | ||
|
||
# verify coords are included roundtrip | ||
expected = ds.set_coords('b') | ||
actual = Dataset.from_dict(expected.to_dict()) | ||
expected_ds = ds.set_coords('b') | ||
actual = Dataset.from_dict(expected_ds.to_dict()) | ||
|
||
assert_identical(expected, actual) | ||
assert_identical(expected_ds, actual) | ||
|
||
# test some incomplete dicts: | ||
# this one has no attrs field, the dims are strings, and x, y are | ||
|
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.
It could be useful to allow including the data for the coordinates but not the data variables?
I'm thinking something like