2

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?

1
  • 1
    map is lazy in Python-3.x, so you do not evaluate the map object (unless forced to do so). Commented May 2, 2020 at 10:14

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Ok this one worked; can you tell me the difference between your code and mine? is it because of some deprecated pandas syntax?
@Ale - No, because map used in your solution is pure python map function and in pandas it is index.map function. It is not same.
@Ale - Also is used Series.map for working with Series or columns.
1

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.