14
14
import pandas .hashtable as _hash
15
15
from pandas import compat , lib , algos , tslib
16
16
from pandas .compat import builtins
17
- from pandas .core .common import isnull , notnull , _values_from_object , is_float
17
+ from pandas .core .common import (isnull , notnull , _values_from_object ,
18
+ _maybe_upcast_putmask ,
19
+ ensure_float , _ensure_float64 ,
20
+ _ensure_int64 , _ensure_object ,
21
+ is_float , is_integer , is_complex ,
22
+ is_float_dtype , _is_floating_dtype ,
23
+ is_complex_dtype , is_integer_dtype ,
24
+ is_bool_dtype , is_object_dtype ,
25
+ is_datetime64_dtype , is_timedelta64_dtype ,
26
+ _is_datetime_or_timedelta_dtype ,
27
+ _is_int_or_datetime_dtype , _is_any_int_dtype )
18
28
19
29
20
30
class disallow (object ):
@@ -90,8 +100,8 @@ def f(values, axis=None, skipna=True, **kwds):
90
100
91
101
def _bn_ok_dtype (dt , name ):
92
102
# Bottleneck chokes on datetime64
93
- if dt != np . object_ and not issubclass (dt . type , ( np . datetime64 ,
94
- np . timedelta64 )):
103
+ if ( not is_object_dtype (dt ) and
104
+ not _is_datetime_or_timedelta_dtype ( dt )):
95
105
96
106
# bottleneck does not properly upcast during the sum
97
107
# so can overflow
@@ -166,8 +176,7 @@ def _get_values(values, skipna, fill_value=None, fill_value_typ=None,
166
176
167
177
# promote if needed
168
178
else :
169
- values , changed = com ._maybe_upcast_putmask (values , mask ,
170
- fill_value )
179
+ values , changed = _maybe_upcast_putmask (values , mask , fill_value )
171
180
172
181
elif copy :
173
182
values = values .copy ()
@@ -176,47 +185,42 @@ def _get_values(values, skipna, fill_value=None, fill_value_typ=None,
176
185
177
186
# return a platform independent precision dtype
178
187
dtype_max = dtype
179
- if dtype .kind == 'i' and not issubclass (dtype .type , (np .bool ,
180
- np .datetime64 ,
181
- np .timedelta64 )):
188
+ if is_integer_dtype (dtype ) or is_bool_dtype (dtype ):
182
189
dtype_max = np .int64
183
- elif dtype .kind in ['b' ] or issubclass (dtype .type , np .bool ):
184
- dtype_max = np .int64
185
- elif dtype .kind in ['f' ]:
190
+ elif is_float_dtype (dtype ):
186
191
dtype_max = np .float64
187
192
188
193
return values , mask , dtype , dtype_max
189
194
190
195
191
196
def _isfinite (values ):
192
- if issubclass (values . dtype . type , ( np . timedelta64 , np . datetime64 ) ):
197
+ if _is_datetime_or_timedelta_dtype (values ):
193
198
return isnull (values )
194
- elif isinstance ( values . dtype , object ):
195
- return ~ np . isfinite (values . astype ( 'float64' ))
196
-
197
- return ~ np .isfinite (values )
199
+ if ( is_complex_dtype ( values ) or is_float_dtype ( values ) or
200
+ is_integer_dtype (values ) or is_bool_dtype ( values )):
201
+ return ~ np . isfinite ( values )
202
+ return ~ np .isfinite (values . astype ( 'float64' ) )
198
203
199
204
200
205
def _na_ok_dtype (dtype ):
201
- return not issubclass (dtype .type , (np .integer , np .datetime64 ,
202
- np .timedelta64 ))
206
+ return not _is_int_or_datetime_dtype (dtype )
203
207
204
208
205
209
def _view_if_needed (values ):
206
- if issubclass (values . dtype . type , ( np . datetime64 , np . timedelta64 ) ):
210
+ if _is_datetime_or_timedelta_dtype (values ):
207
211
return values .view (np .int64 )
208
212
return values
209
213
210
214
211
215
def _wrap_results (result , dtype ):
212
216
""" wrap our results if needed """
213
217
214
- if issubclass (dtype . type , np . datetime64 ):
218
+ if is_datetime64_dtype (dtype ):
215
219
if not isinstance (result , np .ndarray ):
216
220
result = lib .Timestamp (result )
217
221
else :
218
222
result = result .view (dtype )
219
- elif issubclass (dtype . type , np . timedelta64 ):
223
+ elif is_timedelta64_dtype (dtype ):
220
224
if not isinstance (result , np .ndarray ):
221
225
222
226
# this is a scalar timedelta result!
@@ -334,7 +338,7 @@ def _get_counts_nanvar(mask, axis, ddof):
334
338
@disallow ('M8' )
335
339
@bottleneck_switch (ddof = 1 )
336
340
def nanvar (values , axis = None , skipna = True , ddof = 1 ):
337
- if not isinstance (values . dtype . type , np . floating ):
341
+ if not _is_floating_dtype (values ):
338
342
values = values .astype ('f8' )
339
343
340
344
mask = isnull (values )
@@ -353,7 +357,7 @@ def nanvar(values, axis=None, skipna=True, ddof=1):
353
357
def nansem (values , axis = None , skipna = True , ddof = 1 ):
354
358
var = nanvar (values , axis , skipna , ddof = ddof )
355
359
356
- if not isinstance (values . dtype . type , np . floating ):
360
+ if not _is_floating_dtype (values ):
357
361
values = values .astype ('f8' )
358
362
mask = isnull (values )
359
363
count , _ = _get_counts_nanvar (mask , axis , ddof )
@@ -367,7 +371,7 @@ def nanmin(values, axis=None, skipna=True):
367
371
fill_value_typ = '+inf' )
368
372
369
373
# numpy 1.6.1 workaround in Python 3.x
370
- if (values . dtype == np . object_ and compat .PY3 ) :
374
+ if is_object_dtype (values ) and compat .PY3 :
371
375
if values .ndim > 1 :
372
376
apply_ax = axis if axis is not None else 0
373
377
result = np .apply_along_axis (builtins .min , apply_ax , values )
@@ -380,7 +384,7 @@ def nanmin(values, axis=None, skipna=True):
380
384
if ((axis is not None and values .shape [axis ] == 0 )
381
385
or values .size == 0 ):
382
386
try :
383
- result = com . ensure_float (values .sum (axis , dtype = dtype_max ))
387
+ result = ensure_float (values .sum (axis , dtype = dtype_max ))
384
388
result .fill (np .nan )
385
389
except :
386
390
result = np .nan
@@ -397,7 +401,7 @@ def nanmax(values, axis=None, skipna=True):
397
401
fill_value_typ = '-inf' )
398
402
399
403
# numpy 1.6.1 workaround in Python 3.x
400
- if (values . dtype == np . object_ and compat .PY3 ) :
404
+ if is_object_dtype (values ) and compat .PY3 :
401
405
402
406
if values .ndim > 1 :
403
407
apply_ax = axis if axis is not None else 0
@@ -411,7 +415,7 @@ def nanmax(values, axis=None, skipna=True):
411
415
if ((axis is not None and values .shape [axis ] == 0 )
412
416
or values .size == 0 ):
413
417
try :
414
- result = com . ensure_float (values .sum (axis , dtype = dtype_max ))
418
+ result = ensure_float (values .sum (axis , dtype = dtype_max ))
415
419
result .fill (np .nan )
416
420
except :
417
421
result = np .nan
@@ -446,7 +450,7 @@ def nanargmin(values, axis=None, skipna=True):
446
450
447
451
@disallow ('M8' )
448
452
def nanskew (values , axis = None , skipna = True ):
449
- if not isinstance (values . dtype . type , np . floating ):
453
+ if not _is_floating_dtype (values ):
450
454
values = values .astype ('f8' )
451
455
452
456
mask = isnull (values )
@@ -480,7 +484,7 @@ def nanskew(values, axis=None, skipna=True):
480
484
481
485
@disallow ('M8' )
482
486
def nankurt (values , axis = None , skipna = True ):
483
- if not isinstance (values . dtype . type , np . floating ):
487
+ if not _is_floating_dtype (values ):
484
488
values = values .astype ('f8' )
485
489
486
490
mask = isnull (values )
@@ -515,7 +519,7 @@ def nankurt(values, axis=None, skipna=True):
515
519
@disallow ('M8' )
516
520
def nanprod (values , axis = None , skipna = True ):
517
521
mask = isnull (values )
518
- if skipna and not issubclass (values . dtype . type , np . integer ):
522
+ if skipna and not _is_any_int_dtype (values ):
519
523
values = values .copy ()
520
524
values [mask ] = 1
521
525
result = values .prod (axis )
@@ -644,17 +648,17 @@ def nancov(a, b, min_periods=None):
644
648
645
649
def _ensure_numeric (x ):
646
650
if isinstance (x , np .ndarray ):
647
- if x . dtype . kind in [ 'i' , 'b' ] :
651
+ if is_integer_dtype ( x ) or is_bool_dtype ( x ) :
648
652
x = x .astype (np .float64 )
649
- elif x . dtype == np . object_ :
653
+ elif is_object_dtype ( x ) :
650
654
try :
651
655
x = x .astype (np .complex128 )
652
656
except :
653
657
x = x .astype (np .float64 )
654
658
else :
655
659
if not np .any (x .imag ):
656
660
x = x .real
657
- elif not (com . is_float (x ) or com . is_integer (x ) or com . is_complex (x )):
661
+ elif not (is_float (x ) or is_integer (x ) or is_complex (x )):
658
662
try :
659
663
x = float (x )
660
664
except Exception :
@@ -678,7 +682,7 @@ def f(x, y):
678
682
result = op (x , y )
679
683
680
684
if mask .any ():
681
- if result . dtype == np . bool_ :
685
+ if is_bool_dtype ( result ) :
682
686
result = result .astype ('O' )
683
687
np .putmask (result , mask , np .nan )
684
688
@@ -699,16 +703,16 @@ def unique1d(values):
699
703
"""
700
704
if np .issubdtype (values .dtype , np .floating ):
701
705
table = _hash .Float64HashTable (len (values ))
702
- uniques = np .array (table .unique (com . _ensure_float64 (values )),
706
+ uniques = np .array (table .unique (_ensure_float64 (values )),
703
707
dtype = np .float64 )
704
708
elif np .issubdtype (values .dtype , np .datetime64 ):
705
709
table = _hash .Int64HashTable (len (values ))
706
- uniques = table .unique (com . _ensure_int64 (values ))
710
+ uniques = table .unique (_ensure_int64 (values ))
707
711
uniques = uniques .view ('M8[ns]' )
708
712
elif np .issubdtype (values .dtype , np .integer ):
709
713
table = _hash .Int64HashTable (len (values ))
710
- uniques = table .unique (com . _ensure_int64 (values ))
714
+ uniques = table .unique (_ensure_int64 (values ))
711
715
else :
712
716
table = _hash .PyObjectHashTable (len (values ))
713
- uniques = table .unique (com . _ensure_object (values ))
717
+ uniques = table .unique (_ensure_object (values ))
714
718
return uniques
0 commit comments