Search code examples
pythonpandasdatetimeazimuth

Pysolar get_azimuth function applied to pandas DataFrame


I got myself a pandas dataframe with columns latitude, longitude (which are integer type) and a date column (datetime64[ns, UTC] - as needed for the function). I use following line to produce new column of sun's azimuth:

 daa['azimuth'] = daa.apply(lambda row: get_azimuth(row['latitude'], row['longitude'], row['date']), axis=1)

It crashes and I cannot figure out why, the only thing I know is that there is a problem in date:

 File "pandas\_libs\tslibs\timestamps.pyx", line 1332, in pandas._libs.tslibs.timestamps.Timestamp.__new__
 TypeError: an integer is required

If anyone had an idea what I am supposed to do with the date, it would be great, thanks.


Solution

  • this goes back to a bug in pandas, see issue #32174. pysolar.solar.get_azimuth calls .utctimetuple() method of given datetime object (or pd.Timestamp), which fails:

    import pandas as pd
    
    s = pd.to_datetime(pd.Series(["2020-01-01", "2020-01-02"])).dt.tz_localize('UTC')
    
    s.iloc[0]
    Out[3]: Timestamp('2020-01-01 00:00:00+0000', tz='UTC')
    
    s.iloc[0].utctimetuple()
    Traceback (most recent call last):
    
      File "<ipython-input-4-f5e393f18fdb>", line 1, in <module>
        s.iloc[0].utctimetuple()
    
      File "pandas\_libs\tslibs\timestamps.pyx", line 1332, in pandas._libs.tslibs.timestamps.Timestamp.__new__
    
    TypeError: an integer is required
    

    You can work-around by converting the pandas Timestamp to a Python datetime object, were utctimetuple works as expected. For the given example, you can use

    daa.apply(lambda row: get_azimuth(row['latitude'], row['longitude'], row['date'].to_pydatetime()), axis=1)