Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does this mean? xarray error: cannot handle a non-unique multi-index

I am trying to convert a dataframe to xarray. The head is like this:

z   Class    DA       x          y          iline      xline      idz                                                      
2     651   289  1455.0        2.0        0.62239  2345322.0  76720.0
            290  1460.0        0.0        0.46037  2345322.0  76720.0
            291  1465.0        4.0        0.41280  2345322.0  76720.0
            292  1470.0        0.0        0.39540  2345322.0  76720.0
            293  1475.0        2.0        0.61809  2345322.0  76720.0

when I use xr.DataSet.from_dataframe, or df.to_xarray, I got the following error message:

cannot handle a non-unique multi-index!

Anybody know what is going on here?

like image 622
Y. Peng Avatar asked Jan 03 '19 19:01

Y. Peng


People also ask

How do I get rid of MultiIndex in pandas?

To drop multiple levels from a multi-level column index, use the columns. droplevel() repeatedly.

Does pandas support non unique index?

From ndarray If data is an ndarray, index must be the same length as data. If no index is passed, one will be created having values [0, ..., len(data) - 1] . pandas supports non-unique index values. If an operation that does not support duplicate index values is attempted, an exception will be raised at that time.

What is MultiIndex Dataframe?

In this article, we will discuss Multi-index for Pandas Dataframe and Groupby operations . Multi-index allows you to select more than one row and column in your index. It is a multi-level or hierarchical object for pandas object. Now there are various methods of multi-index that are used such as MultiIndex.


1 Answers

The multi-index of your data frame has duplicate entries, which xarray cannot unstack into a multi-dimensional array -- the elements of the hypothetical arrays would not have unique values.

You need to remove the duplicated entries in the index first, e.g., as described in Remove pandas rows with duplicate indices:

  • The simplest choice would be to drop duplicates, e.g., df[~df.index.duplicated()]
  • You might also use a groupby operation, e.g., to compute the mean: df.groupby(level=df.index.names).mean()

Once you've done this, you can safely convert the dataframe into xarray.

like image 52
shoyer Avatar answered Sep 23 '22 02:09

shoyer