pandas.DataFrame.to_xarray#

DataFrame.to_xarray()[源代码]#

从 pandas 对象返回一个 xarray 对象。

返回:
xarray.DataArray 或 xarray.Dataset

如果对象是 DataFrame,则 pandas 结构中的数据转换为 Dataset;如果对象是 Series,则转换为 DataArray。

参见

DataFrame.to_hdf

将 DataFrame 写入 HDF5 文件。

DataFrame.to_parquet

将 DataFrame 写入二进制 parquet 格式。

注释

请参阅 xarray 文档

例子

>>> df = pd.DataFrame(
...     [
...         ("falcon", "bird", 389.0, 2),
...         ("parrot", "bird", 24.0, 2),
...         ("lion", "mammal", 80.5, 4),
...         ("monkey", "mammal", np.nan, 4),
...     ],
...     columns=["name", "class", "max_speed", "num_legs"],
... )
>>> df
     name   class  max_speed  num_legs
0  falcon    bird      389.0         2
1  parrot    bird       24.0         2
2    lion  mammal       80.5         4
3  monkey  mammal        NaN         4
>>> df.to_xarray()  
<xarray.Dataset>
Dimensions:    (index: 4)
Coordinates:
  * index      (index) int64 32B 0 1 2 3
Data variables:
    name       (index) object 32B 'falcon' 'parrot' 'lion' 'monkey'
    class      (index) object 32B 'bird' 'bird' 'mammal' 'mammal'
    max_speed  (index) float64 32B 389.0 24.0 80.5 nan
    num_legs   (index) int64 32B 2 2 4 4
>>> df["max_speed"].to_xarray()  
<xarray.DataArray 'max_speed' (index: 4)>
array([389. ,  24. ,  80.5,   nan])
Coordinates:
  * index    (index) int64 0 1 2 3
>>> dates = pd.to_datetime(
...     ["2018-01-01", "2018-01-01", "2018-01-02", "2018-01-02"]
... )
>>> df_multiindex = pd.DataFrame(
...     {
...         "date": dates,
...         "animal": ["falcon", "parrot", "falcon", "parrot"],
...         "speed": [350, 18, 361, 15],
...     }
... )
>>> df_multiindex = df_multiindex.set_index(["date", "animal"])
>>> df_multiindex
                   speed
date       animal
2018-01-01 falcon    350
           parrot     18
2018-01-02 falcon    361
           parrot     15
>>> df_multiindex.to_xarray()  
<xarray.Dataset>
Dimensions:  (date: 2, animal: 2)
Coordinates:
  * date     (date) datetime64[s] 2018-01-01 2018-01-02
  * animal   (animal) object 'falcon' 'parrot'
Data variables:
    speed    (date, animal) int64 350 18 361 15