Skip to content
Advertisement

ValueError: cannot convert a DataFrame with a non-unique MultiIndex into xarray

These are the data I want to convert which are saved in CSV. And some of the longitude and latitude may are repeated, actually, they are extracted from a NetCDF file.

JavaScript

So I want to convert them to xarray because I need the ‘pr’ to be 2D(with no repeated long or lat) like the following one.

JavaScript

Here is my code:

JavaScript

df is like this

JavaScript

And then when I use df.to_xarray() I got the errorValueError: cannot convert a DataFrame with a non-unique MultiIndex into xarray

What should I do ? Thanks for answering!

Advertisement

Answer

As your error says, you have a non-unique index. This causes a problem in xarray because you are potentially sending contradictory data to it. Each longitude and latitude should have a unique value. So you either need to drop duplicates, or average the values in each lon/lat. The following will work if you simply have duplicates:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement