Search code examples
pythonpandasdataframepython-datetime

Convert datetime column using function


I have a dataframe with a column of datetimes. I'm trying to get the top answer from this post to work on my dataframe.

def sunrise(timee):

 sun = ephem.Sun()
 observer = ephem.Observer()
 # ↓ Define your coordinates here ↓
 observer.lat, observer.lon, observer.elevation = '48.730302', '9.149483', 400
 # ↓ Set the time (UTC) here ↓
 observer.date = timee
 sun.compute(observer)
 current_sun_alt = sun.alt
 return current_sun_alt * 180 / math.pi

ClimateDF.insert(1, 'datetime', pd.to_datetime(ClimateDF.index)) #Index is date time!
ClimateDF['datetime'] = ClimateDF['datetime'].dt.strftime('%Y/%m/%d %H:%M:%S')
ClimateDF['datetime'] = sunrise(ClimateDF['datetime']) 

I can't figure it out. What am I missing? I am currently getting:

 raise TypeError("cannot convert the series to " "{0}".format(str(converter)))
TypeError: cannot convert the series to <class 'float'>

Solution

  • You should do return current_sun_alt * 180 / math.pi instead of printing it.