14
14
is_categorical_dtype ,
15
15
is_extension_type ,
16
16
is_datetimetz ,
17
+ is_period ,
17
18
is_period_dtype ,
18
- is_period_arraylike ,
19
19
is_float_dtype ,
20
20
needs_i8_conversion ,
21
21
is_categorical ,
@@ -367,7 +367,8 @@ def value_counts(values, sort=True, ascending=False, normalize=False,
367
367
raise TypeError ("bins argument only works with numeric data." )
368
368
values = cat .codes
369
369
370
- if is_extension_type (values ) and not is_datetimetz (values ):
370
+ if (is_extension_type (values ) and
371
+ not (is_datetimetz (values ) or is_period (values ))):
371
372
# handle Categorical and sparse,
372
373
# datetime tz can be handeled in ndarray path
373
374
result = Series (values ).values .value_counts (dropna = dropna )
@@ -399,25 +400,14 @@ def value_counts(values, sort=True, ascending=False, normalize=False,
399
400
400
401
def _value_counts_arraylike (values , dropna = True ):
401
402
is_datetimetz_type = is_datetimetz (values )
402
- is_period_type = (is_period_dtype (values ) or
403
- is_period_arraylike (values ))
404
-
403
+ is_period_type = is_period_dtype (values )
405
404
orig = values
406
405
407
406
from pandas .core .series import Series
408
- values = Series (values ).values
407
+ values = Series (values )._values
409
408
dtype = values .dtype
410
409
411
- if needs_i8_conversion (dtype ) or is_period_type :
412
-
413
- from pandas .tseries .index import DatetimeIndex
414
- from pandas .tseries .period import PeriodIndex
415
-
416
- if is_period_type :
417
- # values may be an object
418
- values = PeriodIndex (values )
419
- freq = values .freq
420
-
410
+ if needs_i8_conversion (dtype ):
421
411
values = values .view (np .int64 )
422
412
keys , counts = htable .value_count_int64 (values , dropna )
423
413
@@ -426,13 +416,14 @@ def _value_counts_arraylike(values, dropna=True):
426
416
keys , counts = keys [msk ], counts [msk ]
427
417
428
418
# convert the keys back to the dtype we came in
429
- keys = keys .astype (dtype )
430
-
431
- # dtype handling
432
419
if is_datetimetz_type :
420
+ from pandas .tseries .index import DatetimeIndex
433
421
keys = DatetimeIndex ._simple_new (keys , tz = orig .dtype .tz )
434
- if is_period_type :
435
- keys = PeriodIndex ._simple_new (keys , freq = freq )
422
+ elif is_period_type :
423
+ from pandas .tseries .period import PeriodIndex
424
+ keys = PeriodIndex ._simple_new (keys , freq = orig .dtype .freq )
425
+ else :
426
+ keys = keys .astype (dtype )
436
427
437
428
elif is_integer_dtype (dtype ):
438
429
values = _ensure_int64 (values )
@@ -476,9 +467,6 @@ def duplicated(values, keep='first'):
476
467
# no need to revert to original type
477
468
if needs_i8_conversion (dtype ):
478
469
values = values .view (np .int64 )
479
- elif is_period_arraylike (values ):
480
- from pandas .tseries .period import PeriodIndex
481
- values = PeriodIndex (values ).asi8
482
470
elif is_categorical_dtype (dtype ):
483
471
values = values .values .codes
484
472
elif isinstance (values , (ABCSeries , ABCIndex )):
@@ -1015,8 +1003,9 @@ def take_nd(arr, indexer, axis=0, out=None, fill_value=np.nan, mask_info=None,
1015
1003
if is_categorical (arr ):
1016
1004
return arr .take_nd (indexer , fill_value = fill_value ,
1017
1005
allow_fill = allow_fill )
1018
- elif is_datetimetz (arr ):
1019
- return arr .take (indexer , fill_value = fill_value , allow_fill = allow_fill )
1006
+ elif is_extension_type (arr ):
1007
+ return arr .take (indexer , fill_value = fill_value ,
1008
+ allow_fill = allow_fill )
1020
1009
1021
1010
if indexer is None :
1022
1011
indexer = np .arange (arr .shape [axis ], dtype = np .int64 )
0 commit comments