我正在尝试为我的城市复制this analysis。 在此步骤中,我复制并粘贴了该函数以计算日光的小时数,然后将汇总计数(每周和每天)添加到分组的数据集中,然后进行绘图。
问题是,我相信这里使用的map函数的行为会很奇怪(或者在pandas版本中有所改变)。例如,如果我给出weekly.head()
,则程序会在hours_of_daylight列中返回此输出,而我希望有一个数字
Total East West daylight
Date
2012-10-07 14292.0 7297.0 6995.0 <map object at 0x7f21afcc1ca0>
2012-10-14 16795.0 8679.0 8116.0 <map object at 0x7f21afcc1ca0>
2012-10-21 15509.0 7946.0 7563.0 <map object at 0x7f21afcc1ca0>
2012-10-28 13437.0 6901.0 6536.0 <map object at 0x7f21afcc1ca0>
2012-11-04 12194.0 6408.0 5786.0 <map object at 0x7f21afcc1ca0>
当然,在此matplotlib说没有要绘制的数字数据之后
代码是
daily = data.resample('d').sum()
weekly = data.resample('w').sum()
def hours_of_daylight(date, axis=23.44, latitude=47.61):
"""Compute the hours of daylight for the given date"""
diff = date - pd.datetime(2000, 12, 21)
day = diff.total_seconds() / 24. / 3600
day %= 365.25
m = 1. - np.tan(np.radians(latitude)) * np.tan(np.radians(axis) * np.cos(day * np.pi / 182.625))
m = max(0, min(m, 2))
return 24. * np.degrees(np.arccos(1 - m)) / 180.
# add this to our weekly data
weekly['daylight'] = map(hours_of_daylight, weekly.index)
daily['daylight'] = map(hours_of_daylight, daily.index)
weekly.head()
然后是情节
weekly['daylight'].plot()
plt.ylabel('hours of daylight (Seattle)');
您能帮助我理解这些代码行的问题并获得结果和图表的有效数字吗?
答案 0 :(得分:2)
使用Index.map
:
weekly['daylight'] = weekly.index.map(hours_of_daylight)
print (weekly)
Total East West daylight
Date
2012-10-07 14292.0 7297.0 6995.0 11.045208
2012-10-14 16795.0 8679.0 8116.0 10.644852
2012-10-21 15509.0 7946.0 7563.0 10.255305
2012-10-28 13437.0 6901.0 6536.0 9.881095
2012-11-04 12194.0 6408.0 5786.0 9.527645
daily['daylight'] = daily.index.map(hours_of_daylight)
答案 1 :(得分:1)
您可以通过在方括号中提供特定的详细信息来尝试.filter()函数,也可以使用index.map()获得结构化DF。 我个人建议使用.filter()