pydata / pydata/xarray

to_dataframe fails if dataarray has dimension 1

Open
#3,761 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

The to_dataframe method fails with ValueError if the dataarray has only value

MCVE Code Sample
# Your code here
x = np.arange(10)
y = np.arange(10)

data = np.zeros((len(x), len(y)))

da = xr.DataArray(data, coords=[x, y], dims=['x', 'y'])

da.sel(x=1,y=1).to_dataframe(name='test')
Expected Output

Expect a dataframe with one row

Problem Description

This happened when selecting a single value out of a gridded dataset - in cases where there was only one value output the to_dataframe failed.

Output of xr.show_versions()
# Paste the output here xr.show_versions() here

INSTALLED VERSIONS

commit: None
python: 3.7.6 | packaged by conda-forge | (default, Jan 7 2020, 22:33:48)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 5.3.0-28-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.5
libnetcdf: 4.7.1

xarray: 0.14.1
pandas: 0.25.3
numpy: 1.17.5
scipy: 1.4.1
netCDF4: 1.5.3
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.0.4.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.1.0
cfgrib: 0.9.7.6
iris: None
bottleneck: 1.3.1
dask: 2.9.2
distributed: 2.9.3
matplotlib: 3.1.2
cartopy: 0.17.0
seaborn: 0.9.0
numbagg: None
setuptools: 45.1.0.post20200119
pip: 20.0.1
conda: None
pytest: None
IPython: 7.11.1
sphinx: None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the DataArray.to_dataframe entry point and reproduce the provided MCVE, focusing on the scalar DataArray produced by selecting x=1 and y=1. Add regression coverage for this case and verify that the result is a one-row dataframe without raising ValueError.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.