Skip to content

Commit 1e1871c

Browse files
committed
Add Dataset.drop_dims()
* Drops full dimensions and any corresponding variables in a Dataset
1 parent 17fa64f commit 1e1871c

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

xarray/core/dataset.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2797,6 +2797,41 @@ def _drop_vars(self, names):
27972797
coord_names = set(k for k in self._coord_names if k in variables)
27982798
return self._replace_vars_and_dims(variables, coord_names)
27992799

2800+
def drop_dims(self, drop_dims, inplace=False):
2801+
"""Drop dimensions from a Dataset. Any data variables containing
2802+
these dimensions will also be dropped.
2803+
2804+
Parameters
2805+
----------
2806+
drop_dims : str or list
2807+
Dimension or dimensions to drop.
2808+
inplace : bool, optional
2809+
If True, modify this dataset inplace. Otherwise, create a new
2810+
object.
2811+
2812+
Returns
2813+
-------
2814+
obj : Dataset
2815+
A dataset without the given dimensions (or any variables
2816+
containing those dimensions)
2817+
"""
2818+
if utils.is_scalar(drop_dims):
2819+
drop_dims = [drop_dims]
2820+
2821+
missing_dimensions = [d for d in drop_dims if d not in self.dims]
2822+
if missing_dimensions:
2823+
raise ValueError('Dataset does not contain the dimensions: %s'
2824+
% missing_dimensions)
2825+
2826+
drop_vars = set(k for k, v in self._variables.items()
2827+
for d in v.dims if d in drop_dims)
2828+
2829+
variables = OrderedDict((k, v) for k, v in self._variables.items()
2830+
if k not in drop_vars)
2831+
coord_names = set(k for k in self._coord_names if k in variables)
2832+
2833+
return self._replace_with_new_dims(variables, coord_names, inplace=inplace)
2834+
28002835
def transpose(self, *dims):
28012836
"""Return a new Dataset object with all array dimensions transposed.
28022837

xarray/tests/test_dataset.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,41 @@ def test_drop_index_labels(self):
18631863
ValueError, 'does not have coordinate labels'):
18641864
data.drop(1, 'y')
18651865

1866+
def test_drop_dims(self):
1867+
data = xr.Dataset({'A': (['x', 'y'], np.random.randn(2, 3)),
1868+
'B': ('x', np.random.randn(2)),
1869+
'x': ['a', 'b'], 'z': np.pi})
1870+
1871+
actual = data.drop_dims('x')
1872+
expected = data.drop(['A','B','x'])
1873+
assert_identical(expected, actual)
1874+
1875+
actual = data.drop_dims('y')
1876+
expected = data.drop('A')
1877+
assert_identical(expected, actual)
1878+
1879+
actual = data.drop_dims(['x','y'])
1880+
expected = data.drop(['A','B','x'])
1881+
assert_identical(expected, actual)
1882+
1883+
actual = data.copy()
1884+
actual.drop_dims('y', inplace=True)
1885+
expected = data.drop('A')
1886+
assert_identical(expected, actual)
1887+
1888+
actual = data.copy()
1889+
actual.drop_dims('x', inplace=True)
1890+
expected = data.drop(['A','B','x'])
1891+
assert_identical(expected, actual)
1892+
1893+
actual = data.copy()
1894+
actual.drop_dims(['x','y'], inplace=True)
1895+
expected = data.drop(['A','B','x'])
1896+
assert_identical(expected, actual)
1897+
1898+
with pytest.raises((ValueError, KeyError)):
1899+
data.drop_dims('z') # not a dimension
1900+
18661901
def test_copy(self):
18671902
data = create_test_data()
18681903

0 commit comments

Comments
 (0)