From 466d3f8d66ece33841460a10883cfe1c544e7d4a Mon Sep 17 00:00:00 2001 From: Will Holmgren Date: Sun, 8 May 2016 20:26:46 -0700 Subject: [PATCH 1/6] increase get_sun_rise_set_transit precision, decrease test precision --- pvlib/solarposition.py | 6 +++--- pvlib/test/test_solarposition.py | 36 +++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/pvlib/solarposition.py b/pvlib/solarposition.py index cf20106f03..cce1ca66e5 100644 --- a/pvlib/solarposition.py +++ b/pvlib/solarposition.py @@ -398,11 +398,11 @@ def get_sun_rise_set_transit(time, latitude, longitude, how='numpy', unixtime, lat, lon, delta_t, numthreads) # arrays are in seconds since epoch format, need to conver to timestamps - transit = pd.to_datetime(transit, unit='s', utc=True).tz_convert( + transit = pd.to_datetime(transit*1e9, unit='ns', utc=True).tz_convert( time.tz).tolist() - sunrise = pd.to_datetime(sunrise, unit='s', utc=True).tz_convert( + sunrise = pd.to_datetime(sunrise*1e9, unit='ns', utc=True).tz_convert( time.tz).tolist() - sunset = pd.to_datetime(sunset, unit='s', utc=True).tz_convert( + sunset = pd.to_datetime(sunset*1e9, unit='ns', utc=True).tz_convert( time.tz).tolist() result = pd.DataFrame({'transit': transit, diff --git a/pvlib/test/test_solarposition.py b/pvlib/test/test_solarposition.py index cf3f3b98ac..dc1de6a737 100644 --- a/pvlib/test/test_solarposition.py +++ b/pvlib/test/test_solarposition.py @@ -146,18 +146,23 @@ def test_get_sun_rise_set_transit(): times = pd.DatetimeIndex([datetime.datetime(1996, 7, 5, 0), datetime.datetime(2004, 12, 4, 0)] ).tz_localize('UTC') - sunrise = pd.DatetimeIndex([datetime.datetime(1996, 7, 5, 7, 8, 15, 471676), - datetime.datetime(2004, 12, 4, 4, 38, 57, 27416)] + sunrise = pd.DatetimeIndex([datetime.datetime(1996, 7, 5, 7, 8, 15), + datetime.datetime(2004, 12, 4, 4, 38, 57)] ).tz_localize('UTC').tolist() - sunset = pd.DatetimeIndex([datetime.datetime(1996, 7, 5, 17, 1, 4, 479889), - datetime.datetime(2004, 12, 4, 19, 2, 2, 499704)] + sunset = pd.DatetimeIndex([datetime.datetime(1996, 7, 5, 17, 1, 4), + datetime.datetime(2004, 12, 4, 19, 2, 2)] ).tz_localize('UTC').tolist() result = solarposition.get_sun_rise_set_transit(times, south.latitude, south.longitude, delta_t=64.0) frame = pd.DataFrame({'sunrise':sunrise, 'sunset':sunset}, index=times) - del result['transit'] - assert_frame_equal(frame, result) + result_rounded = pd.DataFrame(index=result.index) + for col, data in result.iteritems(): + result_rounded[col] = pd.to_datetime( + np.floor(data.values.astype(int) / 1e9)*1e9, utc=True) + + del result_rounded['transit'] + assert_frame_equal(frame, result_rounded) # tests from USNO @@ -166,18 +171,25 @@ def test_get_sun_rise_set_transit(): times = pd.DatetimeIndex([datetime.datetime(2015, 1, 2), datetime.datetime(2015, 8, 2),] ).tz_localize('MST') - sunrise = pd.DatetimeIndex([datetime.datetime(2015, 1, 2, 7, 19, 2, 225169), - datetime.datetime(2015, 8, 2, 5, 1, 26, 963145) + sunrise = pd.DatetimeIndex([datetime.datetime(2015, 1, 2, 7, 19, 2), + datetime.datetime(2015, 8, 2, 5, 1, 26) ]).tz_localize('MST').tolist() - sunset = pd.DatetimeIndex([datetime.datetime(2015, 1, 2, 16, 49, 10, 13145), - datetime.datetime(2015, 8, 2, 19, 11, 31, 816401) + sunset = pd.DatetimeIndex([datetime.datetime(2015, 1, 2, 16, 49, 10), + datetime.datetime(2015, 8, 2, 19, 11, 31) ]).tz_localize('MST').tolist() result = solarposition.get_sun_rise_set_transit(times, golden.latitude, golden.longitude, delta_t=64.0) frame = pd.DataFrame({'sunrise':sunrise, 'sunset':sunset}, index=times) - del result['transit'] - assert_frame_equal(frame, result) + result_rounded = pd.DataFrame(index=result.index) + for col, data in result.iteritems(): + result_rounded[col] = (pd.to_datetime( + np.floor(data.values.astype(int) / 1e9)*1e9, utc=True) + .tz_convert('MST')) + + del result_rounded['transit'] + assert_frame_equal(frame, result_rounded) + @requires_ephem def test_pyephem_physical(): From 0064bc17d74dcb1fdd27c30555c7abb9db97c4d4 Mon Sep 17 00:00:00 2001 From: Will Holmgren Date: Sun, 8 May 2016 21:31:02 -0700 Subject: [PATCH 2/6] add pandas 0131 incompat decorator --- pvlib/test/__init__.py | 15 +++++++++++++++ pvlib/test/test_solarposition.py | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pvlib/test/__init__.py b/pvlib/test/__init__.py index bf5fe61b31..947bde1a54 100644 --- a/pvlib/test/__init__.py +++ b/pvlib/test/__init__.py @@ -63,3 +63,18 @@ def incompatible_pandas_0180(test): out = test return out + + +def incompatible_pandas_0131(test): + """ + Test won't work on pandas 0.18.0 due to pandas/numpy issue with + np.round. + """ + + if pd.__version__ == '0.13.1': + out = unittest.skip( + 'error on pandas 0.13.1 due to pandas/numpy')(test) + else: + out = test + + return out diff --git a/pvlib/test/test_solarposition.py b/pvlib/test/test_solarposition.py index dc1de6a737..bc3b1de532 100644 --- a/pvlib/test/test_solarposition.py +++ b/pvlib/test/test_solarposition.py @@ -14,7 +14,7 @@ from pvlib.location import Location from pvlib import solarposition -from . import requires_ephem +from . import requires_ephem, incompatible_pandas_0131 # setup times and locations to be tested. times = pd.date_range(start=datetime.datetime(2014,6,24), @@ -141,6 +141,7 @@ def test_spa_python_numba_physical_dst(): assert_frame_equal(this_expected, ephem_data[expected.columns]) +@incompatible_pandas_0131 def test_get_sun_rise_set_transit(): south = Location(-35.0, 0.0, tz='UTC') times = pd.DatetimeIndex([datetime.datetime(1996, 7, 5, 0), From ad2f35a9d811109cd4548d85b4c11d423d15cc1e Mon Sep 17 00:00:00 2001 From: Will Holmgren Date: Sun, 8 May 2016 22:42:23 -0700 Subject: [PATCH 3/6] use astype on the series, not the values --- pvlib/test/test_solarposition.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pvlib/test/test_solarposition.py b/pvlib/test/test_solarposition.py index bc3b1de532..662418a33e 100644 --- a/pvlib/test/test_solarposition.py +++ b/pvlib/test/test_solarposition.py @@ -141,7 +141,6 @@ def test_spa_python_numba_physical_dst(): assert_frame_equal(this_expected, ephem_data[expected.columns]) -@incompatible_pandas_0131 def test_get_sun_rise_set_transit(): south = Location(-35.0, 0.0, tz='UTC') times = pd.DatetimeIndex([datetime.datetime(1996, 7, 5, 0), @@ -160,7 +159,7 @@ def test_get_sun_rise_set_transit(): result_rounded = pd.DataFrame(index=result.index) for col, data in result.iteritems(): result_rounded[col] = pd.to_datetime( - np.floor(data.values.astype(int) / 1e9)*1e9, utc=True) + np.floor(data.astype(int) / 1e9)*1e9, utc=True) del result_rounded['transit'] assert_frame_equal(frame, result_rounded) @@ -185,7 +184,7 @@ def test_get_sun_rise_set_transit(): result_rounded = pd.DataFrame(index=result.index) for col, data in result.iteritems(): result_rounded[col] = (pd.to_datetime( - np.floor(data.values.astype(int) / 1e9)*1e9, utc=True) + np.floor(data.astype(int) / 1e9)*1e9, utc=True) .tz_convert('MST')) del result_rounded['transit'] From e5a51a3bd737c255e220cdf69d5663735d5458c6 Mon Sep 17 00:00:00 2001 From: Will Holmgren Date: Sun, 8 May 2016 23:02:41 -0700 Subject: [PATCH 4/6] add incompat again --- pvlib/test/test_solarposition.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pvlib/test/test_solarposition.py b/pvlib/test/test_solarposition.py index 662418a33e..fb559cd630 100644 --- a/pvlib/test/test_solarposition.py +++ b/pvlib/test/test_solarposition.py @@ -141,6 +141,7 @@ def test_spa_python_numba_physical_dst(): assert_frame_equal(this_expected, ephem_data[expected.columns]) +@incompatible_pandas_0131 def test_get_sun_rise_set_transit(): south = Location(-35.0, 0.0, tz='UTC') times = pd.DatetimeIndex([datetime.datetime(1996, 7, 5, 0), @@ -157,6 +158,8 @@ def test_get_sun_rise_set_transit(): delta_t=64.0) frame = pd.DataFrame({'sunrise':sunrise, 'sunset':sunset}, index=times) result_rounded = pd.DataFrame(index=result.index) + # need to iterate because to_datetime does not accept 2D data + # the rounding fails on pandas < 0.17 for col, data in result.iteritems(): result_rounded[col] = pd.to_datetime( np.floor(data.astype(int) / 1e9)*1e9, utc=True) @@ -182,6 +185,8 @@ def test_get_sun_rise_set_transit(): delta_t=64.0) frame = pd.DataFrame({'sunrise':sunrise, 'sunset':sunset}, index=times) result_rounded = pd.DataFrame(index=result.index) + # need to iterate because to_datetime does not accept 2D data + # the rounding fails on pandas < 0.17 for col, data in result.iteritems(): result_rounded[col] = (pd.to_datetime( np.floor(data.astype(int) / 1e9)*1e9, utc=True) From 2e5cae6a73c847e8611ff87d62eb7f1c37b8f252 Mon Sep 17 00:00:00 2001 From: Will Holmgren Date: Sun, 8 May 2016 23:12:13 -0700 Subject: [PATCH 5/6] back to values.astype... --- pvlib/test/test_solarposition.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pvlib/test/test_solarposition.py b/pvlib/test/test_solarposition.py index fb559cd630..7af4702889 100644 --- a/pvlib/test/test_solarposition.py +++ b/pvlib/test/test_solarposition.py @@ -162,7 +162,7 @@ def test_get_sun_rise_set_transit(): # the rounding fails on pandas < 0.17 for col, data in result.iteritems(): result_rounded[col] = pd.to_datetime( - np.floor(data.astype(int) / 1e9)*1e9, utc=True) + np.floor(data.values.astype(int) / 1e9)*1e9, utc=True) del result_rounded['transit'] assert_frame_equal(frame, result_rounded) @@ -189,7 +189,7 @@ def test_get_sun_rise_set_transit(): # the rounding fails on pandas < 0.17 for col, data in result.iteritems(): result_rounded[col] = (pd.to_datetime( - np.floor(data.astype(int) / 1e9)*1e9, utc=True) + np.floor(data.values.astype(int) / 1e9)*1e9, utc=True) .tz_convert('MST')) del result_rounded['transit'] From be23c827d86a8c445bbbda7808697bd979b94eeb Mon Sep 17 00:00:00 2001 From: Will Holmgren Date: Mon, 9 May 2016 07:01:25 -0700 Subject: [PATCH 6/6] try int64 instead for windows --- pvlib/test/test_solarposition.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pvlib/test/test_solarposition.py b/pvlib/test/test_solarposition.py index 7af4702889..772ab9589b 100644 --- a/pvlib/test/test_solarposition.py +++ b/pvlib/test/test_solarposition.py @@ -162,7 +162,7 @@ def test_get_sun_rise_set_transit(): # the rounding fails on pandas < 0.17 for col, data in result.iteritems(): result_rounded[col] = pd.to_datetime( - np.floor(data.values.astype(int) / 1e9)*1e9, utc=True) + np.floor(data.values.astype(np.int64) / 1e9)*1e9, utc=True) del result_rounded['transit'] assert_frame_equal(frame, result_rounded) @@ -189,7 +189,7 @@ def test_get_sun_rise_set_transit(): # the rounding fails on pandas < 0.17 for col, data in result.iteritems(): result_rounded[col] = (pd.to_datetime( - np.floor(data.values.astype(int) / 1e9)*1e9, utc=True) + np.floor(data.values.astype(np.int64) / 1e9)*1e9, utc=True) .tz_convert('MST')) del result_rounded['transit']