Skip to content

Commit 3f43a82

Browse files
jbrockmendelyeshsurya
authored andcommitted
TYP: annotations (pandas-dev#40955)
1 parent c4de2d1 commit 3f43a82

File tree

6 files changed

+99
-156
lines changed

6 files changed

+99
-156
lines changed

pandas/_libs/lib.pyi

+7-51
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ from typing import (
55
Any,
66
Callable,
77
Generator,
8-
Literal,
9-
overload,
108
)
119

1210
import numpy as np
@@ -53,65 +51,23 @@ def is_float_array(values: np.ndarray, skipna: bool = False): ...
5351
def is_integer_array(values: np.ndarray, skipna: bool = False): ...
5452
def is_bool_array(values: np.ndarray, skipna: bool = False): ...
5553

56-
def fast_multiget(mapping: dict, keys: np.ndarray, default=np.nan) -> np.ndarray: ...
54+
def fast_multiget(mapping: dict, keys: np.ndarray, default=np.nan) -> ArrayLike: ...
5755

5856
def fast_unique_multiple_list_gen(gen: Generator, sort: bool = True) -> list: ...
5957
def fast_unique_multiple_list(lists: list, sort: bool = True) -> list: ...
6058
def fast_unique_multiple(arrays: list, sort: bool = True) -> list: ...
6159

6260
def map_infer(
6361
arr: np.ndarray, f: Callable[[Any], Any], convert: bool = True, ignore_na: bool = False
64-
) -> np.ndarray: ...
65-
66-
67-
@overload # both convert_datetime and convert_to_nullable_integer False -> np.ndarray
68-
def maybe_convert_objects(
69-
objects: np.ndarray, # np.ndarray[object]
70-
try_float: bool = ...,
71-
safe: bool = ...,
72-
convert_datetime: Literal[False] = ...,
73-
convert_timedelta: bool = ...,
74-
convert_to_nullable_integer: Literal[False] = ...,
75-
) -> np.ndarray: ...
76-
77-
@overload
78-
def maybe_convert_objects(
79-
objects: np.ndarray, # np.ndarray[object]
80-
try_float: bool = ...,
81-
safe: bool = ...,
82-
convert_datetime: Literal[False] = False,
83-
convert_timedelta: bool = ...,
84-
convert_to_nullable_integer: Literal[True] = ...,
8562
) -> ArrayLike: ...
8663

87-
@overload
8864
def maybe_convert_objects(
8965
objects: np.ndarray, # np.ndarray[object]
90-
try_float: bool = ...,
91-
safe: bool = ...,
92-
convert_datetime: Literal[True] = ...,
93-
convert_timedelta: bool = ...,
94-
convert_to_nullable_integer: Literal[False] = ...,
95-
) -> ArrayLike: ...
96-
97-
@overload
98-
def maybe_convert_objects(
99-
objects: np.ndarray, # np.ndarray[object]
100-
try_float: bool = ...,
101-
safe: bool = ...,
102-
convert_datetime: Literal[True] = ...,
103-
convert_timedelta: bool = ...,
104-
convert_to_nullable_integer: Literal[True] = ...,
105-
) -> ArrayLike: ...
106-
107-
@overload
108-
def maybe_convert_objects(
109-
objects: np.ndarray, # np.ndarray[object]
110-
try_float: bool = ...,
111-
safe: bool = ...,
112-
convert_datetime: bool = ...,
113-
convert_timedelta: bool = ...,
114-
convert_to_nullable_integer: bool = ...,
66+
try_float: bool = False,
67+
safe: bool = False,
68+
convert_datetime: bool = False,
69+
convert_timedelta: bool = False,
70+
convert_to_nullable_integer: bool = False,
11571
) -> ArrayLike: ...
11672

11773
def maybe_convert_numeric(
@@ -184,7 +140,7 @@ def map_infer_mask(
184140
convert: bool = ...,
185141
na_value: Any = ...,
186142
dtype: np.dtype = ...,
187-
) -> np.ndarray: ...
143+
) -> ArrayLike: ...
188144

189145
def indices_fast(
190146
index: np.ndarray, # ndarray[intp_t]

pandas/_libs/lib.pyx

+32-66
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ cdef extern from "numpy/arrayobject.h":
6868
object fields
6969
tuple names
7070

71-
cdef extern from "numpy/ndarrayobject.h":
72-
bint PyArray_CheckScalar(obj) nogil
73-
7471

7572
cdef extern from "src/parse_helper.h":
7673
int floatify(object, float64_t *result, int *maybe_int) except -1
@@ -212,24 +209,6 @@ def is_scalar(val: object) -> bool:
212209
or is_offset_object(val))
213210

214211

215-
cdef inline int64_t get_itemsize(object val):
216-
"""
217-
Get the itemsize of a NumPy scalar, -1 if not a NumPy scalar.
218-
219-
Parameters
220-
----------
221-
val : object
222-
223-
Returns
224-
-------
225-
is_ndarray : bool
226-
"""
227-
if PyArray_CheckScalar(val):
228-
return cnp.PyArray_DescrFromScalar(val).itemsize
229-
else:
230-
return -1
231-
232-
233212
def is_iterator(obj: object) -> bool:
234213
"""
235214
Check if the object is an iterator.
@@ -2209,7 +2188,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False,
22092188

22102189
Parameters
22112190
----------
2212-
objects : ndarray[object]
2191+
values : ndarray[object]
22132192
Array of object elements to convert.
22142193
try_float : bool, default False
22152194
If an array-like object contains only float or NaN values is
@@ -2233,7 +2212,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False,
22332212
Array of converted object values to more specific dtypes if applicable.
22342213
"""
22352214
cdef:
2236-
Py_ssize_t i, n, itemsize_max = 0
2215+
Py_ssize_t i, n
22372216
ndarray[float64_t] floats
22382217
ndarray[complex128_t] complexes
22392218
ndarray[int64_t] ints
@@ -2266,10 +2245,6 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False,
22662245

22672246
for i in range(n):
22682247
val = objects[i]
2269-
if itemsize_max != -1:
2270-
itemsize = get_itemsize(val)
2271-
if itemsize > itemsize_max or itemsize == -1:
2272-
itemsize_max = itemsize
22732248

22742249
if val is None:
22752250
seen.null_ = True
@@ -2371,101 +2346,92 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False,
23712346
seen.object_ = True
23722347

23732348
if not seen.object_:
2374-
result = None
23752349
if not safe:
23762350
if seen.null_ or seen.nan_:
23772351
if seen.is_float_or_complex:
23782352
if seen.complex_:
2379-
result = complexes
2353+
return complexes
23802354
elif seen.float_:
2381-
result = floats
2355+
return floats
23822356
elif seen.int_:
23832357
if convert_to_nullable_integer:
23842358
from pandas.core.arrays import IntegerArray
2385-
result = IntegerArray(ints, mask)
2359+
return IntegerArray(ints, mask)
23862360
else:
2387-
result = floats
2361+
return floats
23882362
elif seen.nan_:
2389-
result = floats
2363+
return floats
23902364
else:
23912365
if not seen.bool_:
23922366
if seen.datetime_:
23932367
if not seen.numeric_ and not seen.timedelta_:
2394-
result = datetimes
2368+
return datetimes
23952369
elif seen.timedelta_:
23962370
if not seen.numeric_:
2397-
result = timedeltas
2371+
return timedeltas
23982372
elif seen.nat_:
23992373
if not seen.numeric_:
24002374
if convert_datetime and convert_timedelta:
24012375
# TODO: array full of NaT ambiguity resolve here needed
24022376
pass
24032377
elif convert_datetime:
2404-
result = datetimes
2378+
return datetimes
24052379
elif convert_timedelta:
2406-
result = timedeltas
2380+
return timedeltas
24072381
else:
24082382
if seen.complex_:
2409-
result = complexes
2383+
return complexes
24102384
elif seen.float_:
2411-
result = floats
2385+
return floats
24122386
elif seen.int_:
24132387
if seen.uint_:
2414-
result = uints
2388+
return uints
24152389
else:
2416-
result = ints
2390+
return ints
24172391
elif seen.is_bool:
2418-
result = bools.view(np.bool_)
2392+
return bools.view(np.bool_)
24192393

24202394
else:
24212395
# don't cast int to float, etc.
24222396
if seen.null_:
24232397
if seen.is_float_or_complex:
24242398
if seen.complex_:
24252399
if not seen.int_:
2426-
result = complexes
2400+
return complexes
24272401
elif seen.float_ or seen.nan_:
24282402
if not seen.int_:
2429-
result = floats
2403+
return floats
24302404
else:
24312405
if not seen.bool_:
24322406
if seen.datetime_:
24332407
if not seen.numeric_ and not seen.timedelta_:
2434-
result = datetimes
2408+
return datetimes
24352409
elif seen.timedelta_:
24362410
if not seen.numeric_:
2437-
result = timedeltas
2411+
return timedeltas
24382412
elif seen.nat_:
24392413
if not seen.numeric_:
24402414
if convert_datetime and convert_timedelta:
24412415
# TODO: array full of NaT ambiguity resolve here needed
24422416
pass
24432417
elif convert_datetime:
2444-
result = datetimes
2418+
return datetimes
24452419
elif convert_timedelta:
2446-
result = timedeltas
2420+
return timedeltas
24472421
else:
24482422
if seen.complex_:
24492423
if not seen.int_:
2450-
result = complexes
2424+
return complexes
24512425
elif seen.float_ or seen.nan_:
24522426
if not seen.int_:
2453-
result = floats
2427+
return floats
24542428
elif seen.int_:
24552429
if seen.uint_:
2456-
result = uints
2430+
return uints
24572431
else:
2458-
result = ints
2432+
return ints
24592433
elif seen.is_bool and not seen.nan_:
2460-
result = bools.view(np.bool_)
2461-
2462-
if result is uints or result is ints or result is floats or result is complexes:
2463-
# cast to the largest itemsize when all values are NumPy scalars
2464-
if itemsize_max > 0 and itemsize_max != result.dtype.itemsize:
2465-
result = result.astype(result.dtype.kind + str(itemsize_max))
2466-
return result
2467-
elif result is not None:
2468-
return result
2434+
return bools.view(np.bool_)
24692435

24702436
return objects
24712437

@@ -2488,7 +2454,7 @@ no_default = NoDefault.no_default # Sentinel indicating the default value.
24882454
@cython.wraparound(False)
24892455
def map_infer_mask(ndarray arr, object f, const uint8_t[:] mask, bint convert=True,
24902456
object na_value=no_default, cnp.dtype dtype=np.dtype(object)
2491-
) -> np.ndarray:
2457+
) -> "ArrayLike":
24922458
"""
24932459
Substitute for np.vectorize with pandas-friendly dtype inference.
24942460

@@ -2508,7 +2474,7 @@ def map_infer_mask(ndarray arr, object f, const uint8_t[:] mask, bint convert=Tr
25082474

25092475
Returns
25102476
-------
2511-
np.ndarray
2477+
np.ndarray or ExtensionArray
25122478
"""
25132479
cdef:
25142480
Py_ssize_t i, n
@@ -2545,7 +2511,7 @@ def map_infer_mask(ndarray arr, object f, const uint8_t[:] mask, bint convert=Tr
25452511
@cython.wraparound(False)
25462512
def map_infer(
25472513
ndarray arr, object f, bint convert=True, bint ignore_na=False
2548-
) -> np.ndarray:
2514+
) -> "ArrayLike":
25492515
"""
25502516
Substitute for np.vectorize with pandas-friendly dtype inference.
25512517

@@ -2559,7 +2525,7 @@ def map_infer(
25592525

25602526
Returns
25612527
-------
2562-
np.ndarray
2528+
np.ndarray or ExtensionArray
25632529
"""
25642530
cdef:
25652531
Py_ssize_t i, n
@@ -2697,7 +2663,7 @@ def to_object_array_tuples(rows: object) -> np.ndarray:
26972663

26982664
@cython.wraparound(False)
26992665
@cython.boundscheck(False)
2700-
def fast_multiget(dict mapping, ndarray keys, default=np.nan) -> np.ndarray:
2666+
def fast_multiget(dict mapping, ndarray keys, default=np.nan) -> "ArrayLike":
27012667
cdef:
27022668
Py_ssize_t i, n = len(keys)
27032669
object val

0 commit comments

Comments
 (0)