|
18 | 18 | from .common import AbstractArray, BaseDataObject
|
19 | 19 | from .coordinates import (DataArrayCoordinates, LevelCoordinatesSource,
|
20 | 20 | Indexes)
|
21 |
| -from .dataset import Dataset |
| 21 | +from .dataset import Dataset, merge_indexes, split_indexes |
22 | 22 | from .pycompat import iteritems, basestring, OrderedDict, zip, range
|
23 | 23 | from .variable import (as_variable, Variable, as_compatible_data,
|
24 | 24 | IndexVariable,
|
@@ -842,6 +842,103 @@ def swap_dims(self, dims_dict):
|
842 | 842 | ds = self._to_temp_dataset().swap_dims(dims_dict)
|
843 | 843 | return self._from_temp_dataset(ds)
|
844 | 844 |
|
| 845 | + def set_index(self, append=False, inplace=False, **indexes): |
| 846 | + """Set DataArray (multi-)indexes using one or more existing coordinates. |
| 847 | +
|
| 848 | + Parameters |
| 849 | + ---------- |
| 850 | + append : bool, optional |
| 851 | + If True, append the supplied index(es) to the existing index(es). |
| 852 | + Otherwise replace the existing index(es) (default). |
| 853 | + inplace : bool, optional |
| 854 | + If True, set new index(es) in-place. Otherwise, return a new DataArray |
| 855 | + object. |
| 856 | + **indexes : {dim: index, ...} |
| 857 | + Keyword arguments with names matching dimensions and values given |
| 858 | + by (lists of) the names of existing coordinates or variables to set |
| 859 | + as new (multi-)index. |
| 860 | +
|
| 861 | + Returns |
| 862 | + ------- |
| 863 | + obj : DataArray |
| 864 | + Another dataarray, with this dataarray's data but replaced coordinates. |
| 865 | +
|
| 866 | + See Also |
| 867 | + -------- |
| 868 | + DataArray.reset_index |
| 869 | + """ |
| 870 | + coords, _ = merge_indexes(indexes, self._coords, set(), append=append) |
| 871 | + if inplace: |
| 872 | + self._coords = coords |
| 873 | + else: |
| 874 | + return self._replace(coords=coords) |
| 875 | + |
| 876 | + def reset_index(self, dims_or_levels, drop=False, inplace=False): |
| 877 | + """Reset the specified index(es) or multi-index level(s). |
| 878 | +
|
| 879 | + Parameters |
| 880 | + ---------- |
| 881 | + dims_or_levels : str or list |
| 882 | + Name(s) of the dimension(s) and/or multi-index level(s) that will |
| 883 | + be reset. |
| 884 | + drop : bool, optional |
| 885 | + If True, remove the specified indexes and/or multi-index levels |
| 886 | + instead of extracting them as new coordinates (default: False). |
| 887 | + inplace : bool, optional |
| 888 | + If True, modify the dataarray in-place. Otherwise, return a new |
| 889 | + DataArray object. |
| 890 | +
|
| 891 | + Returns |
| 892 | + ------- |
| 893 | + obj : DataArray |
| 894 | + Another dataarray, with this dataarray's data but replaced |
| 895 | + coordinates. |
| 896 | +
|
| 897 | + See Also |
| 898 | + -------- |
| 899 | + DataArray.set_index |
| 900 | + """ |
| 901 | + coords, _ = split_indexes(dims_or_levels, self._coords, set(), |
| 902 | + self._level_coords, drop=drop) |
| 903 | + if inplace: |
| 904 | + self._coords = coords |
| 905 | + else: |
| 906 | + return self._replace(coords=coords) |
| 907 | + |
| 908 | + def reorder_levels(self, inplace=False, **dim_order): |
| 909 | + """Rearrange index levels using input order. |
| 910 | +
|
| 911 | + Parameters |
| 912 | + ---------- |
| 913 | + inplace : bool, optional |
| 914 | + If True, modify the dataarray in-place. Otherwise, return a new |
| 915 | + DataArray object. |
| 916 | + **dim_order : optional |
| 917 | + Keyword arguments with names matching dimensions and values given |
| 918 | + by lists representing new level orders. Every given dimension |
| 919 | + must have a multi-index. |
| 920 | +
|
| 921 | + Returns |
| 922 | + ------- |
| 923 | + obj : DataArray |
| 924 | + Another dataarray, with this dataarray's data but replaced |
| 925 | + coordinates. |
| 926 | + """ |
| 927 | + replace_coords = {} |
| 928 | + for dim, order in dim_order.items(): |
| 929 | + coord = self._coords[dim] |
| 930 | + index = coord.to_index() |
| 931 | + if not isinstance(index, pd.MultiIndex): |
| 932 | + raise ValueError("coordinate %r has no MultiIndex" % dim) |
| 933 | + replace_coords[dim] = IndexVariable(coord.dims, |
| 934 | + index.reorder_levels(order)) |
| 935 | + coords = self._coords.copy() |
| 936 | + coords.update(replace_coords) |
| 937 | + if inplace: |
| 938 | + self._coords = coords |
| 939 | + else: |
| 940 | + return self._replace(coords=coords) |
| 941 | + |
845 | 942 | def stack(self, **dimensions):
|
846 | 943 | """
|
847 | 944 | Stack any number of existing dimensions into a single new dimension.
|
|
0 commit comments