digital-science / dimcli

Python client and CLI for scientometrics and research analytics using the Dimensions API.
https://digital-science.github.io/dimcli/getting-started.html
MIT License
43 stars 6 forks source link

Dataframe method fails with `return in facets` #68

Open lambdamusic opened 3 years ago

lambdamusic commented 3 years ago
%%dsldf
search publications 
return in "facets" funders 
return in "facets" research_orgs

leads to

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
~/Envs/jupyterlab/lib/python3.8/site-packages/pandas/core/internals/construction.py in _list_to_arrays(data, columns, coerce_float, dtype)
    495     try:
--> 496         result = _convert_object_array(
    497             content, columns, dtype=dtype, coerce_float=coerce_float

~/Envs/jupyterlab/lib/python3.8/site-packages/pandas/core/internals/construction.py in _convert_object_array(content, columns, coerce_float, dtype)
    579             # caller's responsibility to check for this...
--> 580             raise AssertionError(
    581                 f"{len(columns)} columns passed, passed data had "

AssertionError: 1 columns passed, passed data had 20 columns

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
<ipython-input-69-c85bc5dbe030> in <module>
----> 1 get_ipython().run_cell_magic('dsldf', '', 'search publications \nreturn in "facets" funders \nreturn in "facets" research_orgs\n')

~/Envs/jupyterlab/lib/python3.8/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2369             with self.builtin_trap:
   2370                 args = (magic_arg_s, cell)
-> 2371                 result = fn(*args, **kwargs)
   2372             return result
   2373 

<decorator-gen-127> in dsldf(self, line, cell)

~/Envs/jupyterlab/lib/python3.8/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

~/Envs/jupyterlab/lib/python3.8/site-packages/dimcli/jupyter/magics.py in dsldf(self, line, cell)
     69                 print("Sorry - DSL to dataframe magic methods work only with `search` queries.")
     70                 return None
---> 71             data = self._handle_query(line).as_dataframe()
     72             self.shell.user_ns[self.results_var] = data
     73             return data

~/Envs/jupyterlab/lib/python3.8/site-packages/dimcli/core/api.py in as_dataframe(self, key)
    711 
    712         if not self.json.get("errors"):
--> 713             return self.df_factory.df_simple(self.json, key)
    714 
    715 

~/Envs/jupyterlab/lib/python3.8/site-packages/dimcli/core/dataframe_factory.py in df_simple(self, data, key)
     46                     output = pd.DataFrame.from_dict(data[valid_key])
     47             elif type(data[valid_key]) == dict: # top level dict, use keys as index
---> 48                 output = pd.DataFrame.from_dict(data[valid_key], orient="index", columns=[valid_key])
     49             else: # no list, then make one and try to return everything
     50                 output = pd.DataFrame.from_dict([data])

~/Envs/jupyterlab/lib/python3.8/site-packages/pandas/core/frame.py in from_dict(cls, data, orient, dtype, columns)
   1245             raise ValueError("only recognize index or columns for orient")
   1246 
-> 1247         return cls(data, index=index, columns=columns, dtype=dtype)
   1248 
   1249     def to_numpy(self, dtype=None, copy=False) -> np.ndarray:

~/Envs/jupyterlab/lib/python3.8/site-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
    472                     if is_named_tuple(data[0]) and columns is None:
    473                         columns = data[0]._fields
--> 474                     arrays, columns = to_arrays(data, columns, dtype=dtype)
    475                     columns = ensure_index(columns)
    476 

~/Envs/jupyterlab/lib/python3.8/site-packages/pandas/core/internals/construction.py in to_arrays(data, columns, coerce_float, dtype)
    459         return [], []  # columns if columns is not None else []
    460     if isinstance(data[0], (list, tuple)):
--> 461         return _list_to_arrays(data, columns, coerce_float=coerce_float, dtype=dtype)
    462     elif isinstance(data[0], abc.Mapping):
    463         return _list_of_dict_to_arrays(

~/Envs/jupyterlab/lib/python3.8/site-packages/pandas/core/internals/construction.py in _list_to_arrays(data, columns, coerce_float, dtype)
    498         )
    499     except AssertionError as e:
--> 500         raise ValueError(e) from e
    501     return result
    502 

ValueError: 1 columns passed, passed data had 20 columns