所以我试图修改我使用pandas转换的字典。但是我仍然收到错误消息:
for key in dictionary:
TypeError: 'instancemethod' object is not iterable
i = pandas.date_range('20110101', '20151231')
df = pandas.DataFrame(dict(year = i.year, month = i.month, day = i.day))
time_format = pandas.to_datetime(df.year*10000 + df.month*100 + df.day, format='%Y%m%d')
times = (time_format.to_dict)
def adding_to_dict(dictionary):
for key in dictionary:
dictionary[key].append(list_of_random)
return dictionary
print adding_to_dict(times)
我确实尝试使用短字典来使用该功能,但仍然没有运气。我正在尝试将值列表/元组添加到这个新的时间戳字典中。任何帮助将不胜感激。谢谢你的时间
答案 0 :(得分:0)
如果您尝试print(times)
,您会发现它不是字典。此外,您还试图附加到字典,而不是如何向字典添加值。你只需做dict[key] = value
假设您希望输出为{timestamp: values}
,这应该可以。
import pandas
import datetime
i = pandas.date_range('20110101', '20151231')
df = pandas.DataFrame(dict(year=i.year, month=i.month, day=i.day))
time_format = pandas.to_datetime(df.year * 10000 + df.month * 100 + df.day, format='%Y%m%d')
times = dict(time_format)
output = {}
def adding_to_dict(dictionary):
for key, value in dictionary.items():
output[datetime.datetime.strftime(value, "%Y-%m-%d")] = 'whatever'
return output
print adding_to_dict(times)
输出:
{'2013-09-08': 'whatever', '2014-01-30': 'whatever', '2012-12-03': 'whatever', '2014-03-21': 'whatever'}