0

Normally if I have a json response from a API, I can load it as a dataframe using something like - pd.read_json(json)

but I'm noticed that some of the apis I call that only have a single item returned are not working. For example,a rest call returns:

{
    "carbon_growth": 0.050962261831616916
}

then taking the response of a request to that endpoint and trying to cast it as a dataframe with the above command doesn't work and returns the following error:

/usr/local/lib/python3.7/dist-packages/py15rock/get.py in requestCompanyReturning(ticker, endpoint)
     14     with httpx.Client() as client:
     15         r = client.get(f'{config.api_endpoint}/company/{ticker}/{endpoint}', headers={'Authorization': f'{config.api_key}'}, timeout=timeout)
---> 16     df = pd.read_json(r.content)
     17     return df
     18 

/usr/local/lib/python3.7/dist-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    197                 else:
    198                     kwargs[new_arg_name] = new_arg_value
--> 199             return func(*args, **kwargs)
    200 
    201         return cast(F, wrapper)

/usr/local/lib/python3.7/dist-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    294                 )
    295                 warnings.warn(msg, FutureWarning, stacklevel=stacklevel)
--> 296             return func(*args, **kwargs)
    297 
    298         return wrapper

/usr/local/lib/python3.7/dist-packages/pandas/io/json/_json.py in read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines, chunksize, compression, nrows)
    616         return json_reader
    617 
--> 618     result = json_reader.read()
    619     if should_close:
    620         filepath_or_buffer.close()

/usr/local/lib/python3.7/dist-packages/pandas/io/json/_json.py in read(self)
    753                 obj = self._get_object_parser(self._combine_lines(data))
    754         else:
--> 755             obj = self._get_object_parser(self.data)
    756         self.close()
    757         return obj

/usr/local/lib/python3.7/dist-packages/pandas/io/json/_json.py in _get_object_parser(self, json)
    775         obj = None
    776         if typ == "frame":
--> 777             obj = FrameParser(json, **kwargs).parse()
    778 
    779         if typ == "series" or obj is None:

/usr/local/lib/python3.7/dist-packages/pandas/io/json/_json.py in parse(self)
    884 
    885         else:
--> 886             self._parse_no_numpy()
    887 
    888         if self.obj is None:

/usr/local/lib/python3.7/dist-packages/pandas/io/json/_json.py in _parse_no_numpy(self)
   1117         if orient == "columns":
   1118             self.obj = DataFrame(
-> 1119                 loads(json, precise_float=self.precise_float), dtype=None
   1120             )
   1121         elif orient == "split":

/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
    466 
    467         elif isinstance(data, dict):
--> 468             mgr = init_dict(data, index, columns, dtype=dtype)
    469         elif isinstance(data, ma.MaskedArray):
    470             import numpy.ma.mrecords as mrecords

/usr/local/lib/python3.7/dist-packages/pandas/core/internals/construction.py in init_dict(data, index, columns, dtype)
    281             arr if not is_datetime64tz_dtype(arr) else arr.copy() for arr in arrays
    282         ]
--> 283     return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
    284 
    285 

/usr/local/lib/python3.7/dist-packages/pandas/core/internals/construction.py in arrays_to_mgr(arrays, arr_names, index, columns, dtype, verify_integrity)
     76         # figure out the index, if necessary
     77         if index is None:
---> 78             index = extract_index(arrays)
     79         else:
     80             index = ensure_index(index)

/usr/local/lib/python3.7/dist-packages/pandas/core/internals/construction.py in extract_index(data)
    385 
    386         if not indexes and not raw_lengths:
--> 387             raise ValueError("If using all scalar values, you must pass an index")
    388 
    389         if have_series:

ValueError: If using all scalar values, you must pass an index

Is there something I can do to access responses like above? It seems to work when the response is multiple rows.

Lostsoul
  • 23,230
  • 42
  • 133
  • 217
  • 1
    There are a few ways outlined in the linked duplicate. `df = pd.read_json(json, typ='series').to_frame().T` or `df = pd.read_json(json, typ='series')` works for this example depending how you want the orientation. However, I think using json.loads then doing [`df = pd.DataFrame([data])`](https://stackoverflow.com/a/55720630/15497888) is a little cleaner – Henry Ecker Sep 21 '21 at 21:34

0 Answers0