因此,这是参考先前的问题,对该问题的扩展。
I want to Iterate through an xlsx, using Pandas, containing timestamps and get downtime
因此,我实现了一个字典,其中包含停机时间的时间戳记,其日期为键。 但是现在出现的问题是,如果一天中有两个单独的停机时间,而不是单独输入该条目,它会附加在停机时间列表中,例如,显示为
Timestamp('2019-10-18 00:00:00')":['00:20:00','00:30:00','00:20:00','00:40:00','05:50:00','05:60:00']
因此,为了达到目的,我从该条目中提取了第一个和最后一个元素,以获取特定日期停机时间的开始和结束时间,然后给出总小时数。
我能够将它们分为两个不同的字典,我使用了这个:
df1=pd.DataFrame.from_dict(result, orient='index')
print(df)
df1=df1.fillna('0')
df1=df1.replace(to_replace =0,value ='0')
for i in df1.index:
print(i)
for j in range(len(df1.loc[i])-3):
if (df1.loc[i][j+1] is not '0' and df1.loc[i][j] is not '0'):
#the error is faced over here is, we have a total of 72 72 rows, however not all of the are filled for all the timestamps, hence they remain as NoneType, thus need to be ignored.
x=(datetime.datetime.strptime(df1.loc[i][j+1],"%H:%M:%S"))-(datetime.datetime.strptime(df1.loc[i][j],"%H:%M:%S"))
if(x>datetime.timedelta(seconds=600)):
print(df1.loc[i][j]," ",df1.loc[i][j+1])
print(i,"fixed")
#this gives us the complete appended dictionary with two new entries however we are missing the Date column for these
z=list(df1.loc[i][:j])
y=list(df1.loc[i][j+1:])
z={i:z}
y={i:y}
df2=pd.DataFrame.from_dict(z, orient='index')
df3=pd.DataFrame.from_dict(y, orient='index')
df1=df1.drop(i)
df1 = pd.concat([df2, df1], ignore_index=False,sort=False)
df1 = pd.concat([df3, df1], ignore_index=False,sort=False)
df1=df1.fillna(0)
df1=df1.replace(to_replace ='0',value =0)
break
else:
break
因此,在处理之后,我得到了以结果名称存储的字典。 在此之后我得到的错误是:
Traceback (most recent call last):
File "path", line 85, in <module>
x=(datetime.datetime.strptime(df1.loc[i][j+1],"%H:%M:%S"))-(datetime.datetime.strptime(df1.loc[i][j],"%H:%M:%S"))
TypeError: strptime() argument 1 must be str, not int
如您所见,我已将if分配指定为不超出非零的elemts,但仍然会发生此错误。