数据结构
DataArray
DataArray是xarray对带标签的多维数组的实现。
它有几个关键属性:
- values:保存数组值的numpy.ndarray或类似numpy的数组
- dims:每个轴的尺寸名称(例如,(’ x ‘,’ y ‘,’ z '))
- coords:一个类似dict的数组(坐标)容器,用来标记每个点(例如,一维数组的数字、日期对象或字符串)
- attrs:保存任意元数据(属性)的dict
Creating a DataArray
DataArray构造函数采用:
- 数据:值的多维数组(例如,numpy ndarray、类似numpy的数组、Series、DataFrame或pandas。面板)
- 坐标表:坐标表或坐标字典。如果是列表,应该是元组列表,其中第一个元素是维度名称,第二个元素是对应的坐标array_like对象。
- dims:维度名称列表。如果省略,并且coords是元组列表,则维度名称取自coords。
- attrs:要添加到实例的属性字典
- name:命名实例的字符串
import xarray as xr
import numpy as np
import pandas as pd
data = np.random.rand(4, 3)
locs = ["IA", "IL", "IN"]
times = pd.date_range("2000-01-01", periods=4)
foo = xr.DataArray(data, coords=[times, locs], dims=["time", "space"])
print(foo)
"""
<xarray.DataArray (time: 4, space: 3)>
array([[0.35200563, 0.569955 , 0.98816514],
[0.95311654, 0.8268097 , 0.15125512],
[0.6361332 , 0.35353079, 0.33760348],
[0.28808098, 0.19150729, 0.79793025]])
Coordinates:
* time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
* space (space) <U2 'IA' 'IL' 'IN'
"""
DataArray properties
import xarray as xr
import numpy as np
import pandas as pd
data = np.random.rand(4, 3)
locs = ["IA", "IL", "IN"]
times = pd.date_range("2000-01-01", periods=4)
foo = xr.DataArray(data, coords=[times, locs], dims=["time1", "space1"])
print(foo.values)
print(foo.dims)
print(foo.coords)
print(foo.attrs)
print(foo.name)
"""
[[0.90006724 0.52320969 0.54733588]
[0.1018204 0.65948629 0.52749082]
[0.73632638 0.50559387 0.57415316]
[0.24010929 0.52222784 0.39407124]]
('time1', 'space1')
Coordinates:
* time1 (time1) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
* space1 (space1) <U2 'IA' 'IL' 'IN'
{}
None
"""
foo.name = "foo"
foo.attrs["units"] = "meters"
foo
"""
<xarray.DataArray 'foo' (time: 4, space: 3)>
array([[0.127, 0.967, 0.26 ],
[0.897, 0.377, 0.336],
[0.451, 0.84 , 0.123],
[0.543, 0.373, 0.448]])
Coordinates:
* time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
* space (space) <U2 'IA' 'IL' 'IN'
Attributes:
units: meters
"""
foo.rename("bar")
"""
<xarray.DataArray 'bar' (time: 4, space: 3)>
array([[0.127, 0.967, 0.26 ],
[0.897, 0.377, 0.336],
[0.451, 0.84 , 0.123],
[0.543, 0.373, 0.448]])
Coordinates:
* time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
* space (space) <U2 'IA' 'IL' 'IN'
Attributes:
units: meters
"""
DataArray Coordinates
foo.coords["time"]
"""
<xarray.DataArray 'time' (time: 4)>
array(['2000-01-01T00:00:00.000000000', '2000-01-02T00:00:00.000000000',
'2000-01-03T00:00:00.000000000', '2000-01-04T00:00:00.000000000'],
dtype='datetime64[ns]')
Coordinates:
*