【发布时间】:2018-12-24 05:11:55
【问题描述】:
我正在尝试将 netcdf (*.nc)(经典格式)转换为 CSV。该文件取自 NOAA 降水数据集。
我在this post 中找到了有用的代码;但是,当我运行它时,我得到了这个异常:
Traceback(最近一次调用最后一次):文件“./test2.py”,第 31 行,在 precip_ts = pd.Series(precip, index=dtime) 文件“/usr/local/lib/python2.7/site-packages/pandas/core/series.py”,行 275,在 初始化 raise_cast_failure=True) 文件“/usr/local/lib/python2.7/site-packages/pandas/core/series.py”,行 第4165章 raise Exception('Data must be 1-dimensional') Exception: Data must be 1-dimensional
这是 test2.py 脚本(与上面引用的帖子相同):
#!/usr/local/bin/python2.7
import netCDF4
import pandas as pd
precip_nc_file = 'precip.V1.0.2006.nc'
nc = netCDF4.Dataset(precip_nc_file, mode='r')
nc.variables.keys()
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
time_var = nc.variables['time']
dtime = netCDF4.num2date(time_var[:],time_var.units)
precip = nc.variables['precip'][:]
# a pandas.Series designed for time series of a 2D lat,lon grid
precip_ts = pd.Series(precip, index=dtime)
precip_ts.to_csv('precip.csv',index=True, header=True)
它在 Pandas 系列电话会议上失败了。你能给我任何指导为什么熊猫会失败吗?我认为它应该处理 2D 数据!
我正在寻找的最终结果是一个 CSV 文件,其中每一行都有 lon、lat、datetime、precip 值
【问题讨论】:
标签: python python-2.7 pandas netcdf4