home / github / issues

Menu
  • GraphQL API
  • Search all tables

issues: 1758106607

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
1758106607 I_kwDOAMm_X85oypPv 7921 Error when adding a DataArray to an existing Dataset with a MultiIndex 6095790 closed 0     10 2023-06-15T06:08:17Z 2023-09-06T06:25:45Z 2023-08-29T14:23:32Z NONE      

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:

python 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:

python 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).

python 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!

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/7921/reactions",
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  completed 13221727 issue

Links from other tables

  • 2 rows from issues_id in issues_labels
  • 0 rows from issue in issue_comments
Powered by Datasette · Queries took 0.915ms · About: xarray-datasette