@@ -322,10 +322,6 @@ def _generate(cls, start, end, periods, name, offset,
322
322
# Convert local to UTC
323
323
ints = index .view ('i8' )
324
324
index = lib .tz_localize_to_utc (ints , tz )
325
-
326
- # lib.tz_localize_check(ints, tz)
327
- # index = lib.tz_convert(ints, tz, _utc())
328
-
329
325
index = index .view (_NS_DTYPE )
330
326
331
327
index = index .view (cls )
@@ -1174,7 +1170,9 @@ def tz_convert(self, tz):
1174
1170
tz = tools ._maybe_get_tz (tz )
1175
1171
1176
1172
if self .tz is None :
1177
- return self .tz_localize (tz )
1173
+ # tz naive, use tz_localize
1174
+ raise Exception ('Cannot convert tz-naive timestamps, use '
1175
+ 'tz_localize to localize' )
1178
1176
1179
1177
# No conversion since timestamps are all UTC to begin with
1180
1178
return self ._simple_new (self .values , self .name , self .offset , tz )
@@ -1188,15 +1186,13 @@ def tz_localize(self, tz):
1188
1186
localized : DatetimeIndex
1189
1187
"""
1190
1188
if self .tz is not None :
1191
- raise ValueError ("Already have timezone info, "
1192
- "use tz_convert to convert." )
1189
+ raise ValueError ("Already tz-aware, use tz_convert to convert." )
1193
1190
tz = tools ._maybe_get_tz (tz )
1194
1191
1195
- lib .tz_localize_check (self .asi8 , tz )
1196
-
1197
1192
# Convert to UTC
1198
- new_dates = lib .tz_convert (self .asi8 , tz , _utc () )
1193
+ new_dates = lib .tz_localize_to_utc (self .asi8 , tz )
1199
1194
new_dates = new_dates .view (_NS_DTYPE )
1195
+
1200
1196
return self ._simple_new (new_dates , self .name , self .offset , tz )
1201
1197
1202
1198
def tz_validate (self ):
@@ -1355,111 +1351,3 @@ def _time_to_nanosecond(time):
1355
1351
return (1000000 * seconds + time .microsecond ) * 1000
1356
1352
1357
1353
1358
-
1359
- def tz_localize_to_utc (vals , tz ):
1360
- """
1361
- Localize tzinfo-naive DateRange to given time zone (using pytz). If
1362
- there are ambiguities in the values, raise AmbiguousTimeError.
1363
-
1364
- Returns
1365
- -------
1366
- localized : DatetimeIndex
1367
- """
1368
- # Vectorized version of DstTzInfo.localize
1369
-
1370
- # if not have_pytz:
1371
- # raise Exception("Could not find pytz module")
1372
- import pytz
1373
-
1374
- n = len (vals )
1375
- DAY_NS = 86400000000000L
1376
- NPY_NAT = lib .iNaT
1377
-
1378
- if tz == pytz .utc or tz is None :
1379
- return vals
1380
-
1381
- trans = _get_transitions (tz ) # transition dates
1382
- deltas = _get_deltas (tz ) # utc offsets
1383
-
1384
- result = np .empty (n , dtype = np .int64 )
1385
- result_a = np .empty (n , dtype = np .int64 )
1386
- result_b = np .empty (n , dtype = np .int64 )
1387
- result_a .fill (NPY_NAT )
1388
- result_b .fill (NPY_NAT )
1389
-
1390
- # left side
1391
- idx_shifted = np .maximum (0 , trans .searchsorted (vals - DAY_NS ,
1392
- side = 'right' ) - 1 )
1393
-
1394
- for i in range (n ):
1395
- v = vals [i ] - deltas [idx_shifted [i ]]
1396
- pos = trans .searchsorted (v , side = 'right' ) - 1
1397
-
1398
- # timestamp falls to the left side of the DST transition
1399
- if v + deltas [pos ] == vals [i ]:
1400
- result_a [i ] = v
1401
-
1402
- # right side
1403
- idx_shifted = np .maximum (0 , trans .searchsorted (vals + DAY_NS ,
1404
- side = 'right' ) - 1 )
1405
-
1406
- for i in range (n ):
1407
- v = vals [i ] - deltas [idx_shifted [i ]]
1408
- pos = trans .searchsorted (v , side = 'right' ) - 1
1409
-
1410
- # timestamp falls to the right side of the DST transition
1411
- if v + deltas [pos ] == vals [i ]:
1412
- result_b [i ] = v
1413
-
1414
- for i in range (n ):
1415
- left = result_a [i ]
1416
- right = result_b [i ]
1417
- if left != NPY_NAT and right != NPY_NAT :
1418
- if left == right :
1419
- result [i ] = left
1420
- else :
1421
- stamp = Timestamp (vals [i ])
1422
- raise pytz .AmbiguousTimeError (stamp )
1423
- elif left != NPY_NAT :
1424
- result [i ] = left
1425
- elif right != NPY_NAT :
1426
- result [i ] = right
1427
- else :
1428
- stamp = Timestamp (vals [i ])
1429
- raise pytz .NonExistentTimeError (stamp )
1430
-
1431
- return result
1432
-
1433
-
1434
- trans_cache = {}
1435
- utc_offset_cache = {}
1436
-
1437
- def _get_transitions (tz ):
1438
- """
1439
- Get UTC times of DST transitions
1440
- """
1441
- if tz not in trans_cache :
1442
- arr = np .array (tz ._utc_transition_times , dtype = 'M8[ns]' )
1443
- trans_cache [tz ] = arr .view ('i8' )
1444
- return trans_cache [tz ]
1445
-
1446
- def _get_deltas (tz ):
1447
- """
1448
- Get UTC offsets in microseconds corresponding to DST transitions
1449
- """
1450
- if tz not in utc_offset_cache :
1451
- utc_offset_cache [tz ] = _unbox_utcoffsets (tz ._transition_info )
1452
- return utc_offset_cache [tz ]
1453
-
1454
- def total_seconds (td ): # Python 2.6 compat
1455
- return ((td .microseconds + (td .seconds + td .days * 24 * 3600 ) * 10 ** 6 ) //
1456
- 10 ** 6 )
1457
-
1458
- def _unbox_utcoffsets (transinfo ):
1459
- sz = len (transinfo )
1460
- arr = np .empty (sz , dtype = 'i8' )
1461
-
1462
- for i in range (sz ):
1463
- arr [i ] = int (total_seconds (transinfo [i ][0 ])) * 1000000000
1464
-
1465
- return arr
0 commit comments