This code has been working for me for months and this morning it is throwing the error: TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'
import pandas as pd
import datetime
dt=datetime.datetime.strptime
date_array=[]
for i in range(len(Date)):
date_array.append(dt(Date[i],'%Y-%m-%dT%H:%M:%S%z')) # Data downloaded with obtimezone=local
date_array=n.array(date_array)
# Wire Mountain Dataframe
W_data=pd.DataFrame(data={'Solar':WIRC1},index=date_array)
W_mask=W_data.where(W_data > 0) # Using only daytime data, when solar does not equal 0
W_mean=W_mask.resample('D').mean() #Daily mean
The dataframe looks like this:
Solar
2020-10-25 00:50:00-07:00 0.0
2020-10-25 01:50:00-07:00 0.0
2020-10-25 02:50:00-07:00 0.0
2020-10-25 03:50:00-07:00 0.0
2020-10-25 04:50:00-07:00 0.0
2020-10-25 05:50:00-07:00 0.0
2020-10-25 06:50:00-07:00 0.0
2020-10-25 07:50:00-07:00 2.0
2020-10-25 08:50:00-07:00 49.0
2020-10-25 09:50:00-07:00 116.0
2020-10-25 10:50:00-07:00 155.0
2020-10-25 11:50:00-07:00 233.0
2020-10-25 12:50:00-07:00 363.0
The array I used as an index for the dataframe is python datetime
type(date_array[0])
Out[24]: datetime.datetime
Why did this suddenly stop working? Maybe backend code on Pandas changing? I thought maybe I could change the python datetime index to Pandas using:
date_array=n.array(pd.to_datetime(date_array))
But got:
ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True
I also tried from another Stack Overflow question:
W_mean=W_mask.set_index(date_array).resample('D').mean()
But I got the same error. Thank you for any help you can provide!