12

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?

Yuca
  • 6,010
  • 3
  • 22
  • 42
Y. Peng
  • 324
  • 1
  • 2
  • 7
  • 2
    You have duplicate values in the colum that you use for indexing. So find another column that you can use as an index source, remove duplicates OR add a new column with unique ID and then convert or reindex. – Stacking For Heap Jan 03 '19 at 19:30

3 Answers3

18

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.

SamuelNLP
  • 4,038
  • 9
  • 59
  • 102
shoyer
  • 9,165
  • 1
  • 37
  • 55
8

In this case df.columns.is_unique would return False. To identify which one is repeating you can see the frequency of each column pair by df.columns.value_counts(). For multiindexing to work it should show 1 for all tuples.

Krishna
  • 6,107
  • 2
  • 40
  • 43
0

When you convert csv to netcdf through to_xarray, It is important the arrangement of heads are equal to the arrangement in your cod, otherwise you get the error: cannot handle a non-unique multi-index.

HMadadi
  • 391
  • 5
  • 22