python-3.x 以年为单位阅读netcdf时间

oyxsuwqo  于 2023-01-03  发布在  Python
关注(0)|答案(1)|浏览(116)

所有,我正在尝试从伯克利地球读取以下温度文件中的时间坐标,时间跨度从1850年到2022年,时间单位是公元年(1850.041667,1850.125,1850.208333,...,2022.708333,2022.791667,2022.875)。
pandas.to_datetime无法正确解释时间数组,因为我认为我需要声明时间坐标的原点和单位。我尝试使用pd.to_datetime(dti,unit='D',origin='julian’),但它不起作用(超出界限)。此外,我认为我必须使用年单位而不是天。
该文件位于http://berkeleyearth.lbl.gov/auto/Global/Gridded/Land_and_Ocean_LatLong1.nc

import xarray as xr
import numpy as np
import pandas as pd  
# read data into memory
flname="Land_and_Ocean_LatLon1.nc"
ds = xr.open_dataset("./"+flname)
dti = ds['time']
pd.to_datetime(dti,unit='D',origin='julian')
np.diff(dti)
nx7onnlm

nx7onnlm1#

使用%Y作为解析指令转换为datetime,以仅获取年份,然后添加小数年作为天数的timedelta。注意,在计算timedelta时,您可能必须考虑闰年。例如:

import pandas as pd

dti = pd.to_datetime(ds['time'], format="%Y")

# it might be sufficient to use e.g. 365 or 365.25 here, depending on the input
daysinyear = pd.Series([366]*dti.size).where(dti.is_leap_year, 365)

dti = dti + pd.to_timedelta(daysinyear * (ds['time']-ds['time'].astype(int)), unit="d")

dti
0      1850-01-16 04:59:59.999971200
1      1850-02-15 15:00:00.000000000
2      1850-03-18 01:00:00.000028800
3      1850-04-17 10:59:59.999971200
4      1850-05-17 21:00:00.000000000
            
2070   2022-07-17 16:59:59.999971200
2071   2022-08-17 03:00:00.000000000
2072   2022-09-16 13:00:00.000028800
2073   2022-10-16 22:59:59.999971200
2074   2022-11-16 09:00:00.000000000
Length: 2075, dtype: datetime64[ns]

相关问题