I wonder if you need all this conversion work. With the right time units a datetime64 can produce a datetime object directly.
I'm not sure about your when variable, but let's assume it comes from pandas, and is something like a DatetimeIndex:
In [56]: time = pandas.date_range('6/28/2013', periods=5, freq='5D')
In [57]: time
Out[57]:
DatetimeIndex(['2013-06-28', '2013-07-03', '2013-07-08', '2013-07-13',
'2013-07-18'],
dtype='datetime64[ns]', freq='5D')
The equivalent numpy array
In [58]: time.values
Out[58]:
array(['2013-06-28T00:00:00.000000000', '2013-07-03T00:00:00.000000000',
'2013-07-08T00:00:00.000000000', '2013-07-13T00:00:00.000000000',
'2013-07-18T00:00:00.000000000'], dtype='datetime64[ns]')
In [59]: time.values.tolist()
Out[59]:
[1372377600000000000,
1372809600000000000,
1373241600000000000,
1373673600000000000,
1374105600000000000]
With [ns] the result is a large integer, a 'timestamp' of some sort. But if I convert the time units to something like seconds, or even microseconds (us):
In [60]: time.values.astype('datetime64[s]')
Out[60]:
array(['2013-06-28T00:00:00', '2013-07-03T00:00:00',
'2013-07-08T00:00:00', '2013-07-13T00:00:00',
'2013-07-18T00:00:00'], dtype='datetime64[s]')
In [61]: time.values.astype('datetime64[s]').tolist()
Out[61]:
[datetime.datetime(2013, 6, 28, 0, 0),
datetime.datetime(2013, 7, 3, 0, 0),
datetime.datetime(2013, 7, 8, 0, 0),
datetime.datetime(2013, 7, 13, 0, 0),
datetime.datetime(2013, 7, 18, 0, 0)]
the result is a list of datetime objects.