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

Computing unique values in an empty catalog results in an error #378

Closed
andersy005 opened this issue Oct 14, 2021 · 0 comments · Fixed by #391
Closed

Computing unique values in an empty catalog results in an error #378

andersy005 opened this issue Oct 14, 2021 · 0 comments · Fixed by #391
Labels
bug Issues that present a reasonable conviction there is a reproducible bug.

Comments

@andersy005
Copy link
Member

Description

Computing unique values in an empty catalog result in an error. We should add a check for an empty dataframe to ensure we return an empty data frame early.

What I Did

In [4]: import intake

In [5]: cat = intake.open_esm_datastore("./tests/sample-collections/catalog-dict-records.json")

In [6]: cat_sub = cat.search(variable='foo')
/Users/abanihi/devel/intake/intake-esm/intake_esm/_types.py:303: UserWarning: Query: {'variable': ['foo']} returned zero results.
  results = search(self.df, _query, self.columns_with_iterables)

In [7]: cat_sub
Out[7]: <aws-cesm1-le catalog with 0 dataset(s) from 0 asset(s)>

In [8]: cat_sub.esmcat.unique()

Traceback:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-37a1bfa1a437> in <module>
----> 1 cat_sub.esmcat.unique()

~/devel/intake/intake-esm/intake_esm/_types.py in unique(self)
    276 
    277     def unique(self) -> pd.Series:
--> 278         return pd.Series(self._unique())
    279 
    280     def nunique(self) -> pd.Series:

~/devel/intake/intake-esm/intake_esm/_types.py in _unique(self)
    273             return list(tlz.unique(values))
    274 
--> 275         return self.df[self.df.columns].apply(_find_unique, result_type='reduce').to_dict()
    276 
    277     def unique(self) -> pd.Series:

~/.mambaforge/envs/intake-esm-dev/lib/python3.9/site-packages/pandas/core/frame.py in apply(self, func, axis, raw, result_type, args, **kwargs)
   8739             kwargs=kwargs,
   8740         )
-> 8741         return op.apply()
   8742 
   8743     def applymap(

~/.mambaforge/envs/intake-esm-dev/lib/python3.9/site-packages/pandas/core/apply.py in apply(self)
    680         # one axis empty
    681         elif not all(self.obj.shape):
--> 682             return self.apply_empty_result()
    683 
    684         # raw

~/.mambaforge/envs/intake-esm-dev/lib/python3.9/site-packages/pandas/core/apply.py in apply_empty_result(self)
    750                 r = np.nan
    751 
--> 752             return self.obj._constructor_sliced(r, index=self.agg_axis)
    753         else:
    754             return self.obj.copy()

~/.mambaforge/envs/intake-esm-dev/lib/python3.9/site-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    428                 index = ibase.default_index(len(data))
    429             elif is_list_like(data):
--> 430                 com.require_length_match(data, index)
    431 
    432             # create/copy the manager

~/.mambaforge/envs/intake-esm-dev/lib/python3.9/site-packages/pandas/core/common.py in require_length_match(data, index)
    529     """
    530     if len(data) != len(index):
--> 531         raise ValueError(
    532             "Length of values "
    533             f"({len(data)}) "

ValueError: Length of values (0) does not match length of index (5)

Relevant lines:

return self.df[self.df.columns].apply(_find_unique, result_type='reduce').to_dict()

@andersy005 andersy005 added the bug Issues that present a reasonable conviction there is a reproducible bug. label Oct 14, 2021
@andersy005 andersy005 added this to Xdev Oct 22, 2021
@andersy005 andersy005 moved this to 🌳 Todo in Xdev Oct 22, 2021
Repository owner moved this from 🌳 Todo to ✅ Done in Xdev Oct 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues that present a reasonable conviction there is a reproducible bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant