11
11
import pandas ._libs .internals as libinternals
12
12
from pandas ._libs .tslibs import Timedelta , conversion
13
13
from pandas ._libs .tslibs .timezones import tz_compare
14
+ from pandas ._typing import ArrayLike
14
15
from pandas .util ._validators import validate_bool_kwarg
15
16
16
17
from pandas .core .dtypes .cast import (
@@ -340,11 +341,12 @@ def iget(self, i):
340
341
341
342
def set (self , locs , values ):
342
343
"""
343
- Modify Block in-place with new item value
344
+ Modify block values in-place with new item value.
344
345
345
- Returns
346
- -------
347
- None
346
+ Notes
347
+ -----
348
+ `set` never creates a new array or new Block, whereas `setitem` _may_
349
+ create a new array and always creates a new Block.
348
350
"""
349
351
self .values [locs ] = values
350
352
@@ -793,7 +795,7 @@ def _replace_single(self, *args, **kwargs):
793
795
794
796
def setitem (self , indexer , value ):
795
797
"""
796
- Set the value inplace, returning a a maybe different typed block .
798
+ Attempt self.values[indexer] = value, possibly creating a new array .
797
799
798
800
Parameters
799
801
----------
@@ -1633,12 +1635,15 @@ def iget(self, col):
1633
1635
raise IndexError (f"{ self } only contains one item" )
1634
1636
return self .values
1635
1637
1636
- def should_store (self , value ):
1638
+ def should_store (self , value : ArrayLike ) -> bool :
1639
+ """
1640
+ Can we set the given array-like value inplace?
1641
+ """
1637
1642
return isinstance (value , self ._holder )
1638
1643
1639
- def set (self , locs , values , check = False ):
1644
+ def set (self , locs , values ):
1640
1645
assert locs .tolist () == [0 ]
1641
- self .values = values
1646
+ self .values [:] = values
1642
1647
1643
1648
def putmask (
1644
1649
self , mask , new , align = True , inplace = False , axis = 0 , transpose = False ,
@@ -1749,7 +1754,7 @@ def is_numeric(self):
1749
1754
1750
1755
def setitem (self , indexer , value ):
1751
1756
"""
1752
- Set the value inplace, returning a same-typed block .
1757
+ Attempt self.values[indexer] = value, possibly creating a new array .
1753
1758
1754
1759
This differs from Block.setitem by not allowing setitem to change
1755
1760
the dtype of the Block.
@@ -2055,7 +2060,7 @@ def to_native_types(
2055
2060
)
2056
2061
return formatter .get_result_as_array ()
2057
2062
2058
- def should_store (self , value ) -> bool :
2063
+ def should_store (self , value : ArrayLike ) -> bool :
2059
2064
# when inserting a column should not coerce integers to floats
2060
2065
# unnecessarily
2061
2066
return issubclass (value .dtype .type , np .floating ) and value .dtype == self .dtype
@@ -2073,7 +2078,7 @@ def _can_hold_element(self, element: Any) -> bool:
2073
2078
element , (float , int , complex , np .float_ , np .int_ )
2074
2079
) and not isinstance (element , (bool , np .bool_ ))
2075
2080
2076
- def should_store (self , value ) -> bool :
2081
+ def should_store (self , value : ArrayLike ) -> bool :
2077
2082
return issubclass (value .dtype .type , np .complexfloating )
2078
2083
2079
2084
@@ -2092,7 +2097,7 @@ def _can_hold_element(self, element: Any) -> bool:
2092
2097
)
2093
2098
return is_integer (element )
2094
2099
2095
- def should_store (self , value ) -> bool :
2100
+ def should_store (self , value : ArrayLike ) -> bool :
2096
2101
return is_integer_dtype (value ) and value .dtype == self .dtype
2097
2102
2098
2103
@@ -2103,6 +2108,9 @@ class DatetimeLikeBlockMixin:
2103
2108
def _holder (self ):
2104
2109
return DatetimeArray
2105
2110
2111
+ def should_store (self , value ):
2112
+ return is_dtype_equal (self .dtype , value .dtype )
2113
+
2106
2114
@property
2107
2115
def fill_value (self ):
2108
2116
return np .datetime64 ("NaT" , "ns" )
@@ -2239,16 +2247,9 @@ def to_native_types(
2239
2247
).reshape (i8values .shape )
2240
2248
return np .atleast_2d (result )
2241
2249
2242
- def should_store (self , value ) -> bool :
2243
- return is_datetime64_dtype (value .dtype )
2244
-
2245
2250
def set (self , locs , values ):
2246
2251
"""
2247
- Modify Block in-place with new item value
2248
-
2249
- Returns
2250
- -------
2251
- None
2252
+ See Block.set.__doc__
2252
2253
"""
2253
2254
values = conversion .ensure_datetime64ns (values , copy = False )
2254
2255
@@ -2272,6 +2273,7 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeBlock):
2272
2273
_can_hold_element = DatetimeBlock ._can_hold_element
2273
2274
to_native_types = DatetimeBlock .to_native_types
2274
2275
fill_value = np .datetime64 ("NaT" , "ns" )
2276
+ should_store = DatetimeBlock .should_store
2275
2277
2276
2278
@property
2277
2279
def _holder (self ):
@@ -2481,9 +2483,6 @@ def fillna(self, value, **kwargs):
2481
2483
)
2482
2484
return super ().fillna (value , ** kwargs )
2483
2485
2484
- def should_store (self , value ) -> bool :
2485
- return is_timedelta64_dtype (value .dtype )
2486
-
2487
2486
def to_native_types (self , slicer = None , na_rep = None , quoting = None , ** kwargs ):
2488
2487
""" convert to our native types format, slicing if desired """
2489
2488
values = self .values
@@ -2525,7 +2524,7 @@ def _can_hold_element(self, element: Any) -> bool:
2525
2524
return issubclass (tipo .type , np .bool_ )
2526
2525
return isinstance (element , (bool , np .bool_ ))
2527
2526
2528
- def should_store (self , value ) -> bool :
2527
+ def should_store (self , value : ArrayLike ) -> bool :
2529
2528
return issubclass (value .dtype .type , np .bool_ ) and not is_extension_array_dtype (
2530
2529
value
2531
2530
)
@@ -2617,7 +2616,7 @@ def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"]
2617
2616
def _can_hold_element (self , element : Any ) -> bool :
2618
2617
return True
2619
2618
2620
- def should_store (self , value ) -> bool :
2619
+ def should_store (self , value : ArrayLike ) -> bool :
2621
2620
return not (
2622
2621
issubclass (
2623
2622
value .dtype .type ,
@@ -2866,6 +2865,9 @@ def __init__(self, values, placement, ndim=None):
2866
2865
def _holder (self ):
2867
2866
return Categorical
2868
2867
2868
+ def should_store (self , arr : ArrayLike ):
2869
+ return isinstance (arr , self ._holder ) and is_dtype_equal (self .dtype , arr .dtype )
2870
+
2869
2871
def to_native_types (self , slicer = None , na_rep = "" , quoting = None , ** kwargs ):
2870
2872
""" convert to our native types format, slicing if desired """
2871
2873
values = self .values
0 commit comments