[英]Python datetime still gives "TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'"
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'这段代码已经为我工作了几个月,今天早上它抛出错误: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我用作数据帧索引的数组是 python datetime
type(date_array[0])
Out[24]: datetime.datetime
Why did this suddenly stop working?为什么这突然停止工作? Maybe backend code on Pandas changing?
也许 Pandas 上的后端代码发生了变化? I thought maybe I could change the python datetime index to Pandas using:
我想也许我可以使用以下方法将 python 日期时间索引更改为 Pandas:
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:我还尝试了另一个 Stack Overflow 问题:
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!
感谢您提供任何帮助!
The "something" that changed was the local time- from daylight savings to standard.改变的“东西”是当地时间——从夏令时到标准时间。 From this similar issue ,
从这个类似的问题,
A pandas datetime column also requires the offset to be the same.
Pandas 日期时间列也要求偏移量相同。 A column with different offsets, will not be converted to a datetime dtype.
具有不同偏移量的列不会转换为日期时间数据类型。 I suggest, do not convert the data to a datetime until it's in pandas.
我建议,不要将数据转换为日期时间,直到它在 Pandas 中。
My data had two offsets, as shown below:我的数据有两个偏移量,如下所示:
Date[0]
Out[34]: '2020-10-25T00:50:00-0700'
Date[-1]
Out[35]: '2020-11-07T22:50:00-0800'
Because of the two different offsets, the dates were not being converted to a datetime dtype.由于两个不同的偏移量,日期没有被转换为 datetime dtype。
I pulled the data in UTC instead of local time, then as suggested, I did not convert to datetime until the date column was in Pandas.我以 UTC 而不是本地时间提取数据,然后按照建议,直到日期列在 Pandas 中,我才转换为日期时间。 After adding the conversion to US/Pacific time, Pandas handled the time change seamlessly.
添加到美国/太平洋时间的转换后,Pandas 无缝处理了时间变化。
import pandas as pd
Date=n.genfromtxt('WIRC1.txt',delimiter=',',skip_header=8,usecols=1,dtype=str)
W_data=pd.DataFrame(data={'Solar':WIRC1},index=pd.to_datetime(Date).tz_convert('US/Pacific'))
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.