11
11
import pandas .algos as algos
12
12
import pandas .lib as lib
13
13
import pandas .tslib as tslib
14
- from distutils .version import LooseVersion
15
14
from pandas import compat
16
15
from pandas .compat import StringIO , BytesIO , range , long , u , zip , map
17
16
from datetime import timedelta
18
17
19
18
from pandas .core .config import get_option
20
19
from pandas .core import array as pa
21
20
22
-
23
- # XXX: HACK for NumPy 1.5.1 to suppress warnings
24
- try :
25
- np .seterr (all = 'ignore' )
26
- # np.set_printoptions(suppress=True)
27
- except Exception : # pragma: no cover
28
- pass
29
-
30
-
31
21
class PandasError (Exception ):
32
22
pass
33
23
34
24
35
25
class AmbiguousIndexError (PandasError , KeyError ):
36
26
pass
37
27
38
- # versioning
39
- _np_version = np .version .short_version
40
- _np_version_under1p6 = LooseVersion (_np_version ) < '1.6'
41
- _np_version_under1p7 = LooseVersion (_np_version ) < '1.7'
42
-
43
28
_POSSIBLY_CAST_DTYPES = set ([np .dtype (t )
44
29
for t in ['M8[ns]' , 'm8[ns]' , 'O' , 'int8' , 'uint8' , 'int16' , 'uint16' , 'int32' , 'uint32' , 'int64' , 'uint64' ]])
45
30
@@ -704,34 +689,13 @@ def diff(arr, n, axis=0):
704
689
705
690
return out_arr
706
691
707
-
708
- def _coerce_scalar_to_timedelta_type (r ):
709
- # kludgy here until we have a timedelta scalar
710
- # handle the numpy < 1.7 case
711
-
712
- if is_integer (r ):
713
- r = timedelta (microseconds = r / 1000 )
714
-
715
- if _np_version_under1p7 :
716
- if not isinstance (r , timedelta ):
717
- raise AssertionError ("Invalid type for timedelta scalar: %s" % type (r ))
718
- if compat .PY3 :
719
- # convert to microseconds in timedelta64
720
- r = np .timedelta64 (int (r .total_seconds ()* 1e9 + r .microseconds * 1000 ))
721
- else :
722
- return r
723
-
724
- if isinstance (r , timedelta ):
725
- r = np .timedelta64 (r )
726
- elif not isinstance (r , np .timedelta64 ):
727
- raise AssertionError ("Invalid type for timedelta scalar: %s" % type (r ))
728
- return r .astype ('timedelta64[ns]' )
729
-
730
692
def _coerce_to_dtypes (result , dtypes ):
731
693
""" given a dtypes and a result set, coerce the result elements to the dtypes """
732
694
if len (result ) != len (dtypes ):
733
695
raise AssertionError ("_coerce_to_dtypes requires equal len arrays" )
734
696
697
+ from pandas .tseries .timedeltas import _coerce_scalar_to_timedelta_type
698
+
735
699
def conv (r ,dtype ):
736
700
try :
737
701
if isnull (r ):
@@ -1324,68 +1288,6 @@ def _possibly_convert_platform(values):
1324
1288
1325
1289
return values
1326
1290
1327
-
1328
- def _possibly_cast_to_timedelta (value , coerce = True ):
1329
- """ try to cast to timedelta64, if already a timedeltalike, then make
1330
- sure that we are [ns] (as numpy 1.6.2 is very buggy in this regards,
1331
- don't force the conversion unless coerce is True
1332
-
1333
- if coerce='compat' force a compatibilty coercerion (to timedeltas) if needeed
1334
- """
1335
-
1336
- # coercion compatability
1337
- if coerce == 'compat' and _np_version_under1p7 :
1338
-
1339
- def convert (td , dtype ):
1340
-
1341
- # we have an array with a non-object dtype
1342
- if hasattr (td ,'item' ):
1343
- td = td .astype (np .int64 ).item ()
1344
- if td == tslib .iNaT :
1345
- return td
1346
- if dtype == 'm8[us]' :
1347
- td *= 1000
1348
- return td
1349
-
1350
- if td == tslib .compat_NaT :
1351
- return tslib .iNaT
1352
-
1353
- # convert td value to a nanosecond value
1354
- d = td .days
1355
- s = td .seconds
1356
- us = td .microseconds
1357
-
1358
- if dtype == 'object' or dtype == 'm8[ns]' :
1359
- td = 1000 * us + (s + d * 24 * 3600 ) * 10 ** 9
1360
- else :
1361
- raise ValueError ("invalid conversion of dtype in np < 1.7 [%s]" % dtype )
1362
-
1363
- return td
1364
-
1365
- # < 1.7 coercion
1366
- if not is_list_like (value ):
1367
- value = np .array ([ value ])
1368
-
1369
- dtype = value .dtype
1370
- return np .array ([ convert (v ,dtype ) for v in value ], dtype = 'm8[ns]' )
1371
-
1372
- # deal with numpy not being able to handle certain timedelta operations
1373
- if isinstance (value , (ABCSeries , np .ndarray )) and value .dtype .kind == 'm' :
1374
- if value .dtype != 'timedelta64[ns]' :
1375
- value = value .astype ('timedelta64[ns]' )
1376
- return value
1377
-
1378
- # we don't have a timedelta, but we want to try to convert to one (but
1379
- # don't force it)
1380
- if coerce :
1381
- new_value = tslib .array_to_timedelta64 (
1382
- _values_from_object (value ).astype (object ), coerce = False )
1383
- if new_value .dtype == 'i8' :
1384
- value = np .array (new_value , dtype = 'timedelta64[ns]' )
1385
-
1386
- return value
1387
-
1388
-
1389
1291
def _possibly_cast_to_datetime (value , dtype , coerce = False ):
1390
1292
""" try to cast the array/value to a datetimelike dtype, converting float nan to iNaT """
1391
1293
@@ -1423,6 +1325,7 @@ def _possibly_cast_to_datetime(value, dtype, coerce=False):
1423
1325
from pandas .tseries .tools import to_datetime
1424
1326
value = to_datetime (value , coerce = coerce ).values
1425
1327
elif is_timedelta64 :
1328
+ from pandas .tseries .timedeltas import _possibly_cast_to_timedelta
1426
1329
value = _possibly_cast_to_timedelta (value )
1427
1330
except :
1428
1331
pass
@@ -1448,6 +1351,7 @@ def _possibly_cast_to_datetime(value, dtype, coerce=False):
1448
1351
except :
1449
1352
pass
1450
1353
elif inferred_type in ['timedelta' , 'timedelta64' ]:
1354
+ from pandas .tseries .timedeltas import _possibly_cast_to_timedelta
1451
1355
value = _possibly_cast_to_timedelta (value )
1452
1356
1453
1357
return value
0 commit comments