Skip to content

Commit a33d05b

Browse files
jbrockmendelJulianWgs
authored andcommitted
CLN: sqlalchemy deprecation, annotations, share shape properties (pandas-dev#40491)
1 parent d3f9207 commit a33d05b

File tree

14 files changed

+47
-40
lines changed

14 files changed

+47
-40
lines changed

pandas/_libs/intervaltree.pxi.in

+2
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ NODE_CLASSES = {}
238238
{{for dtype, dtype_title, closed, closed_title, cmp_left, cmp_right,
239239
cmp_left_converse, cmp_right_converse, fused_prefix in nodes}}
240240

241+
242+
@cython.internal
241243
cdef class {{dtype_title}}Closed{{closed_title}}IntervalNode:
242244
"""Non-terminal node for an IntervalTree
243245

pandas/_libs/lib.pyx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,7 @@ except AttributeError:
11401140
pass
11411141

11421142

1143+
@cython.internal
11431144
cdef class Seen:
11441145
"""
11451146
Class for keeping track of the types of elements
@@ -2600,7 +2601,7 @@ def tuples_to_object_array(ndarray[object] tuples):
26002601
return result
26012602

26022603

2603-
def to_object_array_tuples(rows: object):
2604+
def to_object_array_tuples(rows: object) -> np.ndarray:
26042605
"""
26052606
Convert a list of tuples into an object array. Any subclass of
26062607
tuple in `rows` will be casted to tuple.
@@ -2612,7 +2613,7 @@ def to_object_array_tuples(rows: object):
26122613

26132614
Returns
26142615
-------
2615-
numpy array of the object dtype.
2616+
np.ndarray[object, ndim=2]
26162617
"""
26172618
cdef:
26182619
Py_ssize_t i, j, n, k, tmp

pandas/_libs/ops.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ from pandas._libs.util cimport (
3232

3333
@cython.wraparound(False)
3434
@cython.boundscheck(False)
35-
def scalar_compare(object[:] values, object val, object op):
35+
def scalar_compare(object[:] values, object val, object op) -> ndarray:
3636
"""
3737
Compare each element of `values` array with the scalar `val`, with
3838
the comparison operation described by `op`.
@@ -114,7 +114,7 @@ def scalar_compare(object[:] values, object val, object op):
114114

115115
@cython.wraparound(False)
116116
@cython.boundscheck(False)
117-
def vec_compare(ndarray[object] left, ndarray[object] right, object op):
117+
def vec_compare(ndarray[object] left, ndarray[object] right, object op) -> ndarray:
118118
"""
119119
Compare the elements of `left` with the elements of `right` pointwise,
120120
with the comparison operation described by `op`.
@@ -180,7 +180,7 @@ def vec_compare(ndarray[object] left, ndarray[object] right, object op):
180180

181181
@cython.wraparound(False)
182182
@cython.boundscheck(False)
183-
def scalar_binop(object[:] values, object val, object op):
183+
def scalar_binop(object[:] values, object val, object op) -> ndarray:
184184
"""
185185
Apply the given binary operator `op` between each element of the array
186186
`values` and the scalar `val`.
@@ -217,7 +217,7 @@ def scalar_binop(object[:] values, object val, object op):
217217

218218
@cython.wraparound(False)
219219
@cython.boundscheck(False)
220-
def vec_binop(object[:] left, object[:] right, object op):
220+
def vec_binop(object[:] left, object[:] right, object op) -> ndarray:
221221
"""
222222
Apply the given binary operator `op` pointwise to the elements of
223223
arrays `left` and `right`.

pandas/_libs/reshape.pyx

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ ctypedef fused reshape_t:
4141
@cython.boundscheck(False)
4242
def unstack(reshape_t[:, :] values, const uint8_t[:] mask,
4343
Py_ssize_t stride, Py_ssize_t length, Py_ssize_t width,
44-
reshape_t[:, :] new_values, uint8_t[:, :] new_mask):
44+
reshape_t[:, :] new_values, uint8_t[:, :] new_mask) -> None:
4545
"""
4646
Transform long values to wide new_values.
4747

@@ -111,7 +111,10 @@ def explode(ndarray[object] values):
111111
112112
Returns
113113
-------
114-
tuple(values, counts)
114+
ndarray[object]
115+
result
116+
ndarray[int64_t]
117+
counts
115118
"""
116119
cdef:
117120
Py_ssize_t i, j, count, n

pandas/_libs/tslibs/nattype.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ cdef class _NaT(datetime):
286286
# This allows Timestamp(ts.isoformat()) to always correctly roundtrip.
287287
return "NaT"
288288

289-
def __hash__(self):
289+
def __hash__(self) -> int:
290290
return NPY_NAT
291291

292292
@property

pandas/_libs/tslibs/vectorized.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def ints_to_pydatetime(
9090
object freq=None,
9191
bint fold=False,
9292
str box="datetime"
93-
):
93+
) -> np.ndarray:
9494
"""
9595
Convert an i8 repr to an ndarray of datetimes, date, time or Timestamp.
9696

@@ -116,7 +116,7 @@ def ints_to_pydatetime(
116116

117117
Returns
118118
-------
119-
ndarray of dtype specified by box
119+
ndarray[object] of type specified by box
120120
"""
121121
cdef:
122122
Py_ssize_t i, n = len(arr)
@@ -223,7 +223,7 @@ cdef inline int _reso_stamp(npy_datetimestruct *dts):
223223
return RESO_DAY
224224

225225

226-
def get_resolution(const int64_t[:] stamps, tzinfo tz=None):
226+
def get_resolution(const int64_t[:] stamps, tzinfo tz=None) -> Resolution:
227227
cdef:
228228
Py_ssize_t i, n = len(stamps)
229229
npy_datetimestruct dts
@@ -332,7 +332,7 @@ cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo t
332332

333333
@cython.wraparound(False)
334334
@cython.boundscheck(False)
335-
def is_date_array_normalized(const int64_t[:] stamps, tzinfo tz=None):
335+
def is_date_array_normalized(const int64_t[:] stamps, tzinfo tz=None) -> bool:
336336
"""
337337
Check if all of the given (nanosecond) timestamps are normalized to
338338
midnight, i.e. hour == minute == second == 0. If the optional timezone

pandas/_libs/writers.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def write_csv_rows(
7777

7878
@cython.boundscheck(False)
7979
@cython.wraparound(False)
80-
def convert_json_to_lines(arr: object) -> str:
80+
def convert_json_to_lines(arr: str) -> str:
8181
"""
8282
replace comma separated json with line feeds, paying special attention
8383
to quotes & brackets

pandas/core/arrays/_mixins.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
Type,
99
TypeVar,
1010
Union,
11+
cast,
1112
)
1213

1314
import numpy as np
1415

1516
from pandas._libs import lib
16-
from pandas._typing import Shape
17+
from pandas._typing import (
18+
F,
19+
Shape,
20+
)
1721
from pandas.compat.numpy import function as nv
1822
from pandas.errors import AbstractMethodError
1923
from pandas.util._decorators import (
@@ -41,7 +45,7 @@
4145
)
4246

4347

44-
def ravel_compat(meth):
48+
def ravel_compat(meth: F) -> F:
4549
"""
4650
Decorator to ravel a 2D array before passing it to a cython operation,
4751
then reshape the result to our own shape.
@@ -58,7 +62,7 @@ def method(self, *args, **kwargs):
5862
order = "F" if flags.f_contiguous else "C"
5963
return result.reshape(self.shape, order=order)
6064

61-
return method
65+
return cast(F, method)
6266

6367

6468
class NDArrayBackedExtensionArray(ExtensionArray):

pandas/core/indexes/base.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -6141,15 +6141,14 @@ def _maybe_disable_logical_methods(self, opname: str_t):
61416141
# This call will raise
61426142
make_invalid_op(opname)(self)
61436143

6144+
@final
61446145
@property
61456146
def shape(self) -> Shape:
61466147
"""
61476148
Return a tuple of the shape of the underlying data.
61486149
"""
6149-
# not using "(len(self), )" to return "correct" shape if the values
6150-
# consists of a >1 D array (see GH-27775)
6151-
# overridden in MultiIndex.shape to avoid materializing the values
6152-
return self._values.shape
6150+
# See GH#27775, GH#27384 for history/reasoning in how this is defined.
6151+
return (len(self),)
61536152

61546153

61556154
def ensure_index_from_sequences(sequences, names=None):

pandas/core/indexes/multi.py

-9
Original file line numberDiff line numberDiff line change
@@ -752,15 +752,6 @@ def dtypes(self) -> Series:
752752
}
753753
)
754754

755-
@property
756-
def shape(self) -> Shape:
757-
"""
758-
Return a tuple of the shape of the underlying data.
759-
"""
760-
# overriding the base Index.shape definition to avoid materializing
761-
# the values (GH-27384, GH-27775)
762-
return (len(self),)
763-
764755
def __len__(self) -> int:
765756
return len(self.codes[0])
766757

pandas/core/indexes/range.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ def take(
450450
**kwargs,
451451
)
452452

453-
def tolist(self):
453+
def tolist(self) -> list[int]:
454454
return list(self._range)
455455

456456
@doc(Int64Index.__iter__)
@@ -494,13 +494,13 @@ def _minmax(self, meth: str):
494494

495495
return self.start + self.step * no_steps
496496

497-
def min(self, axis=None, skipna=True, *args, **kwargs) -> int:
497+
def min(self, axis=None, skipna: bool = True, *args, **kwargs) -> int:
498498
"""The minimum value of the RangeIndex"""
499499
nv.validate_minmax_axis(axis)
500500
nv.validate_min(args, kwargs)
501501
return self._minmax("min")
502502

503-
def max(self, axis=None, skipna=True, *args, **kwargs) -> int:
503+
def max(self, axis=None, skipna: bool = True, *args, **kwargs) -> int:
504504
"""The maximum value of the RangeIndex"""
505505
nv.validate_minmax_axis(axis)
506506
nv.validate_max(args, kwargs)

pandas/core/util/hashing.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def combine_hash_arrays(arrays: Iterator[np.ndarray], num_items: int) -> np.ndar
5656
5757
Returns
5858
-------
59-
np.ndarray[int64]
59+
np.ndarray[uint64]
6060
6161
Should be the same as CPython's tupleobject.c
6262
"""
@@ -184,7 +184,7 @@ def hash_tuples(
184184
185185
Returns
186186
-------
187-
ndarray of hashed values array
187+
ndarray[np.uint64] of hashed values
188188
"""
189189
if not is_list_like(vals):
190190
raise TypeError("must be convertible to a list-of-tuples")
@@ -227,7 +227,7 @@ def _hash_categorical(cat: Categorical, encoding: str, hash_key: str) -> np.ndar
227227
228228
Returns
229229
-------
230-
ndarray of hashed values array, same size as len(c)
230+
ndarray[np.uint64] of hashed values, same size as len(c)
231231
"""
232232
# Convert ExtensionArrays to ndarrays
233233
values = np.asarray(cat.categories._values)
@@ -274,7 +274,8 @@ def hash_array(
274274
275275
Returns
276276
-------
277-
1d uint64 numpy array of hash values, same length as the vals
277+
ndarray[np.uint64, ndim=1]
278+
Hashed values, same length as the vals.
278279
"""
279280
if not hasattr(vals, "dtype"):
280281
raise TypeError("must pass a ndarray-like")

pandas/io/json/_normalize.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from pandas import DataFrame
2828

2929

30-
def convert_to_line_delimits(s):
30+
def convert_to_line_delimits(s: str) -> str:
3131
"""
3232
Helper function that converts JSON lists to line delimited JSON.
3333
"""

pandas/tests/io/test_sql.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1991,8 +1991,14 @@ def bar(connection, data):
19911991
def main(connectable):
19921992
with connectable.connect() as conn:
19931993
with conn.begin():
1994-
foo_data = conn.run_callable(foo)
1995-
conn.run_callable(bar, foo_data)
1994+
if _gt14():
1995+
# https://github.com/sqlalchemy/sqlalchemy/commit/
1996+
# 00b5c10846e800304caa86549ab9da373b42fa5d#r48323973
1997+
foo_data = foo(conn)
1998+
bar(conn, foo_data)
1999+
else:
2000+
foo_data = conn.run_callable(foo)
2001+
conn.run_callable(bar, foo_data)
19962002

19972003
DataFrame({"test_foo_data": [0, 1, 2]}).to_sql("test_foo_data", self.conn)
19982004
main(self.conn)

0 commit comments

Comments
 (0)