Skip to content

Commit 2348afd

Browse files
committed
Fix handling of ambiguous times during DST shift
1 parent 6354443 commit 2348afd

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

pvlib/solarposition.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,14 @@ def hour_angle(times, longitude, equation_of_time):
13921392
times = times.tz_localize('utc')
13931393
tzs = np.array([ts.utcoffset().total_seconds() for ts in times]) / 3600
13941394

1395+
is_dst = []
1396+
for time in times:
1397+
dst = time.tzinfo.dst(time)
1398+
if dst is None:
1399+
is_dst.append(False)
1400+
else:
1401+
is_dst.append(dst > dt.timedelta(seconds=0))
1402+
13951403
# Some timezones have a DST shift at midnight:
13961404
# 11:59pm -> 1:00am - results in a nonexistent midnight
13971405
# 12:59am -> 12:00am - results in an ambiguous midnight
@@ -1401,7 +1409,7 @@ def hour_angle(times, longitude, equation_of_time):
14011409
# Use Pandas functionality for shifting nonexistent times forward
14021410
# or infering ambiguous times (which arose from normalizing)
14031411
normalized_times = naive_normalized_times.tz_localize(
1404-
times.tz, nonexistent='shift_forward', ambiguous='infer')
1412+
times.tz, nonexistent='shift_forward', ambiguous=is_dst)
14051413

14061414
hrs_minus_tzs = (times - normalized_times) / pd.Timedelta('1h') - tzs
14071415

pvlib/tests/test_solarposition.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -677,13 +677,15 @@ def test_hour_angle():
677677
def test_hour_angle_with_tricky_timezones():
678678
# tests timezones that have a DST shift at midnight
679679

680-
eot = np.array([-3.935172, -4.117227])
680+
eot = np.array([-3.935172, -4.117227, -4.026295, -4.026295])
681681

682682
longitude = 70.6693
683683
times = pd.DatetimeIndex([
684-
'2014-09-07 10:00:00',
685-
'2014-09-07 11:00:00',
686-
]).tz_localize('America/Santiago')
684+
'2014-09-06 23:00:00',
685+
'2014-09-07 00:00:00',
686+
'2014-09-07 01:00:00',
687+
'2014-09-07 02:00:00',
688+
]).tz_localize('America/Santiago', nonexistent='shift_forward')
687689

688690
with pytest.raises(pytz.exceptions.NonExistentTimeError):
689691
times.normalize()
@@ -693,9 +695,11 @@ def test_hour_angle_with_tricky_timezones():
693695

694696
longitude = 82.3666
695697
times = pd.DatetimeIndex([
696-
'2014-11-02 10:00:00',
697-
'2014-11-02 11:00:00',
698-
]).tz_localize('America/Havana')
698+
'2014-11-01 23:00:00',
699+
'2014-11-02 00:00:00',
700+
'2014-11-02 01:00:00',
701+
'2014-11-02 02:00:00',
702+
]).tz_localize('America/Havana', ambiguous=[True, True, False, False])
699703

700704
with pytest.raises(pytz.exceptions.AmbiguousTimeError):
701705
times.normalize()

0 commit comments

Comments
 (0)