8
8
9
9
import numpy as np
10
10
11
- from pandas ._libs import algos , hashtable as htable , lib
11
+ from pandas ._libs import Timestamp , algos , hashtable as htable , lib
12
12
from pandas ._libs .tslib import iNaT
13
13
from pandas .util ._decorators import Appender , Substitution , deprecate_kwarg
14
14
@@ -1440,7 +1440,9 @@ def _take_nd_object(arr, indexer, out, axis: int, fill_value, mask_info):
1440
1440
}
1441
1441
1442
1442
1443
- def _get_take_nd_function (ndim , arr_dtype , out_dtype , axis : int = 0 , mask_info = None ):
1443
+ def _get_take_nd_function (
1444
+ ndim : int , arr_dtype , out_dtype , axis : int = 0 , mask_info = None
1445
+ ):
1444
1446
if ndim <= 2 :
1445
1447
tup = (arr_dtype .name , out_dtype .name )
1446
1448
if ndim == 1 :
@@ -1474,7 +1476,7 @@ def func2(arr, indexer, out, fill_value=np.nan):
1474
1476
return func2
1475
1477
1476
1478
1477
- def take (arr , indices , axis = 0 , allow_fill : bool = False , fill_value = None ):
1479
+ def take (arr , indices , axis : int = 0 , allow_fill : bool = False , fill_value = None ):
1478
1480
"""
1479
1481
Take elements from an array.
1480
1482
@@ -1568,13 +1570,7 @@ def take(arr, indices, axis=0, allow_fill: bool = False, fill_value=None):
1568
1570
1569
1571
1570
1572
def take_nd (
1571
- arr ,
1572
- indexer ,
1573
- axis = 0 ,
1574
- out = None ,
1575
- fill_value = np .nan ,
1576
- mask_info = None ,
1577
- allow_fill : bool = True ,
1573
+ arr , indexer , axis : int = 0 , out = None , fill_value = np .nan , allow_fill : bool = True
1578
1574
):
1579
1575
"""
1580
1576
Specialized Cython take which sets NaN values in one pass
@@ -1597,10 +1593,6 @@ def take_nd(
1597
1593
maybe_promote to determine this type for any fill_value
1598
1594
fill_value : any, default np.nan
1599
1595
Fill value to replace -1 values with
1600
- mask_info : tuple of (ndarray, boolean)
1601
- If provided, value should correspond to:
1602
- (indexer != -1, (indexer != -1).any())
1603
- If not provided, it will be computed internally if necessary
1604
1596
allow_fill : boolean, default True
1605
1597
If False, indexer is assumed to contain no -1 values so no filling
1606
1598
will be done. This short-circuits computation of a mask. Result is
@@ -1611,6 +1603,7 @@ def take_nd(
1611
1603
subarray : array-like
1612
1604
May be the same type as the input, or cast to an ndarray.
1613
1605
"""
1606
+ mask_info = None
1614
1607
1615
1608
if is_extension_array_dtype (arr ):
1616
1609
return arr .take (indexer , fill_value = fill_value , allow_fill = allow_fill )
@@ -1632,12 +1625,9 @@ def take_nd(
1632
1625
dtype , fill_value = maybe_promote (arr .dtype , fill_value )
1633
1626
if dtype != arr .dtype and (out is None or out .dtype != dtype ):
1634
1627
# check if promotion is actually required based on indexer
1635
- if mask_info is not None :
1636
- mask , needs_masking = mask_info
1637
- else :
1638
- mask = indexer == - 1
1639
- needs_masking = mask .any ()
1640
- mask_info = mask , needs_masking
1628
+ mask = indexer == - 1
1629
+ needs_masking = mask .any ()
1630
+ mask_info = mask , needs_masking
1641
1631
if needs_masking :
1642
1632
if out is not None and out .dtype != dtype :
1643
1633
raise TypeError ("Incompatible type for fill_value" )
@@ -1818,12 +1808,12 @@ def searchsorted(arr, value, side="left", sorter=None):
1818
1808
elif not (
1819
1809
is_object_dtype (arr ) or is_numeric_dtype (arr ) or is_categorical_dtype (arr )
1820
1810
):
1821
- from pandas .core .series import Series
1822
-
1823
1811
# E.g. if `arr` is an array with dtype='datetime64[ns]'
1824
1812
# and `value` is a pd.Timestamp, we may need to convert value
1825
- value_ser = Series ( value ). _values
1813
+ value_ser = array ([ value ]) if is_scalar ( value ) else array ( value )
1826
1814
value = value_ser [0 ] if is_scalar (value ) else value_ser
1815
+ if isinstance (value , Timestamp ) and value .tzinfo is None :
1816
+ value = value .to_datetime64 ()
1827
1817
1828
1818
result = arr .searchsorted (value , side = side , sorter = sorter )
1829
1819
return result
0 commit comments