Skip to content

Commit

Permalink
Support drop* and *_coords methods (#145)
Browse files Browse the repository at this point in the history
* implement names and labels

* no need of _get_with_key

* add tests

* fix tests

* add what's new
  • Loading branch information
malmans2 authored Jan 28, 2021
1 parent 674751b commit 1a15e73
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: CI
on:
push:
branches:
- "*"
- "main"
pull_request:
branches:
- "*"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: pre-commit

on:
push:
branches: "*"
branches: "main"
pull_request:
branches:
- "*"
Expand Down
7 changes: 6 additions & 1 deletion cf_xarray/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,12 @@ def _get_with_standard_name(
"dims_dict": (_get_axis_coord,), # swap_dims, rename_dims
"shifts": (_get_axis_coord,), # shift, roll
"pad_width": (_get_axis_coord,), # shift, roll
# "names": something_with_all_valid_keys? # set_coords, reset_coords
"names": (
_get_axis_coord,
_get_measure,
_get_with_standard_name,
), # set_coords, reset_coords, drop_vars
"labels": (_get_axis_coord, _get_measure, _get_with_standard_name), # drop
"coords": (_get_axis_coord,), # interp
"indexers": (_get_axis_coord,), # sel, isel, reindex
# "indexes": (_get_axis_coord,), # set_index
Expand Down
43 changes: 43 additions & 0 deletions cf_xarray/tests/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
import xarray as xr
from matplotlib import pyplot as plt
from xarray import Dataset
from xarray.testing import assert_allclose, assert_identical

import cf_xarray # noqa
Expand Down Expand Up @@ -665,3 +666,45 @@ def test_standard_name_mapper():
actual = da.cf.sortby("standard_label")
expected = da.sortby("label")
assert_identical(actual, expected)


@pytest.mark.parametrize("obj", objects)
@pytest.mark.parametrize("attr", ["drop", "drop_vars", "set_coords"])
@pytest.mark.filterwarnings("ignore:dropping .* using `drop` .* deprecated")
def test_drop_vars_and_set_coords(obj, attr):

# DataArray object has no attribute set_coords
if not isinstance(obj, Dataset) and attr == "set_coords":
return

# Get attribute
expected = getattr(obj, attr)
actual = getattr(obj.cf, attr)

# Axis
assert_identical(expected("lon"), actual("X"))
# Coordinate
assert_identical(expected("lon"), actual("longitude"))
# Cell measure
assert_identical(expected("cell_area"), actual("area"))
# Variables
if isinstance(obj, Dataset):
assert_identical(expected("air"), actual("air_temperature"))
assert_identical(expected(obj.variables), actual(obj.cf.keys()))


@pytest.mark.parametrize("obj", objects)
def test_drop_sel_and_reset_coords(obj):

# Axis
assert_identical(obj.drop_sel(lat=75), obj.cf.drop_sel(Y=75))
# Coordinate
assert_identical(obj.drop_sel(lat=75), obj.cf.drop_sel(latitude=75))

# Cell measure
assert_identical(obj.reset_coords("cell_area"), obj.cf.reset_coords("area"))
# Variable
if isinstance(obj, Dataset):
assert_identical(
obj.reset_coords("air"), obj.cf.reset_coords("air_temperature")
)
1 change: 1 addition & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ What's New
v0.4.1 (unreleased)
===================

- Support for ``.drop()``, ``.drop_vars()``, ``.drop_sel()``, ``.set_coords()``, ``.reset_coords()``. By `Mattia Almansi`_.
- Support for using ``standard_name`` in more functions. (:pr:`128`) By `Deepak Cherian`_
- Allow ``DataArray.cf[]`` with standard names. By `Deepak Cherian`_
- Rewrite the ``values`` of ``.cf.coords`` and ``.cf.data_vars`` with objects returned
Expand Down

0 comments on commit 1a15e73

Please sign in to comment.