用另一个 Pandas 系列覆盖 Python Pandas 系列,按时间戳索引
Overwriting a Python Pandas series with another Pandas series, indexed by timestamps
我有一个名为“模板”的系列,它是 3600 美元 0.00 美元(最后一小时每秒 0.00 美元,并按此编入索引)。我有另一个名为“交易”的系列,它来自 API 请求的数据,该请求是可变数量的非零美元金额,也由时间戳索引。由于这些交易都是在最后一小时内进行的,因此“交易”中的所有时间戳都将在“模板”中找到,但反之亦然(除非最后一小时的每一秒都有交易)。
对于“模板”中与“交易”中的时间戳相匹配的每个时间戳,我想将 0.00 美元替换为“交易”中的美元金额。我这样做是为了生成过去一小时的交易图,因此需要 0.00 美元而不是 NaN,以及 3600 个数据点(以保持图表时间轴的一致性)。
我认为肯定是这么简单:
graphSeries = template.update(transactions)
但是当我运行
print(template)
print(transactions)
print(graphSeries)
我明白了(问题最后的“None”):
12-26-2021 21:30:39 0.0
12-26-2021 21:30:40 0.0
12-26-2021 21:30:41 0.0
12-26-2021 21:30:42 0.0
12-26-2021 21:30:43 0.0
...
12-26-2021 22:30:34 0.0
12-26-2021 22:30:35 0.0
12-26-2021 22:30:36 0.0
12-26-2021 22:30:37 0.0
12-26-2021 22:30:38 0.0
Length: 3600, dtype: float64
12-26-2021 21:31:10 697986.00
12-26-2021 21:32:07 8780106.00
12-26-2021 21:32:54 900542.00
12-26-2021 21:34:52 8784483.00
12-26-2021 21:35:05 26309692.00
12-26-2021 21:39:48 500000.00
12-26-2021 21:39:55 1999940.00
12-26-2021 21:41:54 1189745.40
12-26-2021 21:43:21 1399102.56
12-26-2021 21:44:33 1607717.00
12-26-2021 21:46:13 1189745.40
12-26-2021 21:47:56 1000000.00
12-26-2021 21:49:08 12414533.00
12-26-2021 21:50:21 1546034.00
12-26-2021 21:50:30 13141109.60
12-26-2021 21:50:33 2277359.00
12-26-2021 21:50:39 503834.00
12-26-2021 21:50:51 1397967.00
12-26-2021 21:51:03 580000.00
12-26-2021 21:51:44 542158.00
12-26-2021 21:52:12 1546034.00
12-26-2021 21:54:04 1662123.80
12-26-2021 21:54:31 12052320.00
12-26-2021 21:54:52 1546024.00
12-26-2021 21:55:53 1000000.00
12-26-2021 21:56:30 1507000.00
dtype: float64
None
我是不是做错了什么? pandas' update(Series) 方法是否不使用时间戳进行索引?
更新是一个就地函数,尝试:
template.update(transactions)
print(template)
关于 update
的更多信息:https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.update.html
我有一个名为“模板”的系列,它是 3600 美元 0.00 美元(最后一小时每秒 0.00 美元,并按此编入索引)。我有另一个名为“交易”的系列,它来自 API 请求的数据,该请求是可变数量的非零美元金额,也由时间戳索引。由于这些交易都是在最后一小时内进行的,因此“交易”中的所有时间戳都将在“模板”中找到,但反之亦然(除非最后一小时的每一秒都有交易)。
对于“模板”中与“交易”中的时间戳相匹配的每个时间戳,我想将 0.00 美元替换为“交易”中的美元金额。我这样做是为了生成过去一小时的交易图,因此需要 0.00 美元而不是 NaN,以及 3600 个数据点(以保持图表时间轴的一致性)。
我认为肯定是这么简单:
graphSeries = template.update(transactions)
但是当我运行
print(template)
print(transactions)
print(graphSeries)
我明白了(问题最后的“None”):
12-26-2021 21:30:39 0.0
12-26-2021 21:30:40 0.0
12-26-2021 21:30:41 0.0
12-26-2021 21:30:42 0.0
12-26-2021 21:30:43 0.0
...
12-26-2021 22:30:34 0.0
12-26-2021 22:30:35 0.0
12-26-2021 22:30:36 0.0
12-26-2021 22:30:37 0.0
12-26-2021 22:30:38 0.0
Length: 3600, dtype: float64
12-26-2021 21:31:10 697986.00
12-26-2021 21:32:07 8780106.00
12-26-2021 21:32:54 900542.00
12-26-2021 21:34:52 8784483.00
12-26-2021 21:35:05 26309692.00
12-26-2021 21:39:48 500000.00
12-26-2021 21:39:55 1999940.00
12-26-2021 21:41:54 1189745.40
12-26-2021 21:43:21 1399102.56
12-26-2021 21:44:33 1607717.00
12-26-2021 21:46:13 1189745.40
12-26-2021 21:47:56 1000000.00
12-26-2021 21:49:08 12414533.00
12-26-2021 21:50:21 1546034.00
12-26-2021 21:50:30 13141109.60
12-26-2021 21:50:33 2277359.00
12-26-2021 21:50:39 503834.00
12-26-2021 21:50:51 1397967.00
12-26-2021 21:51:03 580000.00
12-26-2021 21:51:44 542158.00
12-26-2021 21:52:12 1546034.00
12-26-2021 21:54:04 1662123.80
12-26-2021 21:54:31 12052320.00
12-26-2021 21:54:52 1546024.00
12-26-2021 21:55:53 1000000.00
12-26-2021 21:56:30 1507000.00
dtype: float64
None
我是不是做错了什么? pandas' update(Series) 方法是否不使用时间戳进行索引?
更新是一个就地函数,尝试:
template.update(transactions)
print(template)
关于 update
的更多信息:https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.update.html