14
14
is_categorical_dtype ,
15
15
is_extension_type ,
16
16
is_datetimetz ,
17
+ is_period ,
17
18
is_period_dtype ,
18
19
is_period_arraylike ,
19
20
is_float_dtype ,
@@ -285,15 +286,19 @@ def factorize(values, sort=False, order=None, na_sentinel=-1, size_hint=None):
285
286
note: an array of Periods will ignore sort as it returns an always sorted
286
287
PeriodIndex
287
288
"""
288
- from pandas import Index , Series , DatetimeIndex
289
+ from pandas import Index , Series , DatetimeIndex , PeriodIndex
289
290
290
- vals = np .asarray (values )
291
291
292
- # localize to UTC
293
- is_datetimetz_type = is_datetimetz (values )
294
- if is_datetimetz_type :
292
+ if is_datetimetz (values ):
295
293
values = DatetimeIndex (values )
296
- vals = values .asi8
294
+
295
+ if is_period_dtype (values ):
296
+ values = PeriodIndex (values )
297
+ # period array interface goes to object so intercept
298
+ vals = values .view (np .int64 )
299
+ else :
300
+ vals = np .asarray (values )
301
+
297
302
298
303
is_datetime = is_datetime64_dtype (vals )
299
304
is_timedelta = is_timedelta64_dtype (vals )
@@ -311,10 +316,7 @@ def factorize(values, sort=False, order=None, na_sentinel=-1, size_hint=None):
311
316
uniques , labels = safe_sort (uniques , labels , na_sentinel = na_sentinel ,
312
317
assume_unique = True )
313
318
314
- if is_datetimetz_type :
315
- # reset tz
316
- uniques = values ._shallow_copy (uniques )
317
- elif is_datetime :
319
+ if is_datetime :
318
320
uniques = uniques .astype ('M8[ns]' )
319
321
elif is_timedelta :
320
322
uniques = uniques .astype ('m8[ns]' )
0 commit comments