pydata / pydata/xarray

Error when adding a DataArray to an existing Dataset with a MultiIndex

Open
#7,921 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What is your issue?

This is a mixture between question, bug (potentially) and general issue, so feel free to label it accordingly.

Here is my question: what is the recommended approach to add a xr.DataArray to an existing xr.Dataset with a MultiIndex?

To give some more context, I've a xarray.Dataset called market with several variables and coordinates, one of them, timeslice, a MultiIndex. This is what it looks like:

<xarray.Dataset>
Dimensions:       (region: 1, commodity: 6, timeslice: 6, year: 8)
Coordinates:
  * region        (region) object 'R1'
  * commodity     (commodity) object 'electricity' 'gas' ... 'CO2f' 'wind'
    units_prices  (commodity) object 'MUS$2010/GWh' ... 'MUS$2010/kt'
  * timeslice     (timeslice) object MultiIndex
  * month         (timeslice) object 'all-year' 'all-year' ... 'all-year'
  * day           (timeslice) object 'all-week' 'all-week' ... 'all-week'
  * hour          (timeslice) object 'night' 'morning' ... 'late-peak' 'evening'
  * year          (year) int64 2020 2025 2030 2035 2040 2045 2050 2055
Data variables:
    prices        (commodity, region, year, timeslice) float64 0.0702 ... 0.0
    exports       (commodity, region, year, timeslice) float64 0.0 0.0 ... 0.0
    imports       (timeslice, commodity, region, year) float64 0.0 0.0 ... 0.0
    static_trade  (timeslice, commodity, region, year) float64 0.0 0.0 ... 0.0

Now, I want to add another variable, called supply, identical to exports but filled with zeros. In a code that was working with xarray==2022.3.0 and pandas==1.4.4, I was simply doing:

market["supply"] = xr.zeros_like(market.exports)

And it worked totally fine. With the newest versions of xarray==2023.5.0 and pandas==2.0.2 under python 3.10, this fails with:

*** DeprecationWarning: Deleting a single level of a MultiIndex is deprecated. Previously, this deleted all levels of a MultiIndex. Please also drop the following variables: {'timeslice'} to avoid an error in the future.

I've tried variants like:

market["supply"] = market.exports * 0
market = market.assign(supply = zeros_like(market.exports))

both failing with the same message.

I totally fail to see how this process is deleting a level of the MultiIndex - or modifying the indexes in any form. Probably it is because I don't understand the inner workings of xarray indexes.

The following works totally fine, but it is rather convoluted having to create a brand new Dataset from scratch manually, in addition to be problematic if you really want to modify the Dataset in place (same problem will have assign).

vars = dict(market.data_vars)
vars["supply"] = xr.zeros_like(market.exports)
market = xr.Dataset(vars)

Resulting in:

<xarray.Dataset>
Dimensions:       (region: 1, commodity: 6, timeslice: 6, year: 8)
Coordinates:
  * region        (region) object 'R1'
  * commodity     (commodity) object 'electricity' 'gas' ... 'CO2f' 'wind'
    units_prices  (commodity) object 'MUS$2010/GWh' ... 'MUS$2010/kt'
  * timeslice     (timeslice) object MultiIndex
  * month         (timeslice) object 'all-year' 'all-year' ... 'all-year'
  * day           (timeslice) object 'all-week' 'all-week' ... 'all-week'
  * hour          (timeslice) object 'night' 'morning' ... 'late-peak' 'evening'
  * year          (year) int64 2020 2025 2030 2035 2040 2045 2050 2055
Data variables:
    prices        (commodity, region, year, timeslice) float64 0.0702 ... 0.0
    exports       (commodity, region, year, timeslice) float64 0.0 0.0 ... 0.0
    imports       (timeslice, commodity, region, year) float64 0.0 0.0 ... 0.0
    static_trade  (timeslice, commodity, region, year) float64 0.0 0.0 ... 0.0
    supply        (commodity, region, year, timeslice) float64 0.0 0.0 ... 0.0

Many thanks for your support!

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 reproducing the assignment with xarray.zeros_like on the displayed Dataset under xarray 2023.5.0, pandas 2.0.2, and Python 3.10, then compare it with the older versions and the manual xr.Dataset construction. Trace the assignment and MultiIndex handling involved in market["supply"] and market.assign; done means adding the zero-filled variable no longer emits the deprecation warning or requires rebuilding the Dataset.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.