18
18
find_common_type ,
19
19
infer_dtype_from ,
20
20
infer_dtype_from_scalar ,
21
+ maybe_downcast_numeric ,
21
22
maybe_downcast_to_dtype ,
22
23
maybe_infer_dtype_type ,
23
24
maybe_promote ,
55
56
ABCDataFrame ,
56
57
ABCDatetimeIndex ,
57
58
ABCExtensionArray ,
58
- ABCIndexClass ,
59
59
ABCPandasArray ,
60
60
ABCSeries ,
61
61
)
@@ -685,28 +685,6 @@ def _can_hold_element(self, element):
685
685
return issubclass (tipo .type , dtype )
686
686
return isinstance (element , dtype )
687
687
688
- def _try_cast_result (self , result , dtype = None ):
689
- """ try to cast the result to our original type, we may have
690
- roundtripped thru object in the mean-time
691
- """
692
- if dtype is None :
693
- dtype = self .dtype
694
-
695
- if self .is_integer or self .is_bool or self .is_datetime :
696
- pass
697
- elif self .is_float and result .dtype == self .dtype :
698
- # protect against a bool/object showing up here
699
- if isinstance (dtype , str ) and dtype == "infer" :
700
- return result
701
-
702
- # This is only reached via Block.setitem, where dtype is always
703
- # either "infer", self.dtype, or values.dtype.
704
- assert dtype == self .dtype , (dtype , self .dtype )
705
- return result
706
-
707
- # may need to change the dtype here
708
- return maybe_downcast_to_dtype (result , dtype )
709
-
710
688
def _try_coerce_args (self , other ):
711
689
""" provide coercion to our input arguments """
712
690
@@ -729,10 +707,6 @@ def _try_coerce_args(self, other):
729
707
730
708
return other
731
709
732
- def _try_coerce_and_cast_result (self , result , dtype = None ):
733
- result = self ._try_cast_result (result , dtype = dtype )
734
- return result
735
-
736
710
def to_native_types (self , slicer = None , na_rep = "nan" , quoting = None , ** kwargs ):
737
711
""" convert to our native types format, slicing if desired """
738
712
@@ -925,8 +899,6 @@ def setitem(self, indexer, value):
925
899
else :
926
900
values [indexer ] = value
927
901
928
- # coerce and try to infer the dtypes of the result
929
- values = self ._try_coerce_and_cast_result (values , dtype )
930
902
if transpose :
931
903
values = values .T
932
904
block = self .make_block (values )
@@ -1444,10 +1416,6 @@ def func(cond, values, other):
1444
1416
if transpose :
1445
1417
result = result .T
1446
1418
1447
- # try to cast if requested
1448
- if try_cast :
1449
- result = self ._try_cast_result (result )
1450
-
1451
1419
return [self .make_block (result )]
1452
1420
1453
1421
# might need to separate out blocks
@@ -1459,7 +1427,7 @@ def func(cond, values, other):
1459
1427
for m in [mask , ~ mask ]:
1460
1428
if m .any ():
1461
1429
taken = result .take (m .nonzero ()[0 ], axis = axis )
1462
- r = self . _try_cast_result (taken )
1430
+ r = maybe_downcast_numeric (taken , self . dtype )
1463
1431
nb = self .make_block (r .T , placement = self .mgr_locs [m ])
1464
1432
result_blocks .append (nb )
1465
1433
@@ -1692,9 +1660,6 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0, transpose=False)
1692
1660
new_values [mask ] = new
1693
1661
return [self .make_block (values = new_values )]
1694
1662
1695
- def _try_cast_result (self , result , dtype = None ):
1696
- return result
1697
-
1698
1663
def _get_unstack_items (self , unstacker , new_columns ):
1699
1664
"""
1700
1665
Get the placement, values, and mask for a Block unstack.
@@ -1746,7 +1711,8 @@ def __init__(self, values, placement, ndim=None):
1746
1711
super ().__init__ (values , placement , ndim )
1747
1712
1748
1713
def _maybe_coerce_values (self , values ):
1749
- """Unbox to an extension array.
1714
+ """
1715
+ Unbox to an extension array.
1750
1716
1751
1717
This will unbox an ExtensionArray stored in an Index or Series.
1752
1718
ExtensionArrays pass through. No dtype coercion is done.
@@ -1759,9 +1725,7 @@ def _maybe_coerce_values(self, values):
1759
1725
-------
1760
1726
ExtensionArray
1761
1727
"""
1762
- if isinstance (values , (ABCIndexClass , ABCSeries )):
1763
- values = values ._values
1764
- return values
1728
+ return extract_array (values )
1765
1729
1766
1730
@property
1767
1731
def _holder (self ):
0 commit comments