Python 熊猫:从列表和forloop创建日期时间索引
如果我写得正确(并且理解了),下面的代码将遍历一个列表,并用每个日期值创建一个datetime索引 下面是它的外观:Python 熊猫:从列表和forloop创建日期时间索引,python,pandas,Python,Pandas,如果我写得正确(并且理解了),下面的代码将遍历一个列表,并用每个日期值创建一个datetime索引 下面是它的外观: for k in range(len(prediction_times)): prediction_index = pd.DatetimeIndex(start=prediction_times[k]) print(prediction_index) 下面是预测时间的样子: ['2020-06-15', '2020-06-
for k in range(len(prediction_times)):
prediction_index = pd.DatetimeIndex(start=prediction_times[k])
print(prediction_index)
下面是预测时间的样子:
['2020-06-15', '2020-06-15', '2020-06-15', '2020-06-15']
这将产生以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-183-bb5c7db96287> in <module>
6 predictor.set_prediction_parameters(freq, prediction_length)
7
----> 8 list_of_df = predictor.predict(time_series_training[:60])
9 actual_data = time_series[:5]
10
<ipython-input-182-08c8653decf6> in predict(self, ts, cat, encoding, num_samples, quantiles)
31 req = self.__encode_request(ts, cat, encoding, num_samples, quantiles)
32 res = super(DeepARPredictor, self).predict(req)
---> 33 return self.__decode_response(res, prediction_times, encoding)
34
35 def __encode_request(self, ts, cat, encoding, num_samples, quantiles):
<ipython-input-182-08c8653decf6> in __decode_response(self, response, prediction_times, encoding)
48 for k in range(len(prediction_times)):
49
---> 50 prediction_index = pd.DatetimeIndex(data=prediction_times[k])
51 #print([prediction_index[k]])
52
~/anaconda3/envs/python3/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py in __new__(cls, data, freq, tz, normalize, closed, ambiguous, dayfirst, yearfirst, dtype, copy, name)
235 if is_scalar(data):
236 raise TypeError(
--> 237 f"{cls.__name__}() must be called with a "
238 f"collection of some kind, {repr(data)} was passed"
239 )
TypeError: DatetimeIndex() must be called with a collection of some kind, '2020-06-15' was passed
以下是整个forloop:
def __decode_response(self, response, prediction_times, encoding):
response_data = json.loads(response.decode(encoding))
list_of_df = []
for k in range(len(prediction_times)):
prediction_index = pd.DatetimeIndex(data=prediction_times[k])
list_of_df.append(pd.DataFrame(data=response_data['predictions'][k]['quantiles'],index=prediction_index))
return list_of_df
除了所有其他主题相同的问题外,我不断地发现错误,却没有找到纠正的方法。它似乎在告诉我将值作为列表传递
最终的结果应该是一个数据帧列表这应该有效,并为您提供一个DatetimeIndex
date_index = pd.DatetimeIndex(data=['2020-06-15', '2020-06-15', '2020-06-15', '2020-06-15'])
要返回列表中的一项,您可以迭代此列表:
for item in date_index:
print(item)
不幸的是,这不起作用,forloop是否应该为每个列表值返回一个索引?您希望实现的目标到底是什么?我的回答为您创建了一个包含4个时间戳对象的列表。我更新了我的问题,以便为手头的问题提供更多上下文。我无法重现您的错误消息。如果我调用
pd.DatetimeIndex(start='2020-06-15')
如果没有提供数据,我得到必须提供freq参数。你能检查一下你是否在共享出现错误的代码行吗?我更新了以向你展示完整的回溯你的问题与你的问题以及你最初共享的代码有什么关系?我将否决你的问题。仅供参考:预测指数
!=time\u series\u training
。我的看法是,因为错误涉及datetimeindex,我认为问题就在那里,除此之外,我不知道time\u series\u training会有什么问题。此外,我还试图使问题尽可能简洁,以符合本论坛的规则
for item in date_index:
print(item)