MCPcopy
hub / github.com/pydata/xarray / rename

Method rename

xarray/core/dataarray.py:2565–2607  ·  view source on GitHub ↗

Returns a new DataArray with renamed coordinates, dimensions or a new name. Parameters ---------- new_name_or_name_dict : str or dict-like, optional If the argument is dict-like, it used as a mapping from old names to new names for coordinates or dime

(
        self,
        new_name_or_name_dict: Hashable | Mapping[Any, Hashable] | None = None,
        **names: Hashable,
    )

Source from the content-addressed store, hash-verified

2563 return self._from_temp_dataset(ds)
2564
2565 def rename(
2566 self,
2567 new_name_or_name_dict: Hashable | Mapping[Any, Hashable] | None = None,
2568 **names: Hashable,
2569 ) -> Self:
2570 """Returns a new DataArray with renamed coordinates, dimensions or a new name.
2571
2572 Parameters
2573 ----------
2574 new_name_or_name_dict : str or dict-like, optional
2575 If the argument is dict-like, it used as a mapping from old
2576 names to new names for coordinates or dimensions. Otherwise,
2577 use the argument as the new name for this array.
2578 **names : Hashable, optional
2579 The keyword arguments form of a mapping from old names to
2580 new names for coordinates or dimensions.
2581 One of new_name_or_name_dict or names must be provided.
2582
2583 Returns
2584 -------
2585 renamed : DataArray
2586 Renamed array or array with renamed coordinates.
2587
2588 See Also
2589 --------
2590 Dataset.rename
2591 DataArray.swap_dims
2592 """
2593 if new_name_or_name_dict is None and not names:
2594 # change name to None?
2595 return self._replace(name=None)
2596 if utils.is_dict_like(new_name_or_name_dict) or new_name_or_name_dict is None:
2597 # change dims/coords
2598 name_dict = either_dict_or_kwargs(new_name_or_name_dict, names, "rename")
2599 dataset = self._to_temp_dataset()._rename(name_dict)
2600 return self._from_temp_dataset(dataset)
2601 if utils.hashable(new_name_or_name_dict) and names:
2602 # change name + dims/coords
2603 dataset = self._to_temp_dataset()._rename(names)
2604 dataarray = self._from_temp_dataset(dataset)
2605 return dataarray._replace(name=new_name_or_name_dict)
2606 # only change name
2607 return self._replace(name=new_name_or_name_dict)
2608
2609 def swap_dims(
2610 self,

Callers 9

test_where_attrsFunction · 0.95
test_to_dataarrayMethod · 0.95
test_fillnaMethod · 0.95
test_renameMethod · 0.95
test_initMethod · 0.95

Calls 5

_replaceMethod · 0.95
_to_temp_datasetMethod · 0.95
_from_temp_datasetMethod · 0.95
either_dict_or_kwargsFunction · 0.90
_renameMethod · 0.80

Tested by 9

test_where_attrsFunction · 0.76
test_to_dataarrayMethod · 0.76
test_fillnaMethod · 0.76
test_renameMethod · 0.76
test_initMethod · 0.76