Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

python - Address 'Protocol Unknown Error" in Pandas read_json

I am attempting to load some meteorlogical data from a NASA web site into a Pandas DataFrame. I am getting a ValueError: Protocol Unknown Error. This is my specific code:

import pandas as pd
import requests 

cmd = 'https://power.larc.nasa.gov/cgi-bin/v1/DataAccess.py?request=execute&identifier=SinglePoint&parameters=T10M&startDate=20200101&endDate=20200103&userCommunity=SSE&tempAverage=DAILY&outputList=JSON,ASCII&lat=-0.2739&lon=36.3765&user=anonymous' 

df = pd.read_json(requests.get(cmd).text, lines=True, orient='table')  

Note: I have tried several combinations of read_json parameters all with the same result.

this is my Traceback error report:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-26-cf1926f4d68e> in <module>
      7 """
      8 
----> 9 df = pd.read_json(requests.get(cmd).text, lines=True, orient='table')

D:Anaconda3libsite-packagespandasutil\_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)

D:Anaconda3libsite-packagespandasutil\_decorators.py in wrapper(*args, **kwargs)
    294                 )
    295                 warnings.warn(msg, FutureWarning, stacklevel=stacklevel)
--> 296             return func(*args, **kwargs)
    297 
    298         return wrapper

D:Anaconda3libsite-packagespandasiojson\_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)
    591 
    592     compression = infer_compression(path_or_buf, compression)
--> 593     filepath_or_buffer, _, compression, should_close = get_filepath_or_buffer(
    594         path_or_buf, encoding=encoding, compression=compression
    595     )

D:Anaconda3libsite-packagespandasiocommon.py in get_filepath_or_buffer(filepath_or_buffer, encoding, compression, mode, storage_options)
    219 
    220         try:
--> 221             file_obj = fsspec.open(
    222                 filepath_or_buffer, mode=mode or "rb", **(storage_options or {})
    223             ).open()

D:Anaconda3libsite-packagesfsspeccore.py in open(urlpath, mode, compression, encoding, errors, protocol, newline, **kwargs)
    427     ``OpenFile`` object.
    428     """
--> 429     return open_files(
    430         [urlpath],
    431         mode,

D:Anaconda3libsite-packagesfsspeccore.py in open_files(urlpath, mode, compression, encoding, errors, name_function, num, protocol, newline, auto_mkdir, expand, **kwargs)
    278     be used as a single context
    279     """
--> 280     fs, fs_token, paths = get_fs_token_paths(
    281         urlpath,
    282         mode,

D:Anaconda3libsite-packagesfsspeccore.py in get_fs_token_paths(urlpath, mode, num, name_function, storage_options, protocol, expand)
    598                     "share the same protocol"
    599                 )
--> 600         cls = get_filesystem_class(protocol)
    601         optionss = list(map(cls._get_kwargs_from_urls, urlpath))
    602         paths = [cls._strip_protocol(u) for u in urlpath]

D:Anaconda3libsite-packagesfsspec
egistry.py in get_filesystem_class(protocol)
    189     if protocol not in registry:
    190         if protocol not in known_implementations:
--> 191             raise ValueError("Protocol not known: %s" % protocol)
    192         bit = known_implementations[protocol]
    193         try:

ValueError: Protocol not known: {
 "features": [
  {
   "geometry": {
    "coordinates": [
     36.37651,
     -0.27389,
     2178.98
    ],
    "type": "Point"
   },
   "properties": {
    "parameter": {
     "T10M": {
      "20200101": 15.57,
      "20200102": 15.7,
      "20200103": 16.01
     }
    }
   },
   "type": "Feature"
  }
 ],
 "header": {
  "api_version": "1.1.0",
  "endDate": "20200103",
  "fillValue": "-999",
  "startDate": "20200101",
  "title": "NASA/POWER SRB/FLASHFlux/MERRA2/GEOS 5.12.4 (FP-IT) 0.5 x 0.5 Degree Daily Averaged Data"
 },
 "messages": [],
 "outputs": {
  "ascii": "https

My question is how to go about properly loading this input into a dataframe.

question from:https://stackoverflow.com/questions/65909364/address-protocol-unknown-error-in-pandas-read-json

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try this example to load the data (Using pd.json_normalize. I assume you want to load only values from "T10M" key):

import requests
import pandas as pd


url = 'https://power.larc.nasa.gov/cgi-bin/v1/DataAccess.py?request=execute&identifier=SinglePoint&parameters=T10M&startDate=20200101&endDate=20200103&userCommunity=SSE&tempAverage=DAILY&outputList=JSON,ASCII&lat=-0.2739&lon=36.3765&user=anonymous' 
df = pd.json_normalize(requests.get(url).json()['features'][0]['properties']['parameter']['T10M']).T
df.index = pd.to_datetime(df.index)

print(df)

Prints:

                0
2020-01-01  15.57
2020-01-02  15.70
2020-01-03  16.01

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...