@@ -5741,16 +5741,17 @@ def _assert_all_in_dataset(
5741
5741
5742
5742
def drop_vars (
5743
5743
self ,
5744
- names : Hashable | Iterable [Hashable ],
5744
+ names : str | Iterable [ Hashable ] | Callable [[ Self ], str | Iterable [Hashable ] ],
5745
5745
* ,
5746
5746
errors : ErrorOptions = "raise" ,
5747
5747
) -> Self :
5748
5748
"""Drop variables from this dataset.
5749
5749
5750
5750
Parameters
5751
5751
----------
5752
- names : hashable or iterable of hashable
5753
- Name(s) of variables to drop.
5752
+ names : Hashable or iterable of Hashable or Callable
5753
+ Name(s) of variables to drop. If a Callable, this object is passed as its
5754
+ only argument and its result is used.
5754
5755
errors : {"raise", "ignore"}, default: "raise"
5755
5756
If 'raise', raises a ValueError error if any of the variable
5756
5757
passed are not in the dataset. If 'ignore', any given names that are in the
@@ -5792,7 +5793,7 @@ def drop_vars(
5792
5793
humidity (time, latitude, longitude) float64 65.0 63.8 58.2 59.6
5793
5794
wind_speed (time, latitude, longitude) float64 10.2 8.5 12.1 9.8
5794
5795
5795
- # Drop the 'humidity' variable
5796
+ Drop the 'humidity' variable
5796
5797
5797
5798
>>> dataset.drop_vars(["humidity"])
5798
5799
<xarray.Dataset>
@@ -5805,7 +5806,7 @@ def drop_vars(
5805
5806
temperature (time, latitude, longitude) float64 25.5 26.3 27.1 28.0
5806
5807
wind_speed (time, latitude, longitude) float64 10.2 8.5 12.1 9.8
5807
5808
5808
- # Drop the 'humidity', 'temperature' variables
5809
+ Drop the 'humidity', 'temperature' variables
5809
5810
5810
5811
>>> dataset.drop_vars(["humidity", "temperature"])
5811
5812
<xarray.Dataset>
@@ -5817,7 +5818,18 @@ def drop_vars(
5817
5818
Data variables:
5818
5819
wind_speed (time, latitude, longitude) float64 10.2 8.5 12.1 9.8
5819
5820
5820
- # Attempt to drop non-existent variable with errors="ignore"
5821
+ Drop all indexes
5822
+
5823
+ >>> dataset.drop_vars(lambda x: x.indexes)
5824
+ <xarray.Dataset>
5825
+ Dimensions: (time: 1, latitude: 2, longitude: 2)
5826
+ Dimensions without coordinates: time, latitude, longitude
5827
+ Data variables:
5828
+ temperature (time, latitude, longitude) float64 25.5 26.3 27.1 28.0
5829
+ humidity (time, latitude, longitude) float64 65.0 63.8 58.2 59.6
5830
+ wind_speed (time, latitude, longitude) float64 10.2 8.5 12.1 9.8
5831
+
5832
+ Attempt to drop non-existent variable with errors="ignore"
5821
5833
5822
5834
>>> dataset.drop_vars(["pressure"], errors="ignore")
5823
5835
<xarray.Dataset>
@@ -5831,7 +5843,7 @@ def drop_vars(
5831
5843
humidity (time, latitude, longitude) float64 65.0 63.8 58.2 59.6
5832
5844
wind_speed (time, latitude, longitude) float64 10.2 8.5 12.1 9.8
5833
5845
5834
- # Attempt to drop non-existent variable with errors="raise"
5846
+ Attempt to drop non-existent variable with errors="raise"
5835
5847
5836
5848
>>> dataset.drop_vars(["pressure"], errors="raise")
5837
5849
Traceback (most recent call last):
@@ -5851,36 +5863,38 @@ def drop_vars(
5851
5863
DataArray.drop_vars
5852
5864
5853
5865
"""
5866
+ if callable (names ):
5867
+ names = names (self )
5854
5868
# the Iterable check is required for mypy
5855
5869
if is_scalar (names ) or not isinstance (names , Iterable ):
5856
- names = {names }
5870
+ names_set = {names }
5857
5871
else :
5858
- names = set (names )
5872
+ names_set = set (names )
5859
5873
if errors == "raise" :
5860
- self ._assert_all_in_dataset (names )
5874
+ self ._assert_all_in_dataset (names_set )
5861
5875
5862
5876
# GH6505
5863
5877
other_names = set ()
5864
- for var in names :
5878
+ for var in names_set :
5865
5879
maybe_midx = self ._indexes .get (var , None )
5866
5880
if isinstance (maybe_midx , PandasMultiIndex ):
5867
5881
idx_coord_names = set (maybe_midx .index .names + [maybe_midx .dim ])
5868
- idx_other_names = idx_coord_names - set (names )
5882
+ idx_other_names = idx_coord_names - set (names_set )
5869
5883
other_names .update (idx_other_names )
5870
5884
if other_names :
5871
- names |= set (other_names )
5885
+ names_set |= set (other_names )
5872
5886
warnings .warn (
5873
5887
f"Deleting a single level of a MultiIndex is deprecated. Previously, this deleted all levels of a MultiIndex. "
5874
5888
f"Please also drop the following variables: { other_names !r} to avoid an error in the future." ,
5875
5889
DeprecationWarning ,
5876
5890
stacklevel = 2 ,
5877
5891
)
5878
5892
5879
- assert_no_index_corrupted (self .xindexes , names )
5893
+ assert_no_index_corrupted (self .xindexes , names_set )
5880
5894
5881
- variables = {k : v for k , v in self ._variables .items () if k not in names }
5895
+ variables = {k : v for k , v in self ._variables .items () if k not in names_set }
5882
5896
coord_names = {k for k in self ._coord_names if k in variables }
5883
- indexes = {k : v for k , v in self ._indexes .items () if k not in names }
5897
+ indexes = {k : v for k , v in self ._indexes .items () if k not in names_set }
5884
5898
return self ._replace_with_new_dims (
5885
5899
variables , coord_names = coord_names , indexes = indexes
5886
5900
)
@@ -5978,6 +5992,9 @@ def drop(
5978
5992
"dropping variables using `drop` is deprecated; use drop_vars." ,
5979
5993
DeprecationWarning ,
5980
5994
)
5995
+ # for mypy
5996
+ if is_scalar (labels ):
5997
+ labels = [labels ]
5981
5998
return self .drop_vars (labels , errors = errors )
5982
5999
if dim is not None :
5983
6000
warnings .warn (
0 commit comments