Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upto_dataframe: no valid index for a 0-dimensional object #4228
Comments
|
You need
The difference is using
while using
And I am not sure that there is a sensible way to convert a scalar DataArray to a DataFrame but we should throw a more informative error in any case. |
|
thanks for the very clear response. The behaviro make sense. In fact, I should have explained what I'm trying to achieve, as this is kind of "take". I've a dict like this: {'label1' : dict(coord1=1, coord2=4),
'label2' : dict(coord1=5, coord2=6),
'label3' : dict(coord1=4, coord2=2),
}and I want to build an xarray (and then a dataframe) with coord1 and coord2 replaced by a new dims with values 'label1', 'label2', 'label3'. I've done that by iterating over the dict, selecting with sel using the dict values, convert to dataframe and then concat the dataframes. pd.concat([x.sel(**d[k]).to_dataframe() or k in d] A better option would be to do this "sel" or "take" with xarray only. |
|
You could do it with "advanced indexing" by providing a dataarray to the da = xr.DataArray([[1, 2, 3], [4,5,6]], dims=["coord1", "coord2"], coords={"coord2": [10, 20, 30], "coord1": [1,2]})
i1 = xr.DataArray([1, 0], dims=["z"], coords={"z": ["label1", "label2"]})
i2 = xr.DataArray([2, 1], dims=["z"], coords={"z": ["label1", "label2"]})
da.isel(coord1=i1, coord2=i2, drop=True).to_dataframe(name="asd")
|


What happened:
xr.DataArray([1], coords=[('onecoord', [2])]).sel(onecoord=2).to_dataframe(name='name')raise an exceptionValueError: no valid index for a 0-dimensional objectWhat you expected to happen:
the same behavior as:
xr.DataArray([1], coords=[('onecoord', [2])]).to_dataframe(name='name')Anything else we need to know?:
I see that the array after the selection has no "dims" anymore, and this is what cause the error. but it still has one "coords", this is confusing. Is there any documentation about this difference ?
Environment:
xarray: 0.15.1
pandas: 1.0.4
numpy: 1.18.5
scipy: 1.4.1
netCDF4: 1.5.3
pydap: None
h5netcdf: None
h5py: 2.10.0
Nio: None
zarr: 2.4.0
cftime: 1.1.3
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.2
dask: 2.18.1
distributed: 2.18.0
matplotlib: 3.2.1
cartopy: None
seaborn: 0.10.1
numbagg: None
setuptools: 47.3.1.post20200616
pip: 20.1.1
conda: 4.8.3
pytest: 5.4.3
IPython: 7.15.0
sphinx: 3.1.1