Python: convert 'days since 1990' to datetime object

前端 未结 3 1620
长情又很酷
长情又很酷 2021-02-20 02:04

I have a time series that I have pulled from a netCDF file and I\'m trying to convert them to a datetime format. The format of the time series is in \'days since 1990-01-01 00:0

3条回答
  •  -上瘾入骨i
    2021-02-20 02:26

    netCDF num2date is the correct function to use here:

    import netCDF4
    
    ncfile = netCDF4.Dataset('./foo.nc', 'r')
    time = ncfile.variables['time'] # do not cast to numpy array yet 
    time_convert = netCDF4.num2date(time[:], time.units, time.calendar)
    

    This will convert number of days since 1900-01-01 (i.e. the units of time) to python datetime objects. If time does not have a calendar attribute, you'll need to specify the calendar, or use the default of standard.

提交回复
热议问题