Skip to content

Commit ca1bfde

Browse files
authored
CLN: Assorted (#52248)
* CLN: Assorted * ignore pyright * suggested edits * revert keyword-only
1 parent 70e8474 commit ca1bfde

29 files changed

+138
-193
lines changed

pandas/_libs/algos.pyx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ from pandas._libs.khash cimport (
5151
kh_resize_int64,
5252
khiter_t,
5353
)
54+
from pandas._libs.missing cimport (
55+
checknull,
56+
isnaobj,
57+
)
5458
from pandas._libs.util cimport get_nat
5559

56-
import pandas._libs.missing as missing
57-
5860
cdef:
5961
float64_t FP_ERR = 1e-13
6062
float64_t NaN = <float64_t>np.NaN
@@ -95,10 +97,10 @@ class Infinity:
9597

9698
def __gt__(self, other):
9799
return (not isinstance(other, Infinity) and
98-
not missing.checknull(other))
100+
not checknull(other))
99101

100102
def __ge__(self, other):
101-
return not missing.checknull(other)
103+
return not checknull(other)
102104

103105

104106
class NegInfinity:
@@ -107,10 +109,10 @@ class NegInfinity:
107109
"""
108110
def __lt__(self, other):
109111
return (not isinstance(other, NegInfinity) and
110-
not missing.checknull(other))
112+
not checknull(other))
111113

112114
def __le__(self, other):
113-
return not missing.checknull(other)
115+
return not checknull(other)
114116

115117
def __eq__(self, other):
116118
return isinstance(other, NegInfinity)
@@ -988,7 +990,7 @@ def rank_1d(
988990
if mask is not None:
989991
pass
990992
elif numeric_object_t is object:
991-
mask = missing.isnaobj(masked_vals)
993+
mask = isnaobj(masked_vals)
992994
elif numeric_object_t is int64_t and is_datetimelike:
993995
mask = (masked_vals == NPY_NAT).astype(np.uint8)
994996
elif numeric_object_t is float64_t or numeric_object_t is float32_t:
@@ -1366,7 +1368,7 @@ def rank_2d(
13661368
nan_fill_val = get_rank_nan_fill_val(nans_rank_highest, <numeric_object_t>0)
13671369

13681370
if numeric_object_t is object:
1369-
mask = missing.isnaobj(values).view(np.uint8)
1371+
mask = isnaobj(values).view(np.uint8)
13701372
elif numeric_object_t is float64_t or numeric_object_t is float32_t:
13711373
mask = np.isnan(values).view(np.uint8)
13721374
else:

pandas/_libs/groupby.pyx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ from pandas._libs.missing cimport checknull
4949

5050

5151
cdef int64_t NPY_NAT = util.get_nat()
52-
_int64_max = np.iinfo(np.int64).max
5352

5453
cdef float64_t NaN = <float64_t>np.NaN
5554

@@ -256,9 +255,9 @@ def group_cumprod(
256255
Always false, `values` is never datetime-like.
257256
skipna : bool
258257
If true, ignore nans in `values`.
259-
mask: np.ndarray[uint8], optional
258+
mask : np.ndarray[uint8], optional
260259
Mask of values
261-
result_mask: np.ndarray[int8], optional
260+
result_mask : np.ndarray[int8], optional
262261
Mask of out array
263262

264263
Notes
@@ -345,9 +344,9 @@ def group_cumsum(
345344
True if `values` contains datetime-like entries.
346345
skipna : bool
347346
If true, ignore nans in `values`.
348-
mask: np.ndarray[uint8], optional
347+
mask : np.ndarray[uint8], optional
349348
Mask of values
350-
result_mask: np.ndarray[int8], optional
349+
result_mask : np.ndarray[int8], optional
351350
Mask of out array
352351

353352
Notes
@@ -615,7 +614,7 @@ def group_any_all(
615614
# value encountered is True
616615
flag_val = 1
617616
else:
618-
raise ValueError("'bool_func' must be either 'any' or 'all'!")
617+
raise ValueError("'val_test' must be either 'any' or 'all'!")
619618

620619
out[:] = 1 - flag_val
621620

@@ -1036,7 +1035,7 @@ def group_ohlc(
10361035
raise NotImplementedError("Argument 'values' must have only one dimension")
10371036

10381037
if int64float_t is float32_t or int64float_t is float64_t:
1039-
out[:] = np.nan
1038+
out[:] = NAN
10401039
else:
10411040
out[:] = 0
10421041

pandas/_libs/lib.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,7 @@ def maybe_convert_objects(ndarray[object] objects,
24392439
Seen seen = Seen()
24402440
object val
24412441
_TSObject tsobj
2442-
float64_t fnan = np.nan
2442+
float64_t fnan = NaN
24432443

24442444
if dtype_if_all_nat is not None:
24452445
# in practice we don't expect to ever pass dtype_if_all_nat

pandas/_libs/reshape.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ from numpy cimport (
99
import numpy as np
1010

1111
cimport numpy as cnp
12+
from numpy.math cimport NAN
1213

1314
cnp.import_array()
1415

@@ -129,7 +130,7 @@ def explode(ndarray[object] values):
129130
count += 1
130131
else:
131132
# empty list-like, use a nan marker
132-
result[count] = np.nan
133+
result[count] = NAN
133134
count += 1
134135
else:
135136
# replace with the existing scalar

pandas/_libs/sparse.pyx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
cimport cython
2+
23
import numpy as np
34

45
cimport numpy as cnp
@@ -10,16 +11,14 @@ from numpy cimport (
1011
ndarray,
1112
uint8_t,
1213
)
14+
from numpy.math cimport (
15+
INFINITY as INF,
16+
NAN as NaN,
17+
)
1318

1419
cnp.import_array()
1520

1621

17-
# -----------------------------------------------------------------------------
18-
# Preamble stuff
19-
20-
cdef float64_t NaN = <float64_t>np.NaN
21-
cdef float64_t INF = <float64_t>np.inf
22-
2322
# -----------------------------------------------------------------------------
2423

2524

pandas/_libs/tslibs/fields.pyx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,13 @@ def get_date_name_field(
150150
name based on requested field (e.g. day_name)
151151
"""
152152
cdef:
153-
Py_ssize_t i, count = dtindex.shape[0]
153+
Py_ssize_t i
154+
cnp.npy_intp count = dtindex.shape[0]
154155
ndarray[object] out, names
155156
npy_datetimestruct dts
156157
int dow
157158

158-
out = np.empty(count, dtype=object)
159+
out = cnp.PyArray_EMPTY(1, &count, cnp.NPY_OBJECT, 0)
159160

160161
if field == "day_name":
161162
if locale is None:

pandas/_libs/tslibs/timezones.pyx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,12 @@ cdef object _get_utc_trans_times_from_dateutil_tz(tzinfo tz):
270270

271271
cdef int64_t[::1] unbox_utcoffsets(object transinfo):
272272
cdef:
273-
Py_ssize_t i, sz
273+
Py_ssize_t i
274+
cnp.npy_intp sz
274275
int64_t[::1] arr
275276

276277
sz = len(transinfo)
277-
arr = np.empty(sz, dtype="i8")
278+
arr = cnp.PyArray_EMPTY(1, &sz, cnp.NPY_INT64, 0)
278279

279280
for i in range(sz):
280281
arr[i] = int(transinfo[i][0].total_seconds()) * 1_000_000_000

pandas/_libs/tslibs/vectorized.pyx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
cimport cython
2+
cimport numpy as cnp
23
from cpython.datetime cimport (
34
date,
45
datetime,
56
time,
67
tzinfo,
78
)
8-
9-
import numpy as np
10-
11-
cimport numpy as cnp
129
from numpy cimport (
1310
int64_t,
1411
ndarray,
@@ -101,7 +98,7 @@ def ints_to_pydatetime(
10198
tzinfo tz=None,
10299
str box="datetime",
103100
NPY_DATETIMEUNIT reso=NPY_FR_ns,
104-
) -> np.ndarray:
101+
) -> ndarray:
105102
# stamps is int64, arbitrary ndim
106103
"""
107104
Convert an i8 repr to an ndarray of datetimes, date, time or Timestamp.

pandas/core/arrays/datetimelike.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,9 @@ def _add_datetime_arraylike(self, other: DatetimeArray) -> DatetimeArray:
10771077
return other + self
10781078

10791079
@final
1080-
def _sub_datetimelike_scalar(self, other: datetime | np.datetime64):
1080+
def _sub_datetimelike_scalar(
1081+
self, other: datetime | np.datetime64
1082+
) -> TimedeltaArray:
10811083
if self.dtype.kind != "M":
10821084
raise TypeError(f"cannot subtract a datelike from a {type(self).__name__}")
10831085

@@ -1094,7 +1096,7 @@ def _sub_datetimelike_scalar(self, other: datetime | np.datetime64):
10941096
return self._sub_datetimelike(ts)
10951097

10961098
@final
1097-
def _sub_datetime_arraylike(self, other: DatetimeArray):
1099+
def _sub_datetime_arraylike(self, other: DatetimeArray) -> TimedeltaArray:
10981100
if self.dtype.kind != "M":
10991101
raise TypeError(f"cannot subtract a datelike from a {type(self).__name__}")
11001102

@@ -1295,7 +1297,7 @@ def _addsub_object_array(self, other: npt.NDArray[np.object_], op):
12951297
res_values = op(self.astype("O"), np.asarray(other))
12961298
return res_values
12971299

1298-
def _accumulate(self, name: str, *, skipna: bool = True, **kwargs):
1300+
def _accumulate(self, name: str, *, skipna: bool = True, **kwargs) -> Self:
12991301
if name not in {"cummin", "cummax"}:
13001302
raise TypeError(f"Accumulation {name} not supported for {type(self)}")
13011303

@@ -2014,7 +2016,7 @@ def round(
20142016
freq,
20152017
ambiguous: TimeAmbiguous = "raise",
20162018
nonexistent: TimeNonexistent = "raise",
2017-
):
2019+
) -> Self:
20182020
return self._round(freq, RoundTo.NEAREST_HALF_EVEN, ambiguous, nonexistent)
20192021

20202022
@Appender((_round_doc + _floor_example).format(op="floor"))
@@ -2023,7 +2025,7 @@ def floor(
20232025
freq,
20242026
ambiguous: TimeAmbiguous = "raise",
20252027
nonexistent: TimeNonexistent = "raise",
2026-
):
2028+
) -> Self:
20272029
return self._round(freq, RoundTo.MINUS_INFTY, ambiguous, nonexistent)
20282030

20292031
@Appender((_round_doc + _ceil_example).format(op="ceil"))
@@ -2032,7 +2034,7 @@ def ceil(
20322034
freq,
20332035
ambiguous: TimeAmbiguous = "raise",
20342036
nonexistent: TimeNonexistent = "raise",
2035-
):
2037+
) -> Self:
20362038
return self._round(freq, RoundTo.PLUS_INFTY, ambiguous, nonexistent)
20372039

20382040
# --------------------------------------------------------------
@@ -2053,7 +2055,7 @@ def all(self, *, axis: AxisInt | None = None, skipna: bool = True) -> bool:
20532055
def _maybe_clear_freq(self) -> None:
20542056
self._freq = None
20552057

2056-
def _with_freq(self, freq):
2058+
def _with_freq(self, freq) -> Self:
20572059
"""
20582060
Helper to get a view on the same data, with a new freq.
20592061

pandas/core/arrays/string_.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,9 @@ def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy: bool = Fal
353353

354354
else:
355355
if hasattr(scalars, "type"):
356-
# pyarrow array
356+
# pyarrow array; we cannot rely on the "to_numpy" check in
357+
# ensure_string_array because calling scalars.to_numpy would set
358+
# zero_copy_only to True which caused problems see GH#52076
357359
scalars = np.array(scalars)
358360
# convert non-na-likes to str, and nan-likes to StringDtype().na_value
359361
result = lib.ensure_string_array(scalars, na_value=libmissing.NA, copy=copy)

pandas/core/dtypes/cast.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@
112112
_int8_max = np.iinfo(np.int8).max
113113
_int16_max = np.iinfo(np.int16).max
114114
_int32_max = np.iinfo(np.int32).max
115-
_int64_max = np.iinfo(np.int64).max
116115

117116
_dtype_obj = np.dtype(object)
118117

0 commit comments

Comments
 (0)