I have a daily time series with DateTime
index. I want to calculate the sunrise and sunset times for each day in the DataFrame. The result will be presented in columns rise
and set
. Below is my script using pyephem:
import ephem
import datetime
AliceS = ephem.Observer()
AliceS.lat = '-23.762'
AliceS.lon = '133.875'
AliceS.date = df.index
sun = ephem.Sun()
df['rise'] = ephem.localtime(AliceS.next_rising(sun))
df['set'] = ephem.localtime(AliceS.next_setting(sun))
This raises
ValueError: dates must be initialized from a number, string, tuple, or datetime
I believe that the cause of the error is AliceS.date = df.index
, but I don't know how to fix it.
Below is an example of the datetime index:
DateTime
2016-04-02
2016-04-03
2016-04-04
2016-04-07
2016-04-08