@@ -2431,8 +2431,26 @@ def remove_na(series):
2431
2431
return series [notnull (_values_from_object (series ))]
2432
2432
2433
2433
2434
+ def _sanitize_index (data , index , copy = False ):
2435
+ """ sanitize an index type to return an ndarray of the underlying, pass thru a non-Index """
2436
+
2437
+ if len (data ) != len (index ):
2438
+ raise ValueError ('Length of values does not match length of '
2439
+ 'index' )
2440
+
2441
+ if isinstance (data , PeriodIndex ):
2442
+ data = data .asobject
2443
+ elif isinstance (data , DatetimeIndex ):
2444
+ data = data ._to_embed (keep_tz = True )
2445
+ if copy :
2446
+ data = data .copy ()
2447
+
2448
+ return data
2449
+
2434
2450
def _sanitize_array (data , index , dtype = None , copy = False ,
2435
2451
raise_cast_failure = False ):
2452
+ """ sanitize input data to an ndarray, copy if specified, coerce to the dtype if specified """
2453
+
2436
2454
if dtype is not None :
2437
2455
dtype = np .dtype (dtype )
2438
2456
@@ -2482,11 +2500,13 @@ def _try_cast(arr, take_fast_path):
2482
2500
raise TypeError ('Cannot cast datetime64 to %s' % dtype )
2483
2501
else :
2484
2502
subarr = _try_cast (data , True )
2485
- else :
2503
+ elif isinstance ( data , Index ) :
2486
2504
# don't coerce Index types
2487
2505
# e.g. indexes can have different conversions (so don't fast path them)
2488
2506
# GH 6140
2489
- subarr = _try_cast (data , not isinstance (data , Index ))
2507
+ subarr = _sanitize_index (data , index , copy = True )
2508
+ else :
2509
+ subarr = _try_cast (data , True )
2490
2510
2491
2511
if copy :
2492
2512
subarr = data .copy ()
0 commit comments