'ValueError: Invalid file path or buffer object type

I've been using mplsoccer library and statsbombpy libraries for a while now with success. Recently, I've tried to use it again with this code (not fully reproducible due to it being behind a paid api).

!pip install mplsoccer
import pandas as pd
import requests
from mplsoccer.statsbomb import read_event
username = creds['user']
password = creds['passwd']
auth = requests.auth.HTTPBasicAuth(username, password)
URL = 'https://data.statsbombservices.com/api/v5/events/18241'
response = requests.get(URL, auth = auth)
df_dict = read_event(response)

and I'm now starting to get the ValueError of invalid file path or buffer type. I contacted the owner of mplsoccer and asked him about it, and he said it wasn't a reproducible error for him, but it looks like my pandas is having trouble reading it.

response is returning exactly what it should be, it just fails with the below error code



---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-64-9be43ec4f519> in <module>
      6 URL = 'https://data.statsbombservices.com/api/v5/events/7430'
      7 response = requests.get(URL, auth=auth)
----> 8 df_dict = read_event(response)

E:\py\lib\site-packages\mplsoccer\statsbomb.py in read_event(path_or_buf, related_event_df, shot_freeze_frame_df, tactics_lineup_df, warn)
    120         match_id = int(path_or_buf.url.split('/')[-1].split('.')[0])
    121     else:
--> 122         df = pd.read_json(path_or_buf, encoding='utf-8')
    123         match_id = int(os.path.basename(path_or_buf)[:-5])
    124 

E:\py\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    205                 else:
    206                     kwargs[new_arg_name] = new_arg_value
--> 207             return func(*args, **kwargs)
    208 
    209         return cast(F, wrapper)

E:\py\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    309                     stacklevel=stacklevel,
    310                 )
--> 311             return func(*args, **kwargs)
    312 
    313         return wrapper

E:\py\lib\site-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, encoding_errors, lines, chunksize, compression, nrows, storage_options)
    588         convert_axes = True
    589 
--> 590     json_reader = JsonReader(
    591         path_or_buf,
    592         orient=orient,

E:\py\lib\site-packages\pandas\io\json\_json.py in __init__(self, filepath_or_buffer, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines, chunksize, compression, nrows, storage_options, encoding_errors)
    673                 raise ValueError("nrows can only be passed if lines=True")
    674 
--> 675         data = self._get_data_from_filepath(filepath_or_buffer)
 676         self.data = self._preprocess_data(data)
    677 

E:\py\lib\site-packages\pandas\io\json\_json.py in _get_data_from_filepath(self, filepath_or_buffer)
    710             or file_exists(filepath_or_buffer)
    711         ):
--> 712             self.handles = get_handle(
    713                 filepath_or_buffer,
    714                 "r",

E:\py\lib\site-packages\pandas\io\common.py in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)
    606 
    607     # open URLs
--> 608     ioargs = _get_filepath_or_buffer(
    609         path_or_buf,
    610         encoding=encoding,

E:\py\lib\site-packages\pandas\io\common.py in _get_filepath_or_buffer(filepath_or_buffer, encoding, compression, mode, storage_options)
    393     if not is_file_like(filepath_or_buffer):
    394         msg = f"Invalid file path or buffer object type: {type(filepath_or_buffer)}"
--> 395         raise ValueError(msg)
    396 
    397     return IOArgs(

ValueError: Invalid file path or buffer object type: <class 'requests_cache.models.response.CachedResponse'>

hoping someone can help me see exactly where pandas is struggling and what I can do to fix it? Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source