16
16
is_categorical_dtype ,
17
17
is_extension_type ,
18
18
is_datetimetz ,
19
+ is_period ,
19
20
is_period_dtype ,
20
- is_period_arraylike ,
21
21
is_float_dtype ,
22
22
needs_i8_conversion ,
23
23
is_categorical ,
@@ -410,7 +410,8 @@ def value_counts(values, sort=True, ascending=False, normalize=False,
410
410
raise TypeError ("bins argument only works with numeric data." )
411
411
values = cat .codes
412
412
413
- if is_extension_type (values ) and not is_datetimetz (values ):
413
+ if (is_extension_type (values ) and
414
+ not (is_datetimetz (values ) or is_period (values ))):
414
415
# handle Categorical and sparse,
415
416
# datetime tz can be handeled in ndarray path
416
417
result = Series (values ).values .value_counts (dropna = dropna )
@@ -442,25 +443,14 @@ def value_counts(values, sort=True, ascending=False, normalize=False,
442
443
443
444
def _value_counts_arraylike (values , dropna = True ):
444
445
is_datetimetz_type = is_datetimetz (values )
445
- is_period_type = (is_period_dtype (values ) or
446
- is_period_arraylike (values ))
447
-
446
+ is_period_type = is_period_dtype (values )
448
447
orig = values
449
448
450
449
from pandas .core .series import Series
451
- values = Series (values ).values
450
+ values = Series (values )._values
452
451
dtype = values .dtype
453
452
454
- if needs_i8_conversion (dtype ) or is_period_type :
455
-
456
- from pandas .tseries .index import DatetimeIndex
457
- from pandas .tseries .period import PeriodIndex
458
-
459
- if is_period_type :
460
- # values may be an object
461
- values = PeriodIndex (values )
462
- freq = values .freq
463
-
453
+ if needs_i8_conversion (dtype ):
464
454
values = values .view (np .int64 )
465
455
keys , counts = htable .value_count_int64 (values , dropna )
466
456
@@ -469,13 +459,14 @@ def _value_counts_arraylike(values, dropna=True):
469
459
keys , counts = keys [msk ], counts [msk ]
470
460
471
461
# convert the keys back to the dtype we came in
472
- keys = keys .astype (dtype )
473
-
474
- # dtype handling
475
462
if is_datetimetz_type :
463
+ from pandas .tseries .index import DatetimeIndex
476
464
keys = DatetimeIndex ._simple_new (keys , tz = orig .dtype .tz )
477
- if is_period_type :
478
- keys = PeriodIndex ._simple_new (keys , freq = freq )
465
+ elif is_period_type :
466
+ from pandas .tseries .period import PeriodIndex
467
+ keys = PeriodIndex ._simple_new (keys , freq = orig .dtype .freq )
468
+ else :
469
+ keys = keys .astype (dtype )
479
470
480
471
elif is_integer_dtype (dtype ):
481
472
values = _ensure_int64 (values )
@@ -522,9 +513,6 @@ def duplicated(values, keep='first'):
522
513
# no need to revert to original type
523
514
if needs_i8_conversion (dtype ):
524
515
values = values .view (np .int64 )
525
- elif is_period_arraylike (values ):
526
- from pandas .tseries .period import PeriodIndex
527
- values = PeriodIndex (values ).asi8
528
516
elif is_categorical_dtype (dtype ):
529
517
values = values .values .codes
530
518
elif isinstance (values , (ABCSeries , ABCIndex )):
@@ -1243,8 +1231,9 @@ def take_nd(arr, indexer, axis=0, out=None, fill_value=np.nan, mask_info=None,
1243
1231
if is_categorical (arr ):
1244
1232
return arr .take_nd (indexer , fill_value = fill_value ,
1245
1233
allow_fill = allow_fill )
1246
- elif is_datetimetz (arr ):
1247
- return arr .take (indexer , fill_value = fill_value , allow_fill = allow_fill )
1234
+ elif is_extension_type (arr ):
1235
+ return arr .take (indexer , fill_value = fill_value ,
1236
+ allow_fill = allow_fill )
1248
1237
1249
1238
if indexer is None :
1250
1239
indexer = np .arange (arr .shape [axis ], dtype = np .int64 )
0 commit comments