Skip to content

TypeError: Expected label or tuple of labels since switching to 0.19.0 #5651

Closed
@georgbuechner

Description

@georgbuechner

What happened:
Since upgrading to xarray==0.19.0 we are experiencing the following error:
TypeError: Expected label or tuple of labels, got (56.580000000002094, 0.38000000000000383)) (output of MCVE using high_resolution.grib2.bz2)

What you expected to happen:
Using xarray==0.18.2 getting values from grib-files works as expected:
[[[186.25311279296875], [185.88555908203125], [185.44927978515625], [184.66273498535156]]] (output of MCVE using high_resolution.grib2.bz2)

Minimal Complete Verifiable Example:

import bz2      
import tempfile      
import xarray      
from typing import List, Tuple      
                                                                                                    
def extract_grib_values(grib_file, locations: List[Tuple[float, float]]) -> List[List[float]]:       
    # Decompress and write to temp-file, as xarray can only work with file objects.      
    decompressed = bz2.decompress(grib_file)      
    tmp_file = tempfile.NamedTemporaryFile()                                           
    tmp_file.write(decompressed)                                                       
                                                                                       
    # Create backend_kwargs needed to correctly parse the grib-file and load dataset.    
    backend_kwargs = {'indexpath': ''}                                                  
    data_set = xarray.open_dataset(tmp_file.name, engine='cfgrib', backend_kwargs=backend_kwargs)    
                                                                                                     
    # Convert to data-frame, find value keys and extract values using coordinates and value_keys.                        
    data_frame = data_set.to_dataframe()                                                 
    value_keys = list(data_set.keys())                                                   
    values = [data_frame.loc[lat_long, value_keys].values.tolist() for lat_long in locations]    
    return values                                                                                
                                                                                                 
                                                                                                 
if __name__ == '__main__':                                                                         
    with open('high_resolution.grib2.bz2', 'rb') as data:                                         
        values = extract_grib_values(data.read(), [(56.580000000002094, 0.38000000000000383)])    
        print(values)                                                                             

Anything else we need to know?:
Use wget -O high_resolution.grib2.bz2 https://opendata.dwd.de/weather/nwp/icon-d2/grib/09/aswdifd_s/icon-d2_germany_regular-lat-lon_single-level_2021073009_019_2d_aswdifd_s.grib2.bz2
or wget -O low_resolution.grib2.bz2 https://opendata.dwd.de/weather/nwp/icon-d2/grib/09/ps/icon-d2_germany_regular-lat-lon_single-level_2021073009_030_2d_ps.grib2.bz2 to get two example files for testing the above code.

The low-resolution files work with the older and the newer version, only when using the high resolution file(s), we are experiencing the described error.
Environment:

Output of xr.show_versions() commit: None python: 3.9.6 (default, Jun 29 2021, 06:20:32) [Clang 12.0.0 (clang-1200.0.32.29)] python-bits: 64 OS: Darwin OS-release: 19.6.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: None LOCALE: (None, 'UTF-8') libhdf5: None libnetcdf: None

xarray: 0.18.2
pandas: 1.3.1
numpy: 1.21.1
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: 0.9.9.0
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
pint: None
setuptools: 57.0.0
pip: 21.2.1
conda: None
pytest: 6.2.4
IPython: None
sphinx: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions