@@ -208,17 +208,15 @@ def array_dtype(self):
208
208
"""
209
209
return self .dtype
210
210
211
- def make_block (self , values , placement = None , ndim = None ):
211
+ def make_block (self , values , placement = None ):
212
212
"""
213
213
Create a new block, with type inference propagate any values that are
214
214
not specified
215
215
"""
216
216
if placement is None :
217
217
placement = self .mgr_locs
218
- if ndim is None :
219
- ndim = self .ndim
220
218
221
- return make_block (values , placement = placement , ndim = ndim )
219
+ return make_block (values , placement = placement , ndim = self . ndim )
222
220
223
221
def make_block_same_class (self , values , placement = None , ndim = None ,
224
222
dtype = None ):
@@ -369,7 +367,9 @@ def fillna(self, value, limit=None, inplace=False, downcast=None):
369
367
370
368
# fillna, but if we cannot coerce, then try again as an ObjectBlock
371
369
try :
372
- values , _ = self ._try_coerce_args (self .values , value )
370
+ # Note: we only call try_coerce_args to let it raise
371
+ self ._try_coerce_args (value )
372
+
373
373
blocks = self .putmask (mask , value , inplace = inplace )
374
374
blocks = [b .make_block (values = self ._try_coerce_result (b .values ))
375
375
for b in blocks ]
@@ -659,7 +659,21 @@ def _try_cast_result(self, result, dtype=None):
659
659
# may need to change the dtype here
660
660
return maybe_downcast_to_dtype (result , dtype )
661
661
662
- def _try_coerce_args (self , values , other ):
662
+ def _coerce_values (self , values ):
663
+ """
664
+ Coerce values (usually derived from self.values) for an operation.
665
+
666
+ Parameters
667
+ ----------
668
+ values : ndarray or ExtensionArray
669
+
670
+ Returns
671
+ -------
672
+ ndarray or ExtensionArray
673
+ """
674
+ return values
675
+
676
+ def _try_coerce_args (self , other ):
663
677
""" provide coercion to our input arguments """
664
678
665
679
if np .any (notna (other )) and not self ._can_hold_element (other ):
@@ -669,7 +683,7 @@ def _try_coerce_args(self, values, other):
669
683
type (other ).__name__ ,
670
684
type (self ).__name__ .lower ().replace ('Block' , '' )))
671
685
672
- return values , other
686
+ return other
673
687
674
688
def _try_coerce_result (self , result ):
675
689
""" reverse of try_coerce_args """
@@ -718,9 +732,9 @@ def replace(self, to_replace, value, inplace=False, filter=None,
718
732
719
733
# try to replace, if we raise an error, convert to ObjectBlock and
720
734
# retry
735
+ values = self ._coerce_values (self .values )
721
736
try :
722
- values , to_replace = self ._try_coerce_args (self .values ,
723
- to_replace )
737
+ to_replace = self ._try_coerce_args (to_replace )
724
738
except (TypeError , ValueError ):
725
739
# GH 22083, TypeError or ValueError occurred within error handling
726
740
# causes infinite loop. Cast and retry only if not objectblock.
@@ -793,7 +807,8 @@ def setitem(self, indexer, value):
793
807
# coerce if block dtype can store value
794
808
values = self .values
795
809
try :
796
- values , value = self ._try_coerce_args (values , value )
810
+ value = self ._try_coerce_args (value )
811
+ values = self ._coerce_values (values )
797
812
# can keep its own dtype
798
813
if hasattr (value , 'dtype' ) and is_dtype_equal (values .dtype ,
799
814
value .dtype ):
@@ -925,7 +940,7 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0,
925
940
new = self .fill_value
926
941
927
942
if self ._can_hold_element (new ):
928
- _ , new = self ._try_coerce_args (new_values , new )
943
+ new = self ._try_coerce_args (new )
929
944
930
945
if transpose :
931
946
new_values = new_values .T
@@ -1127,7 +1142,8 @@ def _interpolate_with_fill(self, method='pad', axis=0, inplace=False,
1127
1142
return [self .copy ()]
1128
1143
1129
1144
values = self .values if inplace else self .values .copy ()
1130
- values , fill_value = self ._try_coerce_args (values , fill_value )
1145
+ values = self ._coerce_values (values )
1146
+ fill_value = self ._try_coerce_args (fill_value )
1131
1147
values = missing .interpolate_2d (values , method = method , axis = axis ,
1132
1148
limit = limit , fill_value = fill_value ,
1133
1149
dtype = self .dtype )
@@ -1298,11 +1314,12 @@ def func(cond, values, other):
1298
1314
if cond .ravel ().all ():
1299
1315
return values
1300
1316
1301
- values , other = self ._try_coerce_args (values , other )
1317
+ values = self ._coerce_values (values )
1318
+ other = self ._try_coerce_args (other )
1302
1319
1303
1320
try :
1304
- return self . _try_coerce_result ( expressions .where (
1305
- cond , values , other ) )
1321
+ fastres = expressions .where (cond , values , other )
1322
+ return self . _try_coerce_result ( fastres )
1306
1323
except Exception as detail :
1307
1324
if errors == 'raise' :
1308
1325
raise TypeError (
@@ -1349,10 +1366,10 @@ def func(cond, values, other):
1349
1366
result_blocks = []
1350
1367
for m in [mask , ~ mask ]:
1351
1368
if m .any ():
1352
- r = self . _try_cast_result ( result .take (m .nonzero ()[0 ],
1353
- axis = axis ) )
1354
- result_blocks . append (
1355
- self . make_block ( r . T , placement = self . mgr_locs [ m ]) )
1369
+ taken = result .take (m .nonzero ()[0 ], axis = axis )
1370
+ r = self . _try_cast_result ( taken )
1371
+ nb = self . make_block ( r . T , placement = self . mgr_locs [ m ])
1372
+ result_blocks . append ( nb )
1356
1373
1357
1374
return result_blocks
1358
1375
@@ -1423,7 +1440,7 @@ def quantile(self, qs, interpolation='linear', axis=0):
1423
1440
values = values [None , :]
1424
1441
else :
1425
1442
values = self .get_values ()
1426
- values , _ = self ._try_coerce_args ( values , values )
1443
+ values = self ._coerce_values ( values )
1427
1444
1428
1445
is_empty = values .shape [axis ] == 0
1429
1446
orig_scalar = not is_list_like (qs )
@@ -1579,7 +1596,8 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0,
1579
1596
# use block's copy logic.
1580
1597
# .values may be an Index which does shallow copy by default
1581
1598
new_values = self .values if inplace else self .copy ().values
1582
- new_values , new = self ._try_coerce_args (new_values , new )
1599
+ new_values = self ._coerce_values (new_values )
1600
+ new = self ._try_coerce_args (new )
1583
1601
1584
1602
if isinstance (new , np .ndarray ) and len (new ) == len (mask ):
1585
1603
new = new [mask ]
@@ -2120,25 +2138,25 @@ def _can_hold_element(self, element):
2120
2138
return (is_integer (element ) or isinstance (element , datetime ) or
2121
2139
isna (element ))
2122
2140
2123
- def _try_coerce_args (self , values , other ):
2141
+ def _coerce_values (self , values ):
2142
+ return values .view ('i8' )
2143
+
2144
+ def _try_coerce_args (self , other ):
2124
2145
"""
2125
- Coerce values and other to dtype 'i8'. NaN and NaT convert to
2146
+ Coerce other to dtype 'i8'. NaN and NaT convert to
2126
2147
the smallest i8, and will correctly round-trip to NaT if converted
2127
2148
back in _try_coerce_result. values is always ndarray-like, other
2128
2149
may not be
2129
2150
2130
2151
Parameters
2131
2152
----------
2132
- values : ndarray-like
2133
2153
other : ndarray-like or scalar
2134
2154
2135
2155
Returns
2136
2156
-------
2137
- base-type values, base-type other
2157
+ base-type other
2138
2158
"""
2139
2159
2140
- values = values .view ('i8' )
2141
-
2142
2160
if isinstance (other , bool ):
2143
2161
raise TypeError
2144
2162
elif is_null_datetimelike (other ):
@@ -2156,7 +2174,7 @@ def _try_coerce_args(self, values, other):
2156
2174
# let higher levels handle
2157
2175
raise TypeError (other )
2158
2176
2159
- return values , other
2177
+ return other
2160
2178
2161
2179
def _try_coerce_result (self , result ):
2162
2180
""" reverse of try_coerce_args """
@@ -2249,13 +2267,6 @@ def is_view(self):
2249
2267
# check the ndarray values of the DatetimeIndex values
2250
2268
return self .values ._data .base is not None
2251
2269
2252
- def copy (self , deep = True ):
2253
- """ copy constructor """
2254
- values = self .values
2255
- if deep :
2256
- values = values .copy ()
2257
- return self .make_block_same_class (values )
2258
-
2259
2270
def get_values (self , dtype = None ):
2260
2271
"""
2261
2272
Returns an ndarray of values.
@@ -2305,21 +2316,22 @@ def _slice(self, slicer):
2305
2316
return self .values [loc ]
2306
2317
return self .values [slicer ]
2307
2318
2308
- def _try_coerce_args (self , values , other ):
2319
+ def _coerce_values (self , values ):
2320
+ # asi8 is a view, needs copy
2321
+ return _block_shape (values .view ("i8" ), ndim = self .ndim )
2322
+
2323
+ def _try_coerce_args (self , other ):
2309
2324
"""
2310
2325
localize and return i8 for the values
2311
2326
2312
2327
Parameters
2313
2328
----------
2314
- values : ndarray-like
2315
2329
other : ndarray-like or scalar
2316
2330
2317
2331
Returns
2318
2332
-------
2319
- base-type values, base-type other
2333
+ base-type other
2320
2334
"""
2321
- # asi8 is a view, needs copy
2322
- values = _block_shape (values .view ("i8" ), ndim = self .ndim )
2323
2335
2324
2336
if isinstance (other , ABCSeries ):
2325
2337
other = self ._holder (other )
@@ -2347,7 +2359,7 @@ def _try_coerce_args(self, values, other):
2347
2359
else :
2348
2360
raise TypeError (other )
2349
2361
2350
- return values , other
2362
+ return other
2351
2363
2352
2364
def _try_coerce_result (self , result ):
2353
2365
""" reverse of try_coerce_args """
@@ -2488,21 +2500,22 @@ def fillna(self, value, **kwargs):
2488
2500
value = Timedelta (value , unit = 's' )
2489
2501
return super ().fillna (value , ** kwargs )
2490
2502
2491
- def _try_coerce_args (self , values , other ):
2503
+ def _coerce_values (self , values ):
2504
+ return values .view ('i8' )
2505
+
2506
+ def _try_coerce_args (self , other ):
2492
2507
"""
2493
2508
Coerce values and other to int64, with null values converted to
2494
2509
iNaT. values is always ndarray-like, other may not be
2495
2510
2496
2511
Parameters
2497
2512
----------
2498
- values : ndarray-like
2499
2513
other : ndarray-like or scalar
2500
2514
2501
2515
Returns
2502
2516
-------
2503
- base-type values, base-type other
2517
+ base-type other
2504
2518
"""
2505
- values = values .view ('i8' )
2506
2519
2507
2520
if isinstance (other , bool ):
2508
2521
raise TypeError
@@ -2517,7 +2530,7 @@ def _try_coerce_args(self, values, other):
2517
2530
# let higher levels handle
2518
2531
raise TypeError (other )
2519
2532
2520
- return values , other
2533
+ return other
2521
2534
2522
2535
def _try_coerce_result (self , result ):
2523
2536
""" reverse of try_coerce_args / try_operate """
@@ -2688,7 +2701,7 @@ def _maybe_downcast(self, blocks, downcast=None):
2688
2701
def _can_hold_element (self , element ):
2689
2702
return True
2690
2703
2691
- def _try_coerce_args (self , values , other ):
2704
+ def _try_coerce_args (self , other ):
2692
2705
""" provide coercion to our input arguments """
2693
2706
2694
2707
if isinstance (other , ABCDatetimeIndex ):
@@ -2701,7 +2714,7 @@ def _try_coerce_args(self, values, other):
2701
2714
# when falling back to ObjectBlock.where
2702
2715
other = other .astype (object )
2703
2716
2704
- return values , other
2717
+ return other
2705
2718
2706
2719
def should_store (self , value ):
2707
2720
return not (issubclass (value .dtype .type ,
0 commit comments