kylebarron / suncalc-py

A Python port of suncalc.js for calculating sun position and sunlight phases
MIT License
60 stars 8 forks source link

Pandas NaT times cause unexpected behaviour #19

Open GalenReich opened 1 month ago

GalenReich commented 1 month ago

Currently if a datetime holding the NaT value (supported by Numpy and Pandas) is passed to get_position it returns results for 12:12:43 UTC on Tuesday, September 21, 1677!

Minimum example to reproduce:

import pandas as pd
from suncalc import get_position

# Create DatetimeIndex containing NaTs
test_pandas_df = pd.to_datetime([None, None], unit="s", utc=True) 

print(get_position(test_pandas_df, 0, 0))

Gives: {'azimuth': Index([-0.9723467728956097, -0.9723467728956097], dtype='float64'), 'altitude': Index([-1.5207425775049375, -1.5207425775049375], dtype='float64')}

The bug is introduced by the coercion in get_milliseconds, which returns -9223372036854.775 (ms) for NaT values. This time offset from the UNIX epoch corresponds to the above date in 1677!

https://github.com/kylebarron/suncalc-py/blob/6568431fe494393774b1ee34647d8cab35b5141d/suncalc/suncalc.py#L69-L72

Instead, it would be great if NaT values were handled gracefully and None or np.nan values were returned instead. Alternatively, an error could be raised indicating that NaT values are unsupported.

kylebarron commented 1 month ago

A pull request would be welcome. I don't have the bandwidth to make updates here myself now