Deltares / ddlpy

API to Dutch Rijkswaterstaat archive (DDL, waterinfo.rws.nl) of monitoring water data
https://deltares.github.io/ddlpy/
GNU General Public License v3.0
20 stars 6 forks source link

TypeError: string indices must be integers #13

Closed pgraafstra closed 2 years ago

pgraafstra commented 2 years ago

Description

Trying to download waterlevel data. Using a script we previously had that worked for us, and also reproduced the error with the 'minimal_example' provided. Consistently getting a type error: TypeError: string indices must be integers

What I Did

from datetime import datetime
import ddlpy

code= 'WATHTE'
unit= 'NAP'
RWS_station= ['HOUN']   # Houtrib Noord HOUN
locations = ddlpy.locations()
selected= locations[locations.index.isin(RWS_station)]
selected = selected[(selected['Grootheid.Code'] == code) & (selected['Hoedanigheid.Code'] == unit) ].reset_index()
location = selected.loc[0]
start = datetime.strptime('2021-07-29', '%Y-%m-%d')
end = datetime.strptime('2021-08-29', '%Y-%m-%d')
measurements = ddlpy.measurements(location=location, start_date=start, end_date=end)

Traceback (most recent call last):

  File "C:\Users\pga\AppData\Local\Temp/ipykernel_8596/861100223.py", line 21, in <module>
    measurements = ddlpy.measurements(location, start_date=start_date, end_date=end_date)

  File "C:\ProgramFiles\Anaconda3\envs\215101\lib\site-packages\ddlpy\ddlpy.py", line 163, in measurements
    measurement = _measurements_slice(location, start_date=start_date_i, end_date=end_date_i)

  File "C:\ProgramFiles\Anaconda3\envs\215101\lib\site-packages\ddlpy\ddlpy.py", line 134, in _measurements_slice
    new_row[key+'.code']= result['WaarnemingenLijst'][i]['AquoMetadata'][key]['Code']

TypeError: string indices must be integers
anneverheijen commented 2 years ago

I encountered the same error. Apparently, the metadata dict can be a string. I only encountered it to be "NVT". I fixed it with the following lines of code. Not sure if this is a desired and long term solution, since I only discovered ddlpy an hour ago :) Great module!

# add meta data
for key in list(result['WaarnemingenLijst'][i]['AquoMetadata'].keys())[2:]:
    meta_dict = result['WaarnemingenLijst'][i]['AquoMetadata'][key]
    if isinstance(meta_dict, str) and meta_dict.lower() == 'nvt':
        code = 'nvt'
        omschrijving = 'nvt'
    else:
        code = meta_dict['Code']
        omschrijving = meta_dict['Omschrijving']
    new_row[key+'.code']= code
    new_row[key+'.Omschrijving']= omschrijving
isabelbrandao commented 2 years ago

Hi pgraafstra, how did you install ddlpy? I tried pip install ddlpy but I get an error:

ERROR: Could not find a version that satisfies the requirement ddlpy (from versions: none) ERROR: No matching distribution found for ddlpy

Cheers,

Isabel

  • Data Distributie Laag. Service from Rijkswaterstaat for distributing water quantity data. version:
  • Python version: 3.9
  • Operating System: Win10 Pro, 64 bit

Description

Trying to download waterlevel data. Using a script we previously had that worked for us, and also reproduced the error with the 'minimal_example' provided. Consistently getting a type error: TypeError: string indices must be integers

What I Did

from datetime import datetime
import ddlpy

code= 'WATHTE'
unit= 'NAP'
RWS_station= ['HOUN']   # Houtrib Noord HOUN
locations = ddlpy.locations()
selected= locations[locations.index.isin(RWS_station)]
selected = selected[(selected['Grootheid.Code'] == code) & (selected['Hoedanigheid.Code'] == unit) ].reset_index()
location = selected.loc[0]
start = datetime.strptime('2021-07-29', '%Y-%m-%d')
end = datetime.strptime('2021-08-29', '%Y-%m-%d')
measurements = ddlpy.measurements(location=location, start_date=start, end_date=end)

Traceback (most recent call last):

  File "C:\Users\pga\AppData\Local\Temp/ipykernel_8596/861100223.py", line 21, in <module>
    measurements = ddlpy.measurements(location, start_date=start_date, end_date=end_date)

  File "C:\ProgramFiles\Anaconda3\envs\215101\lib\site-packages\ddlpy\ddlpy.py", line 163, in measurements
    measurement = _measurements_slice(location, start_date=start_date_i, end_date=end_date_i)

  File "C:\ProgramFiles\Anaconda3\envs\215101\lib\site-packages\ddlpy\ddlpy.py", line 134, in _measurements_slice
    new_row[key+'.code']= result['WaarnemingenLijst'][i]['AquoMetadata'][key]['Code']

TypeError: string indices must be integers
SiggyF commented 2 years ago

Thanks for your contribution. This issue is fixed in the latest master branch.

Note that you can also install ddlpy from the github using, after checkout: pip install -e .

isabelbrandao commented 2 years ago

It is working SiggyF!

Thanks, Isabel

veenstrajelmer commented 7 months ago

Another release of ddlpy was created today and all docs and examples were updated. Please open a new issue if you encounter any bugs.