Skip to content

Commit 1a15e73

Browse files
authored
Support drop* and *_coords methods (#145)
* implement names and labels * no need of _get_with_key * add tests * fix tests * add what's new
1 parent 674751b commit 1a15e73

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: CI
22
on:
33
push:
44
branches:
5-
- "*"
5+
- "main"
66
pull_request:
77
branches:
88
- "*"

.github/workflows/pre-commit.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: pre-commit
22

33
on:
44
push:
5-
branches: "*"
5+
branches: "main"
66
pull_request:
77
branches:
88
- "*"

cf_xarray/accessor.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,12 @@ def _get_with_standard_name(
376376
"dims_dict": (_get_axis_coord,), # swap_dims, rename_dims
377377
"shifts": (_get_axis_coord,), # shift, roll
378378
"pad_width": (_get_axis_coord,), # shift, roll
379-
# "names": something_with_all_valid_keys? # set_coords, reset_coords
379+
"names": (
380+
_get_axis_coord,
381+
_get_measure,
382+
_get_with_standard_name,
383+
), # set_coords, reset_coords, drop_vars
384+
"labels": (_get_axis_coord, _get_measure, _get_with_standard_name), # drop
380385
"coords": (_get_axis_coord,), # interp
381386
"indexers": (_get_axis_coord,), # sel, isel, reindex
382387
# "indexes": (_get_axis_coord,), # set_index

cf_xarray/tests/test_accessor.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
import xarray as xr
66
from matplotlib import pyplot as plt
7+
from xarray import Dataset
78
from xarray.testing import assert_allclose, assert_identical
89

910
import cf_xarray # noqa
@@ -665,3 +666,45 @@ def test_standard_name_mapper():
665666
actual = da.cf.sortby("standard_label")
666667
expected = da.sortby("label")
667668
assert_identical(actual, expected)
669+
670+
671+
@pytest.mark.parametrize("obj", objects)
672+
@pytest.mark.parametrize("attr", ["drop", "drop_vars", "set_coords"])
673+
@pytest.mark.filterwarnings("ignore:dropping .* using `drop` .* deprecated")
674+
def test_drop_vars_and_set_coords(obj, attr):
675+
676+
# DataArray object has no attribute set_coords
677+
if not isinstance(obj, Dataset) and attr == "set_coords":
678+
return
679+
680+
# Get attribute
681+
expected = getattr(obj, attr)
682+
actual = getattr(obj.cf, attr)
683+
684+
# Axis
685+
assert_identical(expected("lon"), actual("X"))
686+
# Coordinate
687+
assert_identical(expected("lon"), actual("longitude"))
688+
# Cell measure
689+
assert_identical(expected("cell_area"), actual("area"))
690+
# Variables
691+
if isinstance(obj, Dataset):
692+
assert_identical(expected("air"), actual("air_temperature"))
693+
assert_identical(expected(obj.variables), actual(obj.cf.keys()))
694+
695+
696+
@pytest.mark.parametrize("obj", objects)
697+
def test_drop_sel_and_reset_coords(obj):
698+
699+
# Axis
700+
assert_identical(obj.drop_sel(lat=75), obj.cf.drop_sel(Y=75))
701+
# Coordinate
702+
assert_identical(obj.drop_sel(lat=75), obj.cf.drop_sel(latitude=75))
703+
704+
# Cell measure
705+
assert_identical(obj.reset_coords("cell_area"), obj.cf.reset_coords("area"))
706+
# Variable
707+
if isinstance(obj, Dataset):
708+
assert_identical(
709+
obj.reset_coords("air"), obj.cf.reset_coords("air_temperature")
710+
)

doc/whats-new.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ What's New
66
v0.4.1 (unreleased)
77
===================
88

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

0 commit comments

Comments
 (0)