Reputation: 104
I'm trying to replicate this analysis for my city. I'm at the step where I copied and pasted the function to compute daily hours of daylight and add the aggregate count (weekly and daily) to the grouped dataset and then plot.
The thing is that I believe that the map function used here behave strangely (or something has changed along the pandas version). If I give for example weekly.head()
the program returns in the hours_of_daylight column this output while I am expecting a number
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>
Of course, after this matplotlib says that there is not any numeric data to plot
The code is
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()
and, for the plot
weekly['daylight'].plot()
plt.ylabel('hours of daylight (Seattle)');
Can you help me understand what is wrong with these lines of code and get the effective number as result as well as the chart?
Upvotes: 2
Views: 273
Reputation: 11
You can either try the .filter() function by providing specific details in the bracket or you can use the index.map() to get a structured DF. I personally would recommend to go for .filter()
Upvotes: 1
Reputation: 863226
Use 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)
Upvotes: 2