Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a netCDF file with 2544 time step (daily, 06-08-2014 to 24-07-2021), I want to find the time step corresponding to the date 25-Aug-2018

What I have tried:

import netCDF4
from netCDF4 import num2date, date2num, date2index
import numpy as np

f=netCDF4.Dataset('/../t.cdf')
times=f.variables['time']
dates = num2date(times[:], times.units)
print(dates.index(datetime.dates(2018, 8, 25)))
Posted
Updated 6-Oct-22 22:30pm
Comments
Richard MacCutchan 7-Oct-22 4:05am    
And? What happens when you run this code?
Swetha sivakumar 7-Oct-22 4:10am    
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'index'
Richard MacCutchan 7-Oct-22 4:20am    
That does not correspond to the code you have posted above.

1 solution

As stated in the error message there is no index attribute. You most likely meant to use item. See numpy.ndarray — NumPy v1.23 Manual[^].
 
Share this answer
 
Comments
Maciej Los 10-Oct-22 16:08pm    
5ed!
Richard MacCutchan 11-Oct-22 3:44am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900