1414 is_categorical_dtype ,
1515 is_extension_type ,
1616 is_datetimetz ,
17+ is_period ,
1718 is_period_dtype ,
18- is_period_arraylike ,
1919 is_float_dtype ,
2020 needs_i8_conversion ,
2121 is_categorical ,
@@ -367,7 +367,8 @@ def value_counts(values, sort=True, ascending=False, normalize=False,
367367 raise TypeError ("bins argument only works with numeric data." )
368368 values = cat .codes
369369
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 ))):
371372 # handle Categorical and sparse,
372373 # datetime tz can be handeled in ndarray path
373374 result = Series (values ).values .value_counts (dropna = dropna )
@@ -399,25 +400,14 @@ def value_counts(values, sort=True, ascending=False, normalize=False,
399400
400401def _value_counts_arraylike (values , dropna = True ):
401402 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 )
405404 orig = values
406405
407406 from pandas .core .series import Series
408- values = Series (values ).values
407+ values = Series (values )._values
409408 dtype = values .dtype
410409
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 ):
421411 values = values .view (np .int64 )
422412 keys , counts = htable .value_count_int64 (values , dropna )
423413
@@ -426,13 +416,14 @@ def _value_counts_arraylike(values, dropna=True):
426416 keys , counts = keys [msk ], counts [msk ]
427417
428418 # convert the keys back to the dtype we came in
429- keys = keys .astype (dtype )
430-
431- # dtype handling
432419 if is_datetimetz_type :
420+ from pandas .tseries .index import DatetimeIndex
433421 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 )
436427
437428 elif is_integer_dtype (dtype ):
438429 values = _ensure_int64 (values )
@@ -476,9 +467,6 @@ def duplicated(values, keep='first'):
476467 # no need to revert to original type
477468 if needs_i8_conversion (dtype ):
478469 values = values .view (np .int64 )
479- elif is_period_arraylike (values ):
480- from pandas .tseries .period import PeriodIndex
481- values = PeriodIndex (values ).asi8
482470 elif is_categorical_dtype (dtype ):
483471 values = values .values .codes
484472 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,
10151003 if is_categorical (arr ):
10161004 return arr .take_nd (indexer , fill_value = fill_value ,
10171005 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 )
10201009
10211010 if indexer is None :
10221011 indexer = np .arange (arr .shape [axis ], dtype = np .int64 )
0 commit comments