pydata / pydata/xarray

indexing an array with decreasing time returns array with time dimension even for a single time selected

Open
#5,277 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I believe I have found an additional bug related to non-increasing time (whether it needs to be monotonic I do not know).
I didn't know how to formulate the title in a more concise way, apologies.

Background: My own research data has decreasing time as standard output (back trajectories), but I will present this example using xr's tutorial data. I already found the bug that you can't select a forward going time slice in a backwards going data set. (I can't find the bug report anymore, sorry).

I know the work-around is easy, and it can be discussed if slices with labelled dimensions should support slices in either direction regardless of the direction of the dataarray too.
But that a single selection still returns a time dimension surely is unintended behavior.

What happened:
When selecting a single time step in a decreasing DataArray, the returned DataArray still has a time dimension, of size one.
This makes trouble for different situations that expects 2D data: for instance plot.contourf() which does not reduce the time dimension - as apparently .plot() does.

What you expected to happen:
A returned DataArray with no time dimension.

Minimal Complete Verifiable Example:

ds = xr.tutorial.open_dataset("air_temperature.nc")
print(ds.air.sel(time='2014-12-31T18').shape)
>> (25, 53)
ds=ds.reindex(time=list(reversed(ds.time.values))) #reverse time to show effect
print(ds.air.sel(time='2014-12-31T18').shape)
>> (1, 25, 53)

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.9 (default, Aug 31 2020, 12:42:55)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 4.15.0-91-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.4
libnetcdf: 4.7.3

xarray: 0.16.1
pandas: 1.1.3
numpy: 1.19.1
scipy: 1.5.2
netCDF4: 1.5.3
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.2.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.3.1
cartopy: 0.16.0
seaborn: 0.11.0
numbagg: None
pint: None
setuptools: 50.3.0.post20201006
pip: 20.2.3
conda: None
pytest: None
IPython: 7.18.1
sphinx: 3.2.1

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

Reproduce the behavior with DataArray.sel after reversing the time coordinate using reindex, starting from the selection path involved in .sel. Add a regression test showing that selecting one timestamp from decreasing time returns no time dimension, and verify the tutorial-data example and plot.contourf use case.

Written by the indexing model from the issue text.

Assessment

Tech stack
matplotlib, numpy, pandas, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.