From adc42bcf6136f5eb5d2c5e750118631611de1ee5 Mon Sep 17 00:00:00 2001 From: Will Holmgren Date: Tue, 4 Oct 2016 12:18:11 -0700 Subject: [PATCH 1/3] fix incompatibility with pandas 0.19 for unixtime calculation --- pvlib/solarposition.py | 10 +++++++--- pvlib/test/test_spa.py | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/pvlib/solarposition.py b/pvlib/solarposition.py index eb3568a3f8..894e19f9c3 100644 --- a/pvlib/solarposition.py +++ b/pvlib/solarposition.py @@ -317,7 +317,11 @@ def spa_python(time, latitude, longitude, except (TypeError, ValueError): time = pd.DatetimeIndex([time, ]) - unixtime = time.astype(np.int64)/10**9 + try: + # pandas 0.19 and greater + unixtime = time.astype(np.int64).values/10**9 + except AttributeError: + unixtime = time.astype(np.int64)/10**9 spa = _spa_python_import(how) @@ -390,7 +394,7 @@ def get_sun_rise_set_transit(time, latitude, longitude, how='numpy', # must convert to midnight UTC on day of interest utcday = pd.DatetimeIndex(time.date).tz_localize('UTC') - unixtime = utcday.astype(np.int64)/10**9 + unixtime = np.array(utcday.astype(np.int64)/10**9) spa = _spa_python_import(how) @@ -813,7 +817,7 @@ def nrel_earthsun_distance(time, how='numpy', delta_t=None, numthreads=4): except (TypeError, ValueError): time = pd.DatetimeIndex([time, ]) - unixtime = time.astype(np.int64)/10**9 + unixtime = np.array(time.astype(np.int64)/10**9) spa = _spa_python_import(how) diff --git a/pvlib/test/test_spa.py b/pvlib/test/test_spa.py index 46f665f0e3..dd22559a25 100644 --- a/pvlib/test/test_spa.py +++ b/pvlib/test/test_spa.py @@ -29,7 +29,12 @@ times = (pd.date_range('2003-10-17 12:30:30', periods=1, freq='D') .tz_localize('MST')) -unixtimes = times.tz_convert('UTC').astype(np.int64)*1.0/10**9 +try: + # pandas 0.19 and greater + unixtimes = times.tz_convert('UTC').astype(np.int64).values*1.0/10**9 +except AttributeError: + unixtimes = times.tz_convert('UTC').astype(np.int64)*1.0/10**9 + lat = 39.742476 lon = -105.1786 elev = 1830.14 @@ -251,6 +256,9 @@ def test_transit_sunrise_sunset(self): sunset = pd.DatetimeIndex([dt.datetime(1996, 7, 5, 17, 1, 4), dt.datetime(2004, 12, 4, 19, 2, 2)] ).tz_localize('UTC').astype(np.int64)*1.0/10**9 + times = np.array(times) + sunrise = np.array(sunrise) + sunset = np.array(sunset) result = self.spa.transit_sunrise_sunset(times, -35.0, 0.0, 64.0, 1) assert_almost_equal(sunrise/1e3, result[1]/1e3, 3) assert_almost_equal(sunset/1e3, result[2]/1e3, 3) @@ -262,6 +270,9 @@ def test_transit_sunrise_sunset(self): ).tz_localize('UTC').astype(np.int64)*1.0/10**9 sunrise = pd.DatetimeIndex([dt.datetime(1994, 1, 2, 7, 8, 12),] ).tz_localize('UTC').astype(np.int64)*1.0/10**9 + times = np.array(times) + sunrise = np.array(sunrise) + sunset = np.array(sunset) result = self.spa.transit_sunrise_sunset(times, 35.0, 0.0, 64.0, 1) assert_almost_equal(sunrise/1e3, result[1]/1e3, 3) assert_almost_equal(sunset/1e3, result[2]/1e3, 3) @@ -283,6 +294,9 @@ def test_transit_sunrise_sunset(self): dt.datetime(2015, 8, 2, 19, 10), dt.datetime(2015, 12, 2, 16, 38),], ).tz_localize('MST').astype(np.int64)*1.0/10**9 + times = np.array(times) + sunrise = np.array(sunrise) + sunset = np.array(sunset) result = self.spa.transit_sunrise_sunset(times, 39.0, -105.0, 64.0, 1) assert_almost_equal(sunrise/1e3, result[1]/1e3, 1) assert_almost_equal(sunset/1e3, result[2]/1e3, 1) @@ -305,6 +319,9 @@ def test_transit_sunrise_sunset(self): dt.datetime(2015, 12, 2, 16, 50),], ).tz_localize('Asia/Shanghai' ).astype(np.int64)*1.0/10**9 + times = np.array(times) + sunrise = np.array(sunrise) + sunset = np.array(sunset) result = self.spa.transit_sunrise_sunset(times, 39.917, 116.383, 64.0,1) assert_almost_equal(sunrise/1e3, result[1]/1e3, 1) assert_almost_equal(sunset/1e3, result[2]/1e3, 1) @@ -313,6 +330,7 @@ def test_earthsun_distance(self): times = (pd.date_range('2003-10-17 12:30:30', periods=1, freq='D') .tz_localize('MST')) unixtimes = times.tz_convert('UTC').astype(np.int64)*1.0/10**9 + unixtimes = np.array(unixtimes) result = self.spa.earthsun_distance(unixtimes, 64.0, 1) assert_almost_equal(R, result, 6) From d89bd30992f2cd3b7bd85e23e99ffb2ad03b7abc Mon Sep 17 00:00:00 2001 From: Will Holmgren Date: Tue, 4 Oct 2016 12:19:58 -0700 Subject: [PATCH 2/3] update whatsnew --- docs/sphinx/source/whatsnew/v0.4.1.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/sphinx/source/whatsnew/v0.4.1.txt b/docs/sphinx/source/whatsnew/v0.4.1.txt index 9847b84f09..79c6b9c372 100644 --- a/docs/sphinx/source/whatsnew/v0.4.1.txt +++ b/docs/sphinx/source/whatsnew/v0.4.1.txt @@ -13,6 +13,8 @@ Bug fixes * Fixed an error in the irradiance.klucher transposition model. The error was introduced in version 0.4.0. (:issue:`228`) * Update RAP forecast model variable names. (:issue:`241`) +* Fix incompatibility with pandas 0.19 and solar position calculations. + (:issue:`246`) Documentation From b18b06eff84791784b8f9885e1b5e5ab61135493 Mon Sep 17 00:00:00 2001 From: Will Holmgren Date: Tue, 4 Oct 2016 13:47:19 -0700 Subject: [PATCH 3/3] use np.array instead of try/except --- pvlib/solarposition.py | 6 +----- pvlib/test/test_spa.py | 6 +----- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/pvlib/solarposition.py b/pvlib/solarposition.py index 894e19f9c3..2e124c33f0 100644 --- a/pvlib/solarposition.py +++ b/pvlib/solarposition.py @@ -317,11 +317,7 @@ def spa_python(time, latitude, longitude, except (TypeError, ValueError): time = pd.DatetimeIndex([time, ]) - try: - # pandas 0.19 and greater - unixtime = time.astype(np.int64).values/10**9 - except AttributeError: - unixtime = time.astype(np.int64)/10**9 + unixtime = np.array(time.astype(np.int64)/10**9) spa = _spa_python_import(how) diff --git a/pvlib/test/test_spa.py b/pvlib/test/test_spa.py index dd22559a25..1962bca5c0 100644 --- a/pvlib/test/test_spa.py +++ b/pvlib/test/test_spa.py @@ -29,11 +29,7 @@ times = (pd.date_range('2003-10-17 12:30:30', periods=1, freq='D') .tz_localize('MST')) -try: - # pandas 0.19 and greater - unixtimes = times.tz_convert('UTC').astype(np.int64).values*1.0/10**9 -except AttributeError: - unixtimes = times.tz_convert('UTC').astype(np.int64)*1.0/10**9 +unixtimes = np.array(times.tz_convert('UTC').astype(np.int64)*1.0/10**9) lat = 39.742476 lon = -105.1786