utils.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:faampy 作者: ncasuk 项目源码 文件源码
def core_to_pandas(ds):
    """converts a netCDF4.Dataset into a pandas Dataframe using the timestamp
    as index.

    ..note: Only the first column of the two dimensional data set is grabbed,
            because of performance issues.

    :param ds: core_faam dataset
    :type param: netCDF4.Dataset
    :return: pandas.Dataframe
    :type return: pandas.Dataframe

    """

    # TODO: make it work for the full dataset too
    vars=sorted(ds.variables.keys())
    vars.remove('Time')

    index=get_mpl_time(ds, 1)
    index=num2date(index.ravel())
    #initialize an empty Dataframe
    df=pd.DataFrame(index=index)
    for v in vars:
        shp=ds.variables[v].shape
        if len(shp) ==2:
            data=np.copy(ds.variables[v][:,0].data).ravel()
        else:
            data=np.copy(ds.variables[v][:].data)
        df_tmp=pd.DataFrame(data[:], index=index, columns=[v,])
        df = pd.concat([df, df_tmp], axis=1)
    #set all missing values to nan
    df[df == -9999.0] = np.nan
    #set timezone to None otherwise there might be issues merging the data
    #frame with others    
    df.index.tz=None
    return df
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号