Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subset should always return None if no value matches are found #4417

Merged
merged 4 commits into from
Nov 17, 2021

Conversation

rcomer
Copy link
Member

@rcomer rcomer commented Nov 14, 2021

🚀 Pull Request

Description

Getting a subcube from a cube based on a coordinates values will usually return None if there is no value match. However, if the source coordinate is a vector, subset currently throws an exception:

import iris.cube
import iris.coords


vector_cube = iris.cube.Cube([0, 2], long_name="spam")
coord = iris.coords.DimCoord([0, 1], long_name="eggs")
vector_cube.add_dim_coord(coord, 0)

scalar_cube = vector_cube[0]

constraint = iris.Constraint(eggs=5)
target_coord = iris.coords.DimCoord(5, long_name="eggs")


for cube in [scalar_cube, vector_cube]:
    print(cube.extract(constraint))
    print(cube.subset(target_coord))

With main branch:

None
None
None
Traceback (most recent call last):
  File "extract_subset_nomatch.py", line 17, in <module>
    print(cube.subset(target_coord))
  File "[git-path]/iris/lib/iris/cube.py", line 2588, in subset
    result = self[full_slice]
  File "[git-path]/iris/lib/iris/cube.py", line 2457, in __getitem__
    dimension_mapping, data = iris.util._slice_data_with_keys(
  File "[git-path]/iris/lib/iris/util.py", line 778, in _slice_data_with_keys
    data = data[this_slice]
IndexError: arrays used as indices must be of integer (or boolean) type

This PR modifies subset so that the 4th case will also return None.


Consult Iris pull request check list

@pp-mo pp-mo added the Peloton 🚴‍♂️ Target a breakaway issue to be caught and closed by the peloton label Nov 17, 2021
Copy link
Contributor

@trexfeathers trexfeathers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @rcomer!

@trexfeathers trexfeathers merged commit 32a3bc8 into SciTools:main Nov 17, 2021
@rcomer
Copy link
Member Author

rcomer commented Nov 17, 2021

Thanks @trexfeathers 🎉

@rcomer rcomer deleted the subset-nomatch branch November 17, 2021 17:13
tkknight added a commit to tkknight/iris that referenced this pull request Dec 7, 2021
* main: (23 commits)
  Suggest type hinting (SciTools#4390)
  area weight regrid test fixes (SciTools#4432)
  Update latest.rst (SciTools#4425)
  Added @wjbenfold to the core dev list (SciTools#4423)
  Removed addition of period from wrap_lons. (SciTools#4421)
  Add release docs sections describing the role of a Release Manager (SciTools#4413)
  Subset should always return None if no value matches are found (SciTools#4417)
  What's new for SciTools#4400 (SciTools#4422)
  `iris.analysis.AreaWeighted` regrid speedup (SciTools#4400)
  [pre-commit.ci] pre-commit autoupdate (SciTools#4419)
  Remove newline to satisfy setuptools (SciTools#4418)
  Updated environment lockfiles (SciTools#4416)
  NAME loader fixes (SciTools#4411)
  Updated whatsnew for PR 4402 (SciTools#4410)
  Support test data in benchmark workflows (SciTools#4402)
  What's new for pr 4387 (SciTools#4405)
  Make concat mismatch warning for scalar coords more accurate (SciTools#4387)
  Added line to latest release notes for updates to pp_save_rules.py (SciTools#4404)
  Update pp_save_rules.py (SciTools#4391)
  [pre-commit.ci] pre-commit autoupdate (SciTools#4403)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Peloton 🚴‍♂️ Target a breakaway issue to be caught and closed by the peloton Type: Bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants