pydata / pydata/xarray

Error in DataArray.from_dict(data_array.to_dict()) when using pd.MultiIndex

Open
#4,073 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Error recovering DataArray with from_dict from what was persisted by to_dict when using pandas.MultiIndex.

MCVE Code Sample
import pandas as pd
import xarray as xr
idx = pd.MultiIndex.from_arrays([[1, 2], [3, 4]], names=('one', 'two'))
array = xr.DataArray([0, 1], dims='idx', coords={'idx': idx})
assert array.sel(one=1, two=3) == 0
assert array.sel(one=2, two=4) == 1
array_dict = array.to_dict()
xr.DataArray.from_dict(array_dict)
Expected Output

No error.

Problem Description

ValueError: Could not convert tuple of form (dims, data[, attrs, encoding]): (('idx',), [(1, 3), (2, 4)], {}) to Variable.

Versions

python: 3.7.6
xarray: 0.15.1
pandas: 1.0.3
numpy: 1.18.4
scipy: 1.4.1

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 by running the MCVE against DataArray.to_dict() and DataArray.from_dict() with the pandas.MultiIndex shown. Trace how the serialized coordinate data is passed back into from_dict; done means the round trip completes without ValueError and preserves the indexed values and names.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.