Given names of one or more variables, set them as coordinates Parameters ---------- names : hashable or iterable of hashable Name(s) of variables in this dataset to convert into coordinates. Examples -------- >>> dataset = xr.Dataset(
(self, names: Hashable | Iterable[Hashable])
| 1809 | return DataVariables(self) |
| 1810 | |
| 1811 | def set_coords(self, names: Hashable | Iterable[Hashable]) -> Self: |
| 1812 | """Given names of one or more variables, set them as coordinates |
| 1813 | |
| 1814 | Parameters |
| 1815 | ---------- |
| 1816 | names : hashable or iterable of hashable |
| 1817 | Name(s) of variables in this dataset to convert into coordinates. |
| 1818 | |
| 1819 | Examples |
| 1820 | -------- |
| 1821 | >>> dataset = xr.Dataset( |
| 1822 | ... { |
| 1823 | ... "pressure": ("time", [1.013, 1.2, 3.5]), |
| 1824 | ... "time": pd.date_range("2023-01-01", periods=3), |
| 1825 | ... } |
| 1826 | ... ) |
| 1827 | >>> dataset |
| 1828 | <xarray.Dataset> Size: 48B |
| 1829 | Dimensions: (time: 3) |
| 1830 | Coordinates: |
| 1831 | * time (time) datetime64[us] 24B 2023-01-01 2023-01-02 2023-01-03 |
| 1832 | Data variables: |
| 1833 | pressure (time) float64 24B 1.013 1.2 3.5 |
| 1834 | |
| 1835 | >>> dataset.set_coords("pressure") |
| 1836 | <xarray.Dataset> Size: 48B |
| 1837 | Dimensions: (time: 3) |
| 1838 | Coordinates: |
| 1839 | * time (time) datetime64[us] 24B 2023-01-01 2023-01-02 2023-01-03 |
| 1840 | pressure (time) float64 24B 1.013 1.2 3.5 |
| 1841 | Data variables: |
| 1842 | *empty* |
| 1843 | |
| 1844 | On calling ``set_coords`` , these data variables are converted to coordinates, as shown in the final dataset. |
| 1845 | |
| 1846 | Returns |
| 1847 | ------- |
| 1848 | Dataset |
| 1849 | |
| 1850 | See Also |
| 1851 | -------- |
| 1852 | Dataset.swap_dims |
| 1853 | Dataset.assign_coords |
| 1854 | """ |
| 1855 | # TODO: allow inserting new coordinates with this method, like |
| 1856 | # DataFrame.set_index? |
| 1857 | # nb. check in self._variables, not self.data_vars to insure that the |
| 1858 | # operation is idempotent |
| 1859 | if isinstance(names, str) or not isinstance(names, Iterable): |
| 1860 | names = [names] |
| 1861 | else: |
| 1862 | names = list(names) |
| 1863 | self._assert_all_in_dataset(names) |
| 1864 | obj = self.copy() |
| 1865 | obj._coord_names.update(names) |
| 1866 | return obj |
| 1867 | |
| 1868 | def reset_coords( |