2

我正在尝试使用 xray 库(Python 中的 ND 标记数组和数据集)。我正在使用转置来更改索引顺序,但没有任何变化。

以下代码段访问 NetCDF 文件并分配给 xray 数据集,提取数据子集,创建 Pandas DataFrame,并将结果输出到 CSV 文件。

接下来,对 xray Dataset 的维度进行转置和提取子集、创建 DataFrame 和输出 CSV 的相同过程。结果是一样的。

import pandas as pd
import xray

# access NetCDF over HTTP
ds = xray.open_dataset('http://www.esrl.noaa.gov/psd/thredds/dodsC/Datasets/noaa.oisst.v2/sst.mnmean.nc')

# Extract subset of data  using indexes: time, lat, lon
sst = ds['sst'][133:157, 80:100, 180:260]

# Convert to Dataframe
df = sst.to_dataframe()

# Outut to csv format
df.to_csv('c:/dev/sst1.csv', mode='w')

'''
lat lon time    sst
9.5 180.5   1993-01-01 00:00:00 26.799999401
9.5 180.5   1993-02-01 00:00:00 27.0699993949
9.5 180.5   1993-03-01 00:00:00 27.1199993938
9.5 180.5   1993-04-01 00:00:00 27.379999388
9.5 180.5   1993-05-01 00:00:00 27.8499993775
9.5 180.5   1993-06-01 00:00:00 28.1699993704
9.5 180.5   1993-07-01 00:00:00 28.2799993679
9.5 180.5   1993-08-01 00:00:00 28.7999993563
9.5 180.5   1993-09-01 00:00:00 29.2099993471
9.5 180.5   1993-10-01 00:00:00 29.2199993469
9.5 180.5   1993-11-01 00:00:00 28.7099993583
9.5 180.5   1993-12-01 00:00:00 28.0799993724
9.5 180.5   1994-01-01 00:00:00 27.7999993786
9.5 180.5   1994-02-01 00:00:00 27.649999382
9.5 180.5   1994-03-01 00:00:00 27.7599993795
9.5 180.5   1994-04-01 00:00:00 28.1099993717
9.5 180.5   1994-05-01 00:00:00 28.3799993657
9.5 180.5   1994-06-01 00:00:00 28.3099993672
9.5 180.5   1994-07-01 00:00:00 28.3599993661
9.5 180.5   1994-08-01 00:00:00 29.1899993476
9.5 180.5   1994-09-01 00:00:00 29.6899993364
9.5 180.5   1994-10-01 00:00:00 29.4799993411
9.5 180.5   1994-11-01 00:00:00 29.0999993496
9.5 180.5   1994-12-01 00:00:00 28.4199993648
9.5 181.5   1993-01-01 00:00:00 26.8399994001
9.5 181.5   1993-02-01 00:00:00 27.1399993934
9.5 181.5   1993-03-01 00:00:00 27.1399993934
...
'''

# Transpose dimensions
ds_T = ds.transpose('lon', 'lat', 'time', 'nbnds')

# Extract subset the data  using indexes: lon, lat, time
sst = ds_T['sst'][180:260, 80:100, 133:157]

# Convert to Dataframe
df = sst.to_dataframe()

# Outut to csv format
df.to_csv('c:/dev/sst2.csv', mode='w')

'''
lat lon time    sst
9.5 180.5   1993-01-01 00:00:00 26.799999401
9.5 180.5   1993-02-01 00:00:00 27.0699993949
9.5 180.5   1993-03-01 00:00:00 27.1199993938
9.5 180.5   1993-04-01 00:00:00 27.379999388
9.5 180.5   1993-05-01 00:00:00 27.8499993775
9.5 180.5   1993-06-01 00:00:00 28.1699993704
9.5 180.5   1993-07-01 00:00:00 28.2799993679
9.5 180.5   1993-08-01 00:00:00 28.7999993563
9.5 180.5   1993-09-01 00:00:00 29.2099993471
9.5 180.5   1993-10-01 00:00:00 29.2199993469
9.5 180.5   1993-11-01 00:00:00 28.7099993583
9.5 180.5   1993-12-01 00:00:00 28.0799993724
9.5 180.5   1994-01-01 00:00:00 27.7999993786
9.5 180.5   1994-02-01 00:00:00 27.649999382
9.5 180.5   1994-03-01 00:00:00 27.7599993795
9.5 180.5   1994-04-01 00:00:00 28.1099993717
9.5 180.5   1994-05-01 00:00:00 28.3799993657
9.5 180.5   1994-06-01 00:00:00 28.3099993672
9.5 180.5   1994-07-01 00:00:00 28.3599993661
9.5 180.5   1994-08-01 00:00:00 29.1899993476
9.5 180.5   1994-09-01 00:00:00 29.6899993364
9.5 180.5   1994-10-01 00:00:00 29.4799993411
9.5 180.5   1994-11-01 00:00:00 29.0999993496
9.5 180.5   1994-12-01 00:00:00 28.4199993648
9.5 181.5   1993-01-01 00:00:00 26.8399994001
9.5 181.5   1993-02-01 00:00:00 27.1399993934
9.5 181.5   1993-03-01 00:00:00 27.1399993934
...
'''
4

1 回答 1

2

我已经在 xray 邮件列表上回答了这个问题,但简而言之,这是一个错误:https ://github.com/xray/xray/issues/260

它已在最新的 xray 版本 (0.3.1) 中修复。

于 2014-11-05T01:08:24.470 回答