How can I store data from a loop into the appropriate datatype (ie. datetime and float64) and not 'object'?

41 Views Asked by At

I am working on a project in python to create a catalog of 6 hourly vorticity values from a 3D vorticity dataset (time,lat,lon) matching some criteria (in this case include all values greater than 1 for all latitudes and longitudes) and then storing them as well as their dates in a new array that can be accessed and thus aggregated upon. Currently, the issue is that when I do this, the data I have is stored as datetype 'object' and not an array of 'float64' values, and the time values I have are also stored as an 'object' and not datetime, which I would need. I've tried some of the suggested workarounds such as np.astype('float64'), to no avail (I get the error of setting an array element with a sequence). If anyone has any ideas on a solution to this, it would be much appreciated! Thank you, and let me know if there is more context I would need to provide to help!

If I invoke np.array(etc_vort), I obtain an array with the following values and so forth.

[array([1.36806563e-05, 1.29832788e-05, 2.88208697e-05, 5.54436148e-05, 1.39301122e-05, 1.53350643e-05, 1.26050872e-05, 1.86952434e-05, 1.89628889e-05, 1.74795102e-05, 2.84078173e-05, 3.89240365e-05, 1.18466042e-05, 4.35820300e-05, 3.35744917e-05, 4.78875100e-05,

etc_dates_list=[]
time_array=[]
vort_indices=[]
vort_years=np.arange(1980,2022)
for i in vort_years:
    time_oneyear = vort_time.sel(time=slice(str(i)+"-01-01", str(i) +"-12-31")).values
    vort_each_year=vort.sel(time=slice(str(i)+"-01-01",str(i)+"-12-31")).values
    for k in range(0,nlats_vort):
        for j in range(0,nlons_vort):
            vort_of_etc_index=np.asarray(np.where(vort_each_year[:,k,j]>1.0e-5))
            if (vort_of_etc_index.size>0):
                etc_dates= time_oneyear[np.squeeze(vort_of_etc_index)]
                etc_dates_list.append(etc_dates)
                etc_vort.append(vort_each_year[np.squeeze(vort_of_etc_index),k,j])
0

There are 0 best solutions below