I have global datasets in netcdf file. Time information on data file is:
<type 'netCDF4._netCDF4.Variable'>
int32 time(time)
units: hours since 1900-01-01 00:00:0.0
long_name: time
calendar: gregorian
unlimited dimensions: time
current shape = (5875,)
filling off
when I extracted time from file, I got this array:
array([ 876600, 876624, 876648, ..., 1017528, 1017552, 1017576], dtype=int32)
my question is how to convert this array into proper date format? [Note: This is a daily data sets and number in an array is corresponding to an hours from 1900-01-01]
You could:
from datetime import date, timedelta
hours = [ 876600, 876624, 876648, 1017528, 1017552, 1017576]
base = date(1900, 1, 1)
for hour in hours:
base + timedelta(hours=hour)
2000-01-02
2000-01-03
2000-01-04
2016-01-30
2016-01-31
2016-02-01
Use datetime
instead of date
if you want hour
etc info.
Or using a pd.DataFrame
:
df = pd.DataFrame(hours, columns=['hours'])
df['date'] = df.hours.apply(lambda x: base + timedelta(hours=x))
hours date
0 876600 2000-01-02
1 876624 2000-01-03
2 876648 2000-01-04
3 1017528 2016-01-30
4 1017552 2016-01-31
5 1017576 2016-02-01
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With