有一个测验脚本,为最快的回答者提供奖金,然后在时间范围内答对任何答案即可获得 1 分。在最新的 pandas 库最近更新后,对所有受访者应用 1 分的部分不再起作用。这是它的部分代码。
if len(repHolder) > 0:
repHolder.columns = ['name', 'points', 'time']
repHolder = repHolder.sort_values('time').reset_index(drop=True)
repHolder.loc[:, 'time'] = repHolder.loc[:, 'time'].apply(
lambda x: (datetime.utcfromtimestamp(x) + timedelta(hours=-4)).strftime("%I:%M:%S %p"))
# Take the first response per participant
minDate = repHolder.groupby('name').min()['time'].reset_index()
repHolder = repHolder.merge(minDate)
# For ties, score by time not by name
timeHolder = repHolder.loc[repHolder['points'], ['time']].drop_duplicates().reset_index(drop=True)
timeHolder['points'] = 0
corHolder = 0
for idx3, row in enumerate(timeHolder.iterrows()):
if corHolder == 0:
timeHolder.loc[idx3, 'points'] = 1
corHolder = 1
elif corHolder == 1:
timeHolder.loc[idx3, 'points'] = 1
corHolder = 2
elif corHolder == 2:
timeHolder.loc[idx3, 'points'] = 1
corHolder = 3
else:
timeHolder.loc[idx3, 'points'] = 1
repHolder = repHolder.merge(timeHolder, on='time', how='left')
repHolder['points'] = (repHolder['points_x'] * repHolder['points_y'])
repHolder.loc[repHolder['points'].isnull(), 'points'] = 0
print('scored winners')
repHolder = repHolder.sort_values('points', ascending=False)
repHolder['qTime'] = (datetime.utcfromtimestamp(qCom.created_utc) + timedelta(hours=-4)).strftime(
"%I:%M:%S %p")
repHolder['deltaTime'] = pd.to_datetime(repHolder['time']) - pd.to_datetime(repHolder['qTime'])
repHolder['points'] = repHolder['points'] + repHolder[['deltaTime', 'points']]. \
apply(lambda x: 1 if (x['deltaTime'].seconds <= 30) and (x['points'] == 1) else 0, axis=1)
print(repHolder)
winHolder = pd.concat([winHolder, repHolder])
从此更新了 pandas 代码:
if not reply.author == None and not reply.banned_by:
repTab = pd.DataFrame([[reply.author.name, gottem, reply.created_utc]])
repHolder = repHolder.append(repTab)
对此:
if not reply.author == None and not reply.banned_by:
repTab = pd.DataFrame([[reply.author.name, gottem, reply.created_utc]])
repHolder = pd.concat([repTab])
更新后,代码再次运行,但随后代码停止保存所有响应
pd.concat 需要一个数据帧列表来连接。
尝试:
if not reply.author == None and not reply.banned_by:
repTab = pd.DataFrame([[reply.author.name, gottem, reply.created_utc]])
repHolder = pd.concat([repHolder, repTab])