Skip to content

mean returns empty DataArray for groupby_bins containing datetime64 #6995

Open
@observingClouds

Description

@observingClouds

What happened?

Applying the mean operator to an xarray dataset that has been grouped by bins, removes variables containing values of type datetime64.

What did you expect to happen?

I expect that all variables are preserved after applying the mean-operator. For min and max this works as expected. sum is not meaningful, so it makes sense that the variable is dropped in that case as well.

Minimal Complete Verifiable Example

import numpy as np
import pandas as pd
import xarray as xr

times = pd.date_range('2020-01-01', '2020-02-01', freq='1H')
index = np.arange(len(times))
bins = np.arange(0,len(index),5)
ds=xr.Dataset({'time':('index', times),'float':('index', np.linspace(0,1,len(index)))},coords={'index':index})

ds.groupby_bins('index', bins).mean()
<xarray.Dataset>
Dimensions:     (index_bins: 148)
Coordinates:
  * index_bins  (index_bins) object (0, 5] (5, 10] ... (730, 735] (735, 740]
Data variables:
    float       (index_bins) float64 0.004032 0.01075 0.01747 ... 0.9852 0.9919

ds.groupby_bins('index', bins).min()
<xarray.Dataset>
Dimensions:     (index_bins: 148)
Coordinates:
  * index_bins  (index_bins) object (0, 5] (5, 10] ... (730, 735] (735, 740]
Data variables:
    time        (index_bins) datetime64[ns] 2020-01-01T01:00:00 ... 2020-01-3...
    float       (index_bins) float64 0.001344 0.008065 0.01478 ... 0.9825 0.9892

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.

Relevant log output

No response

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS ------------------ commit: None python: 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:43:44) [Clang 13.0.1 ] python-bits: 64 OS: Darwin OS-release: 21.6.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: None LOCALE: ('en_US', 'UTF-8') libhdf5: None libnetcdf: None

xarray: 2022.6.0
pandas: 1.4.4
numpy: 1.23.2
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.5.3
cartopy: None
seaborn: None
numbagg: None
fsspec: 2022.8.2
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 65.3.0
pip: 22.2.2
conda: None
pytest: None
IPython: 8.4.0
sphinx: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions