Takže sa snažím zmeniť tento slovník, ktorý som previedol pomocou pandy. Stále však dostávam chybové hlásenie, že:
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)
Pokúsil som sa použiť krátky slovník na použitieale stále bez šťastia. Snažím sa do tohto nového slovníka časových pečiatok pridávať zoznam / nuple hodnôt. Akákoľvek pomoc by bola veľmi ocenená. Ďakujem za Váš čas
odpovede:
0 pre odpoveď č. 1ak vyskúšate print(times)
uvidíte, že to nie je slovník. Tiež sa pokúšate pripojiť k slovníku, čo nie je, ako pridávať hodnoty k diktu dict[key] = value
Za predpokladu, že chcete, aby bol váš výstup {timestamp: values}
, malo by to fungovať.
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)
Výkon:
{"2013-09-08": "whatever", "2014-01-30": "whatever", "2012-12-03": "whatever", "2014-03-21": "whatever"}