7
7
8
8
import numpy as np
9
9
10
- from pandas ._libs import NaT , Timestamp , lib , tslib , tslibs
10
+ from pandas ._libs import NaT , Timestamp , lib , tslib
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
@@ -407,7 +407,7 @@ def fillna(self, value, limit=None, inplace=False, downcast=None):
407
407
return self .copy ()
408
408
409
409
if self ._can_hold_element (value ):
410
- # equivalent: self. _try_coerce_args(value) would not raise
410
+ # equivalent: _try_coerce_args(value) would not raise
411
411
blocks = self .putmask (mask , value , inplace = inplace )
412
412
return self ._maybe_downcast (blocks , downcast )
413
413
@@ -669,7 +669,7 @@ def convert(
669
669
670
670
return self .copy () if copy else self
671
671
672
- def _can_hold_element (self , element ) :
672
+ def _can_hold_element (self , element : Any ) -> bool :
673
673
""" require the same dtype as ourselves """
674
674
dtype = self .values .dtype .type
675
675
tipo = maybe_infer_dtype_type (element )
@@ -857,12 +857,6 @@ def setitem(self, indexer, value):
857
857
if self ._can_hold_element (value ):
858
858
value = self ._try_coerce_args (value )
859
859
860
- # can keep its own dtype
861
- if hasattr (value , "dtype" ) and is_dtype_equal (values .dtype , value .dtype ):
862
- dtype = self .dtype
863
- else :
864
- dtype = "infer"
865
-
866
860
else :
867
861
# current dtype cannot store value, coerce to common dtype
868
862
find_dtype = False
@@ -871,15 +865,9 @@ def setitem(self, indexer, value):
871
865
dtype = value .dtype
872
866
find_dtype = True
873
867
874
- elif lib .is_scalar (value ):
875
- if isna (value ):
876
- # NaN promotion is handled in latter path
877
- dtype = False
878
- else :
879
- dtype , _ = infer_dtype_from_scalar (value , pandas_dtype = True )
880
- find_dtype = True
881
- else :
882
- dtype = "infer"
868
+ elif lib .is_scalar (value ) and not isna (value ):
869
+ dtype , _ = infer_dtype_from_scalar (value , pandas_dtype = True )
870
+ find_dtype = True
883
871
884
872
if find_dtype :
885
873
dtype = find_common_type ([values .dtype , dtype ])
@@ -1088,7 +1076,7 @@ def coerce_to_target_dtype(self, other):
1088
1076
mytz = getattr (self .dtype , "tz" , None )
1089
1077
othertz = getattr (dtype , "tz" , None )
1090
1078
1091
- if str (mytz ) != str ( othertz ):
1079
+ if not tz_compare (mytz , othertz ):
1092
1080
return self .astype (object )
1093
1081
1094
1082
raise AssertionError (
@@ -1308,7 +1296,7 @@ def take_nd(self, indexer, axis, new_mgr_locs=None, fill_tuple=None):
1308
1296
else :
1309
1297
return self .make_block_same_class (new_values , new_mgr_locs )
1310
1298
1311
- def diff (self , n , axis = 1 ) :
1299
+ def diff (self , n : int , axis : int = 1 ) -> List [ "Block" ] :
1312
1300
""" return block for the diff of the values """
1313
1301
new_values = algos .diff (self .values , n , axis = axis )
1314
1302
return [self .make_block (values = new_values )]
@@ -1397,7 +1385,7 @@ def func(cond, values, other):
1397
1385
1398
1386
if not (
1399
1387
(self .is_integer or self .is_bool )
1400
- and lib .is_scalar (other )
1388
+ and lib .is_float (other )
1401
1389
and np .isnan (other )
1402
1390
):
1403
1391
# np.where will cast integer array to floats in this case
@@ -1450,7 +1438,7 @@ def func(cond, values, other):
1450
1438
1451
1439
return result_blocks
1452
1440
1453
- def equals (self , other ):
1441
+ def equals (self , other ) -> bool :
1454
1442
if self .dtype != other .dtype or self .shape != other .shape :
1455
1443
return False
1456
1444
return array_equivalent (self .values , other .values )
@@ -1830,7 +1818,7 @@ def take_nd(self, indexer, axis=0, new_mgr_locs=None, fill_tuple=None):
1830
1818
1831
1819
return self .make_block_same_class (new_values , new_mgr_locs )
1832
1820
1833
- def _can_hold_element (self , element ) :
1821
+ def _can_hold_element (self , element : Any ) -> bool :
1834
1822
# XXX: We may need to think about pushing this onto the array.
1835
1823
# We're doing the same as CategoricalBlock here.
1836
1824
return True
@@ -2000,7 +1988,7 @@ class NumericBlock(Block):
2000
1988
class FloatOrComplexBlock (NumericBlock ):
2001
1989
__slots__ = ()
2002
1990
2003
- def equals (self , other ):
1991
+ def equals (self , other ) -> bool :
2004
1992
if self .dtype != other .dtype or self .shape != other .shape :
2005
1993
return False
2006
1994
left , right = self .values , other .values
@@ -2011,7 +1999,7 @@ class FloatBlock(FloatOrComplexBlock):
2011
1999
__slots__ = ()
2012
2000
is_float = True
2013
2001
2014
- def _can_hold_element (self , element ) :
2002
+ def _can_hold_element (self , element : Any ) -> bool :
2015
2003
tipo = maybe_infer_dtype_type (element )
2016
2004
if tipo is not None :
2017
2005
return issubclass (tipo .type , (np .floating , np .integer )) and not issubclass (
@@ -2075,7 +2063,7 @@ class ComplexBlock(FloatOrComplexBlock):
2075
2063
__slots__ = ()
2076
2064
is_complex = True
2077
2065
2078
- def _can_hold_element (self , element ) :
2066
+ def _can_hold_element (self , element : Any ) -> bool :
2079
2067
tipo = maybe_infer_dtype_type (element )
2080
2068
if tipo is not None :
2081
2069
return issubclass (tipo .type , (np .floating , np .integer , np .complexfloating ))
@@ -2092,7 +2080,7 @@ class IntBlock(NumericBlock):
2092
2080
is_integer = True
2093
2081
_can_hold_na = False
2094
2082
2095
- def _can_hold_element (self , element ) :
2083
+ def _can_hold_element (self , element : Any ) -> bool :
2096
2084
tipo = maybe_infer_dtype_type (element )
2097
2085
if tipo is not None :
2098
2086
return (
@@ -2182,7 +2170,7 @@ def _astype(self, dtype, **kwargs):
2182
2170
# delegate
2183
2171
return super ()._astype (dtype = dtype , ** kwargs )
2184
2172
2185
- def _can_hold_element (self , element ) :
2173
+ def _can_hold_element (self , element : Any ) -> bool :
2186
2174
tipo = maybe_infer_dtype_type (element )
2187
2175
if tipo is not None :
2188
2176
if self .is_datetimetz :
@@ -2372,41 +2360,19 @@ def _slice(self, slicer):
2372
2360
return self .values [slicer ]
2373
2361
2374
2362
def _try_coerce_args (self , other ):
2375
- """
2376
- localize and return i8 for the values
2377
-
2378
- Parameters
2379
- ----------
2380
- other : ndarray-like or scalar
2381
-
2382
- Returns
2383
- -------
2384
- base-type other
2385
- """
2386
- if is_valid_nat_for_dtype (other , self .dtype ):
2387
- other = np .datetime64 ("NaT" , "ns" )
2388
- elif isinstance (other , self ._holder ):
2389
- if not tz_compare (other .tz , self .values .tz ):
2390
- raise ValueError ("incompatible or non tz-aware value" )
2391
-
2392
- elif isinstance (other , (np .datetime64 , datetime , date )):
2393
- other = tslibs .Timestamp (other )
2394
-
2395
- # test we can have an equal time zone
2396
- if not tz_compare (other .tz , self .values .tz ):
2397
- raise ValueError ("incompatible or non tz-aware value" )
2398
- else :
2399
- raise TypeError (other )
2400
-
2363
+ # DatetimeArray handles this for us
2401
2364
return other
2402
2365
2403
- def diff (self , n , axis = 0 ):
2404
- """1st discrete difference
2366
+ def diff (self , n : int , axis : int = 0 ) -> List ["Block" ]:
2367
+ """
2368
+ 1st discrete difference.
2405
2369
2406
2370
Parameters
2407
2371
----------
2408
- n : int, number of periods to diff
2409
- axis : int, axis to diff upon. default 0
2372
+ n : int
2373
+ Number of periods to diff.
2374
+ axis : int, default 0
2375
+ Axis to diff upon.
2410
2376
2411
2377
Returns
2412
2378
-------
@@ -2468,7 +2434,7 @@ def setitem(self, indexer, value):
2468
2434
)
2469
2435
return newb .setitem (indexer , value )
2470
2436
2471
- def equals (self , other ):
2437
+ def equals (self , other ) -> bool :
2472
2438
# override for significant performance improvement
2473
2439
if self .dtype != other .dtype or self .shape != other .shape :
2474
2440
return False
@@ -2507,7 +2473,7 @@ def __init__(self, values, placement, ndim=None):
2507
2473
def _holder (self ):
2508
2474
return TimedeltaArray
2509
2475
2510
- def _can_hold_element (self , element ) :
2476
+ def _can_hold_element (self , element : Any ) -> bool :
2511
2477
tipo = maybe_infer_dtype_type (element )
2512
2478
if tipo is not None :
2513
2479
return issubclass (tipo .type , np .timedelta64 )
@@ -2600,7 +2566,7 @@ class BoolBlock(NumericBlock):
2600
2566
is_bool = True
2601
2567
_can_hold_na = False
2602
2568
2603
- def _can_hold_element (self , element ) :
2569
+ def _can_hold_element (self , element : Any ) -> bool :
2604
2570
tipo = maybe_infer_dtype_type (element )
2605
2571
if tipo is not None :
2606
2572
return issubclass (tipo .type , np .bool_ )
@@ -2694,7 +2660,7 @@ def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"]
2694
2660
# split and convert the blocks
2695
2661
return _extend_blocks ([b .convert (datetime = True , numeric = False ) for b in blocks ])
2696
2662
2697
- def _can_hold_element (self , element ) :
2663
+ def _can_hold_element (self , element : Any ) -> bool :
2698
2664
return True
2699
2665
2700
2666
def _try_coerce_args (self , other ):
0 commit comments