-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle time normalization for nonexistent and ambiguous times #2133
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the whitespace and LGTM
Started running the code from this branch on a real problem and realized the handling of eot = np.array([-3.935172, -4.117227, -4.026295])
longitude = 82.3666
times = pd.DatetimeIndex([
'2014-11-02 09:00:00',
'2014-11-02 10:00:00',
'2014-11-02 11:00:00',
]).tz_localize('America/Havana')
solarposition.hour_angle(times, longitude, eot) We get:
I think the |
I suppose we could look at the UTC offsets to build a list of bools indicating which times are DST. We'd then pass EDIT: looks like the tzinfo already has info about DST |
Hmm...I see that I didn't think enough about the tests :) According to the pandas docs |
This passes tests, but is likely slow due to the looping. I'm now thinking that perhaps The issue arises only when a user wants to use DST-aware timestamps in a few places and is not careful when constructing the series of timestamps. It seems better to suggest the user localize using a non-DST timezone such as GMT-4. As far as importing data recorded with DST timestamps, if the timestamps are correct, the ambiguous times won't be in the data. |
That's a good point. In my own use case I could handle the DST stuff before passing the times into
I think that would result in nans being returned from |
+1 |
Do you think I should just close this PR then? For my own use case, if I'm handling the DST stuff outside pvlib anyway then I don't expect to see a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we still want to handle the NonExistentTimeError
case, but lets raise the AmbiguousTimeError
.
Co-authored-by: Cliff Hansen <[email protected]>
@scttnlsn can you add yourself to the Contributors list in the whatsnew file? I can do that for you, if needed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @scttnlsn for the PR! I cannot stand these bizarre timezone issues...
I think even letting the AmbiguousTimeError be raised is fine if the documentation indicates that DST should be handled outside pvlib
Do we still want to add something to the docstring along these lines? I don't see any changes there currently.
Co-authored-by: Kevin Anderson <[email protected]>
Co-authored-by: Adam R. Jensen <[email protected]>
@pvlib/pvanalytics-maintainer if no objections are raised, I will merge this on 9/13 |
What about pvlib-python/pvlib/solarposition.py Lines 1410 to 1419 in cbfa292
pvlib-python/pvlib/solarposition.py Lines 1422 to 1426 in cbfa292
|
# Use Pandas functionality for shifting nonexistent times forward | ||
normalized_times = naive_normalized_times.tz_localize( | ||
times.tz, nonexistent='shift_forward', ambiguous='raise') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about this with the modification of def hour_angle(times, longitude, equation_of_time)
into def hour_angle(times, longitude, equation_of_time, nonexistent='shift_forward', ambiguous='raise')
to give users a choice to control the tz_localize
behavior if they want rather than just receiving an error and being blocked?
# Use Pandas functionality for shifting nonexistent times forward | |
normalized_times = naive_normalized_times.tz_localize( | |
times.tz, nonexistent='shift_forward', ambiguous='raise') | |
# Use Pandas functionality for shifting nonexistent times forward | |
normalized_times = naive_normalized_times.tz_localize( | |
times.tz, nonexistent=nonexistent, ambiguous=ambiguous) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@scttnlsn could you comment on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user would not necessarily be blocked if the exception is raised. They'd just need to convert the given times to a non-DST timezone ahead of calling hour_angle
. The documentation is updated to mention this.
infer
won't really work as mentioned here: #2133 (comment) The other options are to raise
(current approach) or to have NaN
s returned where the times are ambiguous (the user still likely needs to deal with that case).
I'm personally of the opinion that forcing the user to convert to a non-DST timezone is a fine approach but I could understand someone might want to have NaN
s instead.
NonExistentTimeError
when callinghour_angle
#2132docs/sphinx/source/whatsnew
for all changes. Includes link to the GitHub Issue with:issue:`num`
or this Pull Request with:pull:`num`
. Includes contributor name and/or GitHub username (link with:ghuser:`user`
).remote-data
) and Milestone are assigned to the Pull Request and linked Issue.