-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
add info method to dataset #1176
Changes from 3 commits
50d15ea
84ec045
49bb76c
7c4c4e0
2b56678
74df621
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 |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
from collections import Mapping | ||
from numbers import Number | ||
|
||
import sys | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
|
@@ -802,6 +804,42 @@ def to_netcdf(self, path=None, mode='w', format=None, group=None, | |
def __unicode__(self): | ||
return formatting.dataset_repr(self) | ||
|
||
def info(self, buf=None): | ||
""" | ||
Concise summary of a Dataset variables and attributes. | ||
Parameters | ||
---------- | ||
buf : writable buffer, defaults to sys.stdout | ||
|
||
See Also | ||
-------- | ||
pandas.DataFrame.assign | ||
netCDF's ncdump | ||
""" | ||
|
||
if buf is None: # pragma: no cover | ||
buf = sys.stdout | ||
|
||
lines = [] | ||
lines.append('xarray.Dataset {') | ||
lines.append('dimensions:') | ||
for name, size in self.dims.items(): | ||
lines.append('\t{name} = {size} ;'.format(name=name, size=size)) | ||
lines.append('\nvariables:') | ||
for name, da in self.variables.items(): | ||
dims = ', '.join(da.dims) | ||
lines.append('\t{type} {name}({dims}) ;'.format( | ||
type=da.dtype, name=name, dims=dims)) | ||
for k, v in da.attrs.items(): | ||
lines.append('\t\t{name}:{k} = {v} ;'.format(name=name, k=k, | ||
v=v)) | ||
lines.append('\n// global attributes:') | ||
for k, v in self.attrs.items(): | ||
lines.append('\t:{k} = {v} ;'.format(k=k, v=v)) | ||
lines.append('}') | ||
|
||
formatting._put_lines(buf, lines) | ||
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 would probably just use |
||
|
||
@property | ||
def chunks(self): | ||
"""Block dimensions for this dataset's data or None if it's not a dask | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,10 @@ | |
import dask.array as da | ||
except ImportError: | ||
pass | ||
try: | ||
from io import StringIO | ||
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. we might put this in in |
||
except ImportError: | ||
from cStringIO import StringIO | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
@@ -190,6 +194,39 @@ def test_unicode_data(self): | |
actual = unicode_type(data) | ||
self.assertEqual(expected, actual) | ||
|
||
def test_info(self): | ||
ds = create_test_data(seed=123) | ||
ds = ds.drop('dim3') # string type prints differently in PY2 vs PY3 | ||
ds.attrs['foo'] = 'bar' | ||
buf = StringIO() | ||
ds.info(buf=buf) | ||
|
||
expected = dedent(u'''\ | ||
xarray.Dataset { | ||
dimensions: | ||
dim1 = 8 ; | ||
dim2 = 9 ; | ||
dim3 = 10 ; | ||
time = 20 ; | ||
|
||
variables: | ||
datetime64[ns] time(time) ; | ||
float64 dim2(dim2) ; | ||
float64 var1(dim1, dim2) ; | ||
var1:foo = variable ; | ||
float64 var2(dim1, dim2) ; | ||
var2:foo = variable ; | ||
float64 var3(dim3, dim1) ; | ||
var3:foo = variable ; | ||
int64 numbers(dim3) ; | ||
|
||
// global attributes: | ||
:foo = bar ; | ||
}''') | ||
actual = buf.getvalue() | ||
self.assertEqual(expected, actual) | ||
buf.close() | ||
|
||
def test_constructor(self): | ||
x1 = ('x', 2 * np.arange(100)) | ||
x2 = ('x', np.arange(1000)) | ||
|
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.
I think these probably should all be unicode (using
u
literals), otherwise this will break for non-ASCII characters on Python 2. Take a look at what things look like informatting.py
.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 would also be good to add a test for attributes with non-ASCII values.