Skip to content
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 keys_info() method #515

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions intake_esm/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,39 @@ def keys(self) -> typing.List[str]:
"""
return list(self.esmcat._construct_group_keys(sep=self.sep).keys())

def keys_info(self) -> pd.DataFrame:
"""
Get keys for the catalog entries and their metadata

Returns
-------
pandas.DataFrame
keys for the catalog entries and their metadata

Examples
--------

>>> import intake
>>> cat = intake.open_esm_datastore("./tests/sample-catalogs/cesm1-lens-netcdf.json")
>>> cat.keys_info()
component experiment stream
key
ocn.20C.pop.h ocn 20C pop.h
ocn.CTRL.pop.h ocn CTRL pop.h
ocn.RCP85.pop.h ocn RCP85 pop.h



"""
results = self.esmcat._construct_group_keys(sep=self.sep)
data = {
key: dict(zip(self.esmcat.aggregation_control.groupby_attrs, results[key]))
for key in results
}
data = pd.DataFrame.from_dict(data, orient='index')
data.index.name = 'key'
return data

@property
def key_template(self) -> str:
"""
Expand Down Expand Up @@ -258,6 +291,7 @@ def __dir__(self) -> typing.List[str]:
'to_datatree',
'to_dask',
'keys',
'keys_info',
'serialize',
'datasets',
'search',
Expand Down
8 changes: 8 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ def test_catalog_getitem_error():
cat['foo']


def test_catalog_keys_info():
cat = intake.open_esm_datastore(cdf_cat_sample_cesmle)
data = cat.keys_info()
assert isinstance(data, pd.DataFrame)
assert data.index.name == 'key'
assert len(data) == len(cat)


@pytest.mark.parametrize(
'catalog_type, to_csv_kwargs, json_dump_kwargs',
[('file', {'compression': 'bz2'}, {}), ('file', {'compression': 'gzip'}, {}), ('dict', {}, {})],
Expand Down