Skip to content

Commit 6f98df7

Browse files
committed
Add tools._pandas_to_utc
1 parent 3fed282 commit 6f98df7

File tree

4 files changed

+25
-35
lines changed

4 files changed

+25
-35
lines changed

pvlib/clearsky.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,7 @@ def lookup_linke_turbidity(time, latitude, longitude, filepath=None,
206206
with h5py.File(filepath, 'r') as lt_h5_file:
207207
lts = lt_h5_file['LinkeTurbidity'][latitude_index, longitude_index]
208208

209-
# if localized, convert to UTC. otherwise, assume UTC.
210-
try:
211-
time_utc = time.tz_convert('UTC')
212-
except TypeError:
213-
time_utc = time
209+
time_utc = tools._pandas_to_utc(time)
214210

215211
if interp_turbidity:
216212
linke_turbidity = _interpolate_turbidity(lts, time_utc)

pvlib/irradiance.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,7 @@ def _handle_extra_radiation_types(datetime_or_doy, epoch_year):
126126
# a better way to do it.
127127
if isinstance(datetime_or_doy, pd.DatetimeIndex):
128128
to_doy = tools._pandas_to_doy # won't be evaluated unless necessary
129-
def to_datetimeindex(x): # noqa: E306
130-
# if localized, convert to UTC. otherwise, assume UTC.
131-
try:
132-
x_utc = x.tz_convert('UTC')
133-
except TypeError:
134-
x_utc = x
135-
return x_utc
129+
to_datetimeindex = tools._pandas_to_utc
136130
to_output = partial(pd.Series, index=datetime_or_doy)
137131
elif isinstance(datetime_or_doy, pd.Timestamp):
138132
to_doy = tools._pandas_to_doy

pvlib/solarposition.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import warnings
2525
import datetime
2626

27-
from pvlib import atmosphere
27+
from pvlib import atmosphere, tools
2828
from pvlib.tools import datetime_to_djd, djd_to_datetime
2929

3030

@@ -200,11 +200,7 @@ def spa_c(time, latitude, longitude, pressure=101325, altitude=0,
200200
raise ImportError('Could not import built-in SPA calculator. ' +
201201
'You may need to recompile the SPA code.')
202202

203-
# if localized, convert to UTC. otherwise, assume UTC.
204-
try:
205-
time_utc = time.tz_convert('UTC')
206-
except TypeError:
207-
time_utc = time
203+
time_utc = tools._pandas_to_utc(time)
208204

209205
spa_out = []
210206

@@ -645,11 +641,7 @@ def pyephem(time, latitude, longitude, altitude=0, pressure=101325,
645641
except ImportError:
646642
raise ImportError('PyEphem must be installed')
647643

648-
# if localized, convert to UTC. otherwise, assume UTC.
649-
try:
650-
time_utc = time.tz_convert('UTC')
651-
except TypeError:
652-
time_utc = time
644+
time_utc = tools._pandas_to_utc(time)
653645

654646
sun_coords = pd.DataFrame(index=time)
655647

@@ -766,11 +758,7 @@ def ephemeris(time, latitude, longitude, pressure=101325, temperature=12):
766758
# the SPA algorithm needs time to be expressed in terms of
767759
# decimal UTC hours of the day of the year.
768760

769-
# if localized, convert to UTC. otherwise, assume UTC.
770-
try:
771-
time_utc = time.tz_convert('UTC')
772-
except TypeError:
773-
time_utc = time
761+
time_utc = tools._pandas_to_utc(time)
774762

775763
# strip out the day of the year and calculate the decimal hour
776764
DayOfYear = time_utc.dayofyear

pvlib/tools.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,28 @@ def _pandas_to_doy(pd_object):
207207
-------
208208
dayofyear
209209
"""
210-
# if localized, convert to UTC. otherwise, assume UTC.
210+
return _pandas_to_utc(pd_object).dayofyear
211+
212+
213+
def _pandas_to_utc(pd_object):
214+
"""
215+
Converts a pandas datetime-like object to UTC, if localized.
216+
Otherwise, assume UTC.
217+
218+
Parameters
219+
----------
220+
pd_object : DatetimeIndex or Timestamp
221+
222+
Returns
223+
-------
224+
pandas object localized to or assumed to be UTC.
225+
"""
226+
#
211227
try:
212228
pd_object_utc = pd_object.tz_convert('UTC')
213229
except TypeError:
214230
pd_object_utc = pd_object
215-
return pd_object_utc.dayofyear
231+
return pd_object_utc
216232

217233

218234
def _doy_to_datetimeindex(doy, epoch_year=2014):
@@ -242,11 +258,7 @@ def _datetimelike_scalar_to_datetimeindex(time):
242258

243259
timestamp = pd.Timestamp(time)
244260

245-
# if localized, convert to UTC. otherwise, assume UTC.
246-
try:
247-
timestamp_utc = timestamp.tz_convert('UTC')
248-
except TypeError:
249-
timestamp_utc = timestamp
261+
timestamp_utc = _pandas_to_utc(timestamp)
250262

251263
return pd.DatetimeIndex([timestamp_utc])
252264

0 commit comments

Comments
 (0)