Skip to content

Commit a0cd35e

Browse files
jbrockmendelproost
authored andcommitted
CLN: assorted cleanups (pandas-dev#29232)
1 parent 580a321 commit a0cd35e

File tree

14 files changed

+66
-45
lines changed

14 files changed

+66
-45
lines changed

pandas/_libs/groupby.pyx

+6-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ def group_cumprod_float64(float64_t[:, :] out,
146146
int ngroups,
147147
bint is_datetimelike,
148148
bint skipna=True):
149-
"""Cumulative product of columns of `values`, in row groups `labels`.
149+
"""
150+
Cumulative product of columns of `values`, in row groups `labels`.
150151
151152
Parameters
152153
----------
@@ -203,7 +204,8 @@ def group_cumsum(numeric[:, :] out,
203204
int ngroups,
204205
is_datetimelike,
205206
bint skipna=True):
206-
"""Cumulative sum of columns of `values`, in row groups `labels`.
207+
"""
208+
Cumulative sum of columns of `values`, in row groups `labels`.
207209
208210
Parameters
209211
----------
@@ -314,7 +316,8 @@ def group_shift_indexer(int64_t[:] out, const int64_t[:] labels,
314316
def group_fillna_indexer(ndarray[int64_t] out, ndarray[int64_t] labels,
315317
ndarray[uint8_t] mask, object direction,
316318
int64_t limit):
317-
"""Indexes how to fill values forwards or backwards within a group
319+
"""
320+
Indexes how to fill values forwards or backwards within a group.
318321
319322
Parameters
320323
----------

pandas/_libs/index.pyx

+11-9
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ cdef class IndexEngine:
227227
cdef _get_index_values(self):
228228
return self.vgetter()
229229

230-
def _call_monotonic(self, values):
230+
cdef _call_monotonic(self, values):
231231
return algos.is_monotonic(values, timelike=False)
232232

233233
def get_backfill_indexer(self, other, limit=None):
@@ -236,7 +236,7 @@ cdef class IndexEngine:
236236
def get_pad_indexer(self, other, limit=None):
237237
return algos.pad(self._get_index_values(), other, limit=limit)
238238

239-
cdef _make_hash_table(self, n):
239+
cdef _make_hash_table(self, Py_ssize_t n):
240240
raise NotImplementedError
241241

242242
cdef _check_type(self, object val):
@@ -262,7 +262,7 @@ cdef class IndexEngine:
262262

263263
self.need_unique_check = 0
264264

265-
cpdef _call_map_locations(self, values):
265+
cdef void _call_map_locations(self, values):
266266
self.mapping.map_locations(values)
267267

268268
def clear_mapping(self):
@@ -391,7 +391,7 @@ cdef class ObjectEngine(IndexEngine):
391391
"""
392392
Index Engine for use with object-dtype Index, namely the base class Index
393393
"""
394-
cdef _make_hash_table(self, n):
394+
cdef _make_hash_table(self, Py_ssize_t n):
395395
return _hash.PyObjectHashTable(n)
396396

397397

@@ -418,7 +418,7 @@ cdef class DatetimeEngine(Int64Engine):
418418
cdef _get_index_values(self):
419419
return self.vgetter().view('i8')
420420

421-
def _call_monotonic(self, values):
421+
cdef _call_monotonic(self, values):
422422
return algos.is_monotonic(values, timelike=True)
423423

424424
cpdef get_loc(self, object val):
@@ -500,11 +500,13 @@ cdef class PeriodEngine(Int64Engine):
500500
cdef _get_index_values(self):
501501
return super(PeriodEngine, self).vgetter()
502502

503-
cpdef _call_map_locations(self, values):
504-
super(PeriodEngine, self)._call_map_locations(values.view('i8'))
503+
cdef void _call_map_locations(self, values):
504+
# super(...) pattern doesn't seem to work with `cdef`
505+
Int64Engine._call_map_locations(self, values.view('i8'))
505506

506-
def _call_monotonic(self, values):
507-
return super(PeriodEngine, self)._call_monotonic(values.view('i8'))
507+
cdef _call_monotonic(self, values):
508+
# super(...) pattern doesn't seem to work with `cdef`
509+
return Int64Engine._call_monotonic(self, values.view('i8'))
508510

509511
def get_indexer(self, values):
510512
cdef ndarray[int64_t, ndim=1] ordinals

pandas/_libs/index_class_helper.pxi.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ dtypes = [('Float64', 'float64', 'float64_t', 'Float64', 'float64'),
2929

3030
cdef class {{name}}Engine(IndexEngine):
3131

32-
cdef _make_hash_table(self, n):
32+
cdef _make_hash_table(self, Py_ssize_t n):
3333
return _hash.{{hashtable_name}}HashTable(n)
3434

3535
{{if name not in {'Float64', 'Float32'} }}
@@ -38,7 +38,7 @@ cdef class {{name}}Engine(IndexEngine):
3838
raise KeyError(val)
3939
{{endif}}
4040

41-
cpdef _call_map_locations(self, values):
41+
cdef void _call_map_locations(self, values):
4242
# self.mapping is of type {{hashtable_name}}HashTable,
4343
# so convert dtype of values
4444
self.mapping.map_locations(algos.ensure_{{hashtable_dtype}}(values))

pandas/_libs/internals.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ cdef slice_getitem(slice slc, ind):
319319

320320
@cython.boundscheck(False)
321321
@cython.wraparound(False)
322-
cpdef slice indexer_as_slice(int64_t[:] vals):
322+
cdef slice indexer_as_slice(int64_t[:] vals):
323323
cdef:
324324
Py_ssize_t i, n, start, stop
325325
int64_t d

pandas/_libs/lib.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -2168,8 +2168,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
21682168

21692169
@cython.boundscheck(False)
21702170
@cython.wraparound(False)
2171-
def map_infer_mask(ndarray arr, object f, const uint8_t[:] mask,
2172-
bint convert=1):
2171+
def map_infer_mask(ndarray arr, object f, const uint8_t[:] mask, bint convert=1):
21732172
"""
21742173
Substitute for np.vectorize with pandas-friendly dtype inference
21752174

pandas/_libs/reduction.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ cdef class Reducer:
105105
flatiter it
106106
bint has_labels, has_ndarray_labels
107107
object res, name, labels, index
108-
object cached_typ=None
108+
object cached_typ = None
109109

110110
arr = self.arr
111111
chunk = self.dummy
@@ -248,7 +248,7 @@ cdef class SeriesBinGrouper:
248248
object res
249249
bint initialized = 0
250250
Slider vslider, islider
251-
object name, cached_typ=None, cached_ityp=None
251+
object name, cached_typ = None, cached_ityp = None
252252

253253
counts = np.zeros(self.ngroups, dtype=np.int64)
254254

pandas/_libs/tslibs/parsing.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ def try_parse_dates(object[:] values, parser=None,
581581
else:
582582
result[i] = parse_date(values[i])
583583
except Exception:
584-
# Since parser is user-defined, we can't guess what it migh raise
584+
# Since parser is user-defined, we can't guess what it might raise
585585
return values
586586
else:
587587
parse_date = parser

pandas/core/algorithms.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ def _get_data_algo(values):
260260
if lib.infer_dtype(values, skipna=False) in ["string"]:
261261
ndtype = "string"
262262

263-
f = _hashtables.get(ndtype, _hashtables["object"])
263+
htable = _hashtables.get(ndtype, _hashtables["object"])
264264

265-
return f, values
265+
return htable, values
266266

267267

268268
# --------------- #

pandas/core/computation/engines.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _check_ne_builtin_clash(expr):
2929
overlap = names & _ne_builtins
3030

3131
if overlap:
32-
s = ", ".join(map(repr, overlap))
32+
s = ", ".join(repr(x) for x in overlap)
3333
raise NumExprClobberingError(
3434
'Variables in expression "{expr}" '
3535
"overlap with builtins: ({s})".format(expr=expr, s=s)

pandas/core/computation/ops.py

+30-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from datetime import datetime
55
from distutils.version import LooseVersion
66
from functools import partial
7-
import operator as op
7+
import operator
88

99
import numpy as np
1010

@@ -18,7 +18,7 @@
1818

1919
from pandas.io.formats.printing import pprint_thing, pprint_thing_encoded
2020

21-
_reductions = "sum", "prod"
21+
_reductions = ("sum", "prod")
2222

2323
_unary_math_ops = (
2424
"sin",
@@ -273,20 +273,37 @@ def _not_in(x, y):
273273
return x not in y
274274

275275

276-
_cmp_ops_syms = ">", "<", ">=", "<=", "==", "!=", "in", "not in"
277-
_cmp_ops_funcs = op.gt, op.lt, op.ge, op.le, op.eq, op.ne, _in, _not_in
276+
_cmp_ops_syms = (">", "<", ">=", "<=", "==", "!=", "in", "not in")
277+
_cmp_ops_funcs = (
278+
operator.gt,
279+
operator.lt,
280+
operator.ge,
281+
operator.le,
282+
operator.eq,
283+
operator.ne,
284+
_in,
285+
_not_in,
286+
)
278287
_cmp_ops_dict = dict(zip(_cmp_ops_syms, _cmp_ops_funcs))
279288

280-
_bool_ops_syms = "&", "|", "and", "or"
281-
_bool_ops_funcs = op.and_, op.or_, op.and_, op.or_
289+
_bool_ops_syms = ("&", "|", "and", "or")
290+
_bool_ops_funcs = (operator.and_, operator.or_, operator.and_, operator.or_)
282291
_bool_ops_dict = dict(zip(_bool_ops_syms, _bool_ops_funcs))
283292

284-
_arith_ops_syms = "+", "-", "*", "/", "**", "//", "%"
285-
_arith_ops_funcs = (op.add, op.sub, op.mul, op.truediv, op.pow, op.floordiv, op.mod)
293+
_arith_ops_syms = ("+", "-", "*", "/", "**", "//", "%")
294+
_arith_ops_funcs = (
295+
operator.add,
296+
operator.sub,
297+
operator.mul,
298+
operator.truediv,
299+
operator.pow,
300+
operator.floordiv,
301+
operator.mod,
302+
)
286303
_arith_ops_dict = dict(zip(_arith_ops_syms, _arith_ops_funcs))
287304

288-
_special_case_arith_ops_syms = "**", "//", "%"
289-
_special_case_arith_ops_funcs = op.pow, op.floordiv, op.mod
305+
_special_case_arith_ops_syms = ("**", "//", "%")
306+
_special_case_arith_ops_funcs = (operator.pow, operator.floordiv, operator.mod)
290307
_special_case_arith_ops_dict = dict(
291308
zip(_special_case_arith_ops_syms, _special_case_arith_ops_funcs)
292309
)
@@ -371,7 +388,7 @@ def __call__(self, env):
371388
"""
372389
# handle truediv
373390
if self.op == "/" and env.scope["truediv"]:
374-
self.func = op.truediv
391+
self.func = operator.truediv
375392

376393
# recurse over the left/right nodes
377394
left = self.lhs(env)
@@ -502,8 +519,8 @@ def __init__(self, lhs, rhs, truediv, *args, **kwargs):
502519
_cast_inplace(com.flatten(self), acceptable_dtypes, np.float_)
503520

504521

505-
_unary_ops_syms = "+", "-", "~", "not"
506-
_unary_ops_funcs = op.pos, op.neg, op.invert, op.invert
522+
_unary_ops_syms = ("+", "-", "~", "not")
523+
_unary_ops_funcs = (operator.pos, operator.neg, operator.invert, operator.invert)
507524
_unary_ops_dict = dict(zip(_unary_ops_syms, _unary_ops_funcs))
508525

509526

pandas/core/internals/managers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ def apply(
432432
b_items = self.items[b.mgr_locs.indexer]
433433

434434
for k, obj in aligned_args.items():
435-
axis = getattr(obj, "_info_axis_number", 0)
435+
axis = obj._info_axis_number
436436
kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437437

438438
applied = getattr(b, f)(**kwargs)

pandas/core/nanops.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ def _get_values(
237237
fill_value_typ: Optional[str] = None,
238238
mask: Optional[np.ndarray] = None,
239239
) -> Tuple[np.ndarray, Optional[np.ndarray], np.dtype, np.dtype, Any]:
240-
""" Utility to get the values view, mask, dtype, dtype_max, and fill_value.
240+
"""
241+
Utility to get the values view, mask, dtype, dtype_max, and fill_value.
241242
242243
If both mask and fill_value/fill_value_typ are not None and skipna is True,
243244
the values array will be copied.

pandas/tests/frame/test_query_eval.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -989,13 +989,12 @@ def test_query_with_nested_special_character(self, parser, engine):
989989
assert_frame_equal(res, expec)
990990

991991
def test_query_lex_compare_strings(self, parser, engine):
992-
import operator as opr
993992

994993
a = Series(np.random.choice(list("abcde"), 20))
995994
b = Series(np.arange(a.size))
996995
df = DataFrame({"X": a, "Y": b})
997996

998-
ops = {"<": opr.lt, ">": opr.gt, "<=": opr.le, ">=": opr.ge}
997+
ops = {"<": operator.lt, ">": operator.gt, "<=": operator.le, ">=": operator.ge}
999998

1000999
for op, func in ops.items():
10011000
res = df.query('X %s "d"' % op, engine=engine, parser=parser)

pandas/tests/indexes/datetimes/test_partial_slicing.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
""" test partial slicing on Series/Frame """
22

33
from datetime import datetime
4-
import operator as op
4+
import operator
55

66
import numpy as np
77
import pytest
@@ -408,10 +408,10 @@ def test_loc_datetime_length_one(self):
408408
@pytest.mark.parametrize(
409409
"op,expected",
410410
[
411-
(op.lt, [True, False, False, False]),
412-
(op.le, [True, True, False, False]),
413-
(op.eq, [False, True, False, False]),
414-
(op.gt, [False, False, False, True]),
411+
(operator.lt, [True, False, False, False]),
412+
(operator.le, [True, True, False, False]),
413+
(operator.eq, [False, True, False, False]),
414+
(operator.gt, [False, False, False, True]),
415415
],
416416
)
417417
def test_selection_by_datetimelike(self, datetimelike, op, expected):

0 commit comments

Comments
 (0)