Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp in Numpy

I am trying to extract data from a netcdf file using wrf-python. The data is for every hour. The date is being extracted as a number, and not a calendar-date-time. First I extract the data, convert it to a flat np array, then try to save the file. The format is saved as '%s'

np.savetxt((stn + 'WRF_T2_T10_WS_WD.csv'), np.transpose(arr2D), %s, delimiter=',', header=headers, comments='')

it looks like this: it looks like this

but it needs to look like this: but it needs to look like this

Thanks

like image 692
Alyce Sala Tenna Avatar asked Nov 07 '25 11:11

Alyce Sala Tenna


1 Answers

By convention, dates are frequently stored as an offset in seconds from Jan 1, 1970

For the case of converting seconds, this answer Python Numpy Loadtxt - Convert unix timestamp suggests converting them by changing their datatype (should be as efficient as possible as it dodges by-row loops, copying data, etc.)

x = np.asarray(x, dtype='datetime64[s]')

However, the E+18 postfix implies that if you really have a date, your timestamps are in nanoseconds, so datetime64[ns] may work for you

import time
import numpy as np
>>> a = np.array([time.time() * 10**9])  # epoch seconds to ns
>>> a  # example array
array([1.60473147e+18])
>>> a = np.asarray(a, dtype='datetime64[ns]')
>>> a
array(['2020-11-07T06:44:29.714103040'], dtype='datetime64[ns]')
like image 198
ti7 Avatar answered Nov 09 '25 07:11

ti7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!