E. Burrows
E. Burrows

Reputation: 65

Convert time decimal to datetime object python

I am trying to convert a decimal time to a datetime object to look for the months in order to later divide the time into seasons. I did some research and stumbled upon datetime.datetime.fromtimestamp but everything I have tried produces the following error:

                 TypeError: 'datetime.datetime' object is not iterable

In the past, I have used pandas to create a new time array, but do not feel that is best for my given situation. Currently I have the following in my code and tried doing it without the for loop as well, in hopes that I can get fromtimestamp() to work correctly.

raw_time = (data.variables['time'].getValue()/float(365*24))+1800 #hours since 1800
time = n.where((raw_time>=2006)&(time<=2016)) #our specific time interval

annual_time = []
for i in raw_time[time]:
    annual_time.extend(datetime.datetime.fromtimestamp(i))

My time was read in from as netCDF file and currently appears as follows:

print raw_time[time]
array([ 2006.05205479,  2006.1369863 ,  2006.22191781,  2006.29863014,
    2006.38356164,  2006.46575342,  2006.55068493,  2006.63287671,
    2006.71780822,  2006.80273973,  2006.88493151,  2006.96986301,
    2007.05205479,  2007.1369863 ,  2007.22191781,  2007.29863014,
    2007.38356164,  2007.46575342,  2007.55068493,  2007.63287671,
    2007.71780822,  2007.80273973,  2007.88493151,  2007.96986301,
    2008.05205479,  2008.1369863 ,  2008.22191781,  2008.30136986,
    2008.38630137,  2008.46849315,  2008.55342466,  2008.63561644,
    2008.72054795,  2008.80547945,  2008.88767123,  2008.97260274, ...])

Upvotes: 2

Views: 2653

Answers (2)

N1B4
N1B4

Reputation: 3453

You should use netCDF4 num2date to convert time from numeric values to datetime objects.

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 create a time_convert array of datetime objects that you can then work with to generate seasons, for example.

Upvotes: 2

J Earls
J Earls

Reputation: 1812

The error message is because of your use of extend:

annual_time = []
for i in raw_time[time]:
    annual_time.extend(datetime.datetime.fromtimestamp(i))

list.extend() is used to take another list or iterable and add its contents to the end of the list. datetime does not return a list or iterable; it returns a datetime instance. For scalar values, you need to use append:

annual_time = []
for i in raw_time[time]:
    annual_time.append(datetime.datetime.fromtimestamp(i))

Having said that, I think you will need to manipulate the time values before using this function, because the values you are giving do not look like timestamps (which are a number of seconds past January 1, 1970, 00:00:00 UTC) - unless those times really are supposed to be about half an hour after midnight January 1, 1970...

Upvotes: 1

Related Questions