@@ -125,15 +125,9 @@ def reindex_axis(self, indexer, mask, needs_masking, axis=0,
125
125
"""
126
126
Reindex using pre-computed indexer information
127
127
"""
128
- if self .values .size > 0 :
129
- new_values = com .take_fast (self .values , indexer , mask ,
130
- needs_masking , axis = axis ,
131
- fill_value = fill_value )
132
- else :
133
- shape = list (self .shape )
134
- shape [axis ] = len (indexer )
135
- new_values = np .empty (shape )
136
- new_values .fill (fill_value )
128
+ new_values = com .take_fast (self .values , indexer ,
129
+ mask , needs_masking , axis = axis ,
130
+ fill_value = fill_value )
137
131
return make_block (new_values , self .items , self .ref_items )
138
132
139
133
def reindex_items_from (self , new_ref_items , copy = True ):
@@ -155,12 +149,9 @@ def reindex_items_from(self, new_ref_items, copy=True):
155
149
mask = indexer != - 1
156
150
masked_idx = indexer [mask ]
157
151
158
- if self .values .ndim == 2 :
159
- new_values = com .take_2d (self .values , masked_idx , axis = 0 ,
160
- needs_masking = False )
161
- else :
162
- new_values = self .values .take (masked_idx , axis = 0 )
163
-
152
+ new_values = com .take_fast (self .values , masked_idx ,
153
+ mask = None , needs_masking = False ,
154
+ axis = 0 )
164
155
new_items = self .items .take (masked_idx )
165
156
return make_block (new_values , new_items , new_ref_items )
166
157
@@ -301,24 +292,23 @@ def putmask(self, mask, new, inplace=False):
301
292
new_values = self .values if inplace else self .values .copy ()
302
293
303
294
# may need to align the new
304
- if hasattr (new ,'reindex_axis' ):
305
- axis = getattr (new ,'_het_axis' ,0 )
295
+ if hasattr (new , 'reindex_axis' ):
296
+ axis = getattr (new , '_het_axis' , 0 )
306
297
new = new .reindex_axis (self .items , axis = axis , copy = False ).values .T
307
298
308
299
# may need to align the mask
309
- if hasattr (mask ,'reindex_axis' ):
310
- axis = getattr (mask ,'_het_axis' ,0 )
300
+ if hasattr (mask , 'reindex_axis' ):
301
+ axis = getattr (mask , '_het_axis' , 0 )
311
302
mask = mask .reindex_axis (self .items , axis = axis , copy = False ).values .T
312
303
313
304
if self ._can_hold_element (new ):
314
305
new = self ._try_cast (new )
315
306
np .putmask (new_values , mask , new )
316
-
317
307
# upcast me
318
308
else :
319
-
320
309
# type of the new block
321
- if isinstance (new ,np .ndarray ) and issubclass (new .dtype ,np .number ) or issubclass (type (new ),float ):
310
+ if ((isinstance (new , np .ndarray ) and issubclass (new .dtype , np .number )) or
311
+ isinstance (new , float )):
322
312
typ = float
323
313
else :
324
314
typ = object
@@ -369,9 +359,8 @@ def interpolate(self, method='pad', axis=0, inplace=False,
369
359
def take (self , indexer , axis = 1 , fill_value = np .nan ):
370
360
if axis < 1 :
371
361
raise AssertionError ('axis must be at least 1, got %d' % axis )
372
- new_values = com .take_fast (self .values , indexer , None ,
373
- None , axis = axis ,
374
- fill_value = fill_value )
362
+ new_values = com .take_fast (self .values , indexer , None , False ,
363
+ axis = axis , fill_value = fill_value )
375
364
return make_block (new_values , self .items , self .ref_items )
376
365
377
366
def get_values (self , dtype ):
@@ -401,22 +390,21 @@ def where(self, func, other, cond = None, raise_on_error = True, try_cast = Fals
401
390
402
391
Parameters
403
392
----------
404
- func : how to combine self,other
393
+ func : how to combine self, other
405
394
other : a ndarray/object
406
395
cond : the condition to respect, optional
407
- raise_on_error : if True, raise when I can't perform the function, False by default (and just return
408
- the data that we had coming in)
396
+ raise_on_error : if True, raise when I can't perform the function,
397
+ False by default (and just return the data that we had coming in)
409
398
410
399
Returns
411
400
-------
412
401
a new block, the result of the func
413
402
"""
414
-
415
403
values = self .values
416
404
417
405
# see if we can align other
418
- if hasattr (other ,'reindex_axis' ):
419
- axis = getattr (other ,'_het_axis' ,0 )
406
+ if hasattr (other , 'reindex_axis' ):
407
+ axis = getattr (other , '_het_axis' , 0 )
420
408
other = other .reindex_axis (self .items , axis = axis , copy = True ).values
421
409
422
410
# make sure that we can broadcast
@@ -428,17 +416,20 @@ def where(self, func, other, cond = None, raise_on_error = True, try_cast = Fals
428
416
429
417
# see if we can align cond
430
418
if cond is not None :
431
- if not hasattr (cond ,'shape' ):
432
- raise ValueError ("where must have a condition that is ndarray like" )
433
- if hasattr (cond ,'reindex_axis' ):
434
- axis = getattr (cond ,'_het_axis' ,0 )
435
- cond = cond .reindex_axis (self .items , axis = axis , copy = True ).values
419
+ if not hasattr (cond , 'shape' ):
420
+ raise ValueError ('where must have a condition that is ndarray'
421
+ ' like' )
422
+ if hasattr (cond , 'reindex_axis' ):
423
+ axis = getattr (cond , '_het_axis' , 0 )
424
+ cond = cond .reindex_axis (self .items , axis = axis ,
425
+ copy = True ).values
436
426
else :
437
427
cond = cond .values
438
428
439
429
# may need to undo transpose of values
440
430
if hasattr (values , 'ndim' ):
441
- if values .ndim != cond .ndim or values .shape == cond .shape [::- 1 ]:
431
+ if (values .ndim != cond .ndim or
432
+ values .shape == cond .shape [::- 1 ]):
442
433
values = values .T
443
434
is_transposed = not is_transposed
444
435
@@ -494,7 +485,7 @@ class FloatBlock(NumericBlock):
494
485
495
486
def _can_hold_element (self , element ):
496
487
if isinstance (element , np .ndarray ):
497
- return issubclass (element .dtype .type , (np .floating ,np .integer ))
488
+ return issubclass (element .dtype .type , (np .floating , np .integer ))
498
489
return isinstance (element , (float , int ))
499
490
500
491
def _try_cast (self , element ):
@@ -541,7 +532,8 @@ def _try_cast(self, element):
541
532
def _try_cast_result (self , result ):
542
533
# this is quite restrictive to convert
543
534
try :
544
- if isinstance (result , np .ndarray ) and issubclass (result .dtype .type , np .floating ):
535
+ if (isinstance (result , np .ndarray ) and
536
+ issubclass (result .dtype .type , np .floating )):
545
537
if com .notnull (result ).all ():
546
538
new_result = result .astype (self .dtype )
547
539
if (new_result == result ).all ():
@@ -958,7 +950,8 @@ def _get_clean_block_types(self, type_list):
958
950
return type_list
959
951
960
952
def get_bool_data (self , copy = False , as_blocks = False ):
961
- return self .get_numeric_data (copy = copy , type_list = (BoolBlock ,), as_blocks = as_blocks )
953
+ return self .get_numeric_data (copy = copy , type_list = (BoolBlock ,),
954
+ as_blocks = as_blocks )
962
955
963
956
def get_slice (self , slobj , axis = 0 ):
964
957
new_axes = list (self .axes )
@@ -1429,7 +1422,7 @@ def take(self, indexer, axis=1):
1429
1422
if axis == 0 :
1430
1423
raise NotImplementedError
1431
1424
1432
- indexer = np . asarray (indexer , dtype = 'i4' )
1425
+ indexer = com . _ensure_platform_int (indexer )
1433
1426
1434
1427
n = len (self .axes [axis ])
1435
1428
if ((indexer == - 1 ) | (indexer >= n )).any ():
@@ -1440,8 +1433,8 @@ def take(self, indexer, axis=1):
1440
1433
new_axes [axis ] = self .axes [axis ].take (indexer )
1441
1434
new_blocks = []
1442
1435
for blk in self .blocks :
1443
- new_values = com .take_fast (blk .values , indexer ,
1444
- None , False , axis = axis )
1436
+ new_values = com .take_fast (blk .values , indexer , None , False ,
1437
+ axis = axis )
1445
1438
newb = make_block (new_values , blk .items , self .items )
1446
1439
new_blocks .append (newb )
1447
1440
@@ -1765,8 +1758,6 @@ def _consolidate(blocks, items):
1765
1758
return new_blocks
1766
1759
1767
1760
1768
- # TODO: this could be much optimized
1769
-
1770
1761
def _merge_blocks (blocks , items ):
1771
1762
if len (blocks ) == 1 :
1772
1763
return blocks [0 ]
0 commit comments