作为较大数据集的一部分,我从 API 调用中返回了以下内容:
{'时间': datetime.datetime(2017, 5, 21, 18, 18, 1, tzinfo=tzutc()), '价格': '0.052600'}
{'时间': datetime.datetime(2017, 5, 21, 18, 18, 1, tzinfo=tzutc()), '价格': '0.052500'}
理想情况下,我会使用时间戳作为 Pandas 数据框的索引,但是这似乎失败了,因为在转换为 JSON 时有重复:
df = df.set_index(pd.to_datetime(df['Timestamp']))
print(new_df.to_json(orient='index'))
Run Code Online (Sandbox Code Playgroud)
ValueError:对于 orient='index',DataFrame 索引必须是唯一的。
有关处理这种情况的最佳方法的任何指导?扔掉一个数据点?时间并没有比秒更细粒度,并且在那一秒内显然有价格变化。
我有以下数据帧:
U_ID Group Location Hours People Date
149 17 USA 2 2 2014-11-03
149 17 USA 2 1 2014-11-07
149 21 USA 3 2 2014-12-21
149 18 UK 1.5 1 2014-11-14
149 19 Spain 2 4 2014-11-21
Run Code Online (Sandbox Code Playgroud)
我可以使用以下代码段汇总小时数:
def process_hours(hr_df):
hr_df['Date'] = pd.to_datetime(hr_df['Date'])
hr_df['Hours'] = pd.to_numeric(hr_df['Hours'])
hr_df = (vol_df.groupby(['U_ID', 'Group', 'Location', 'People', pd.Grouper(key='Date', freq='MS')])['Hours'].sum().reset_index(level=[0, 1, 2, 3]))
Run Code Online (Sandbox Code Playgroud)
然而,当人数不同时,这会分裂:
U_ID Group Location People Hours
Date
2014-11-01 149 17 USA 1 2.0
2014-11-01 149 17 USA 2 2.0
2014-11-01 149 18 UK …Run Code Online (Sandbox Code Playgroud) 我在通过 python 请求向 NEST API 发送带有 Bearer 的授权令牌时遇到问题:
curl https://developer-api.nest.com -H "Authorization: Bearer c.123"
-H "Content-Type: application/json"
Run Code Online (Sandbox Code Playgroud)
工作正常,但是:
nest_url = "https://developer-api.nest.com"
headers = {'Authorization': str('Bearer ' + token), 'Content-type': 'application/json'}
print(headers)
nest_data_req = requests.get(nest_url, headers=headers)
Run Code Online (Sandbox Code Playgroud)
打印为:
{'内容类型':'应用程序/json','授权':'承载c.123'}
因 401 未授权而失败,据我所知,他们正在尝试发出相同的请求,那么为什么 curl 工作(以及邮递员)和 python 请求失败?
下图显示了在邮递员中的相同工作:
所以这段代码有 10 次有效(其他 9 次以上给我 401 未授权):
url = "https://developer-api.nest.com/"
auth_t = token.encode("ascii", "ignore")
headers = {
'authorization': "Bearer " + auth_t,
'content-type': "application/json",
'cache-control': "no-cache",
}
response = requests.request("GET", url, headers=headers)
print(response.text)
Run Code Online (Sandbox Code Playgroud)
如果我在邮递员中按提交,它每次都可以正常工作。