Skip to content

Commit 469b4b7

Browse files
ShaharNavehjreback
authored andcommitted
CLN/STY: various code cleanups (#31162)
1 parent 29edd11 commit 469b4b7

File tree

6 files changed

+39
-69
lines changed

6 files changed

+39
-69
lines changed

pandas/core/computation/align.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
"""Core eval alignment algorithms
1+
"""
2+
Core eval alignment algorithms.
23
"""
34

45
from functools import partial, wraps

pandas/core/computation/eval.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python3
2-
32
"""
43
Top level ``eval`` module.
54
"""
@@ -26,30 +25,29 @@ def _check_engine(engine: Optional[str]) -> str:
2625
Parameters
2726
----------
2827
engine : str
28+
String to validate.
2929
3030
Raises
3131
------
3232
KeyError
33-
* If an invalid engine is passed
33+
* If an invalid engine is passed.
3434
ImportError
35-
* If numexpr was requested but doesn't exist
35+
* If numexpr was requested but doesn't exist.
3636
3737
Returns
3838
-------
39-
string engine
39+
str
40+
Engine name.
4041
"""
4142
from pandas.core.computation.check import _NUMEXPR_INSTALLED
4243

4344
if engine is None:
44-
if _NUMEXPR_INSTALLED:
45-
engine = "numexpr"
46-
else:
47-
engine = "python"
45+
engine = "numexpr" if _NUMEXPR_INSTALLED else "python"
4846

4947
if engine not in _engines:
50-
valid = list(_engines.keys())
48+
valid_engines = list(_engines.keys())
5149
raise KeyError(
52-
f"Invalid engine {repr(engine)} passed, valid engines are {valid}"
50+
f"Invalid engine '{engine}' passed, valid engines are {valid_engines}"
5351
)
5452

5553
# TODO: validate this in a more general way (thinking of future engines
@@ -58,10 +56,8 @@ def _check_engine(engine: Optional[str]) -> str:
5856
if engine == "numexpr":
5957
if not _NUMEXPR_INSTALLED:
6058
raise ImportError(
61-
"'numexpr' is not installed or an "
62-
"unsupported version. Cannot use "
63-
"engine='numexpr' for query/eval "
64-
"if 'numexpr' is not installed"
59+
"'numexpr' is not installed or an unsupported version. Cannot use "
60+
"engine='numexpr' for query/eval if 'numexpr' is not installed"
6561
)
6662

6763
return engine
@@ -80,11 +76,9 @@ def _check_parser(parser: str):
8076
KeyError
8177
* If an invalid parser is passed
8278
"""
83-
8479
if parser not in _parsers:
8580
raise KeyError(
86-
f"Invalid parser {repr(parser)} passed, "
87-
f"valid parsers are {_parsers.keys()}"
81+
f"Invalid parser '{parser}' passed, valid parsers are {_parsers.keys()}"
8882
)
8983

9084

@@ -94,8 +88,8 @@ def _check_resolvers(resolvers):
9488
if not hasattr(resolver, "__getitem__"):
9589
name = type(resolver).__name__
9690
raise TypeError(
97-
f"Resolver of type {repr(name)} does not "
98-
f"implement the __getitem__ method"
91+
f"Resolver of type '{name}' does not "
92+
"implement the __getitem__ method"
9993
)
10094

10195

@@ -155,10 +149,8 @@ def _check_for_locals(expr: str, stack_level: int, parser: str):
155149
msg = "The '@' prefix is only supported by the pandas parser"
156150
elif at_top_of_stack:
157151
msg = (
158-
"The '@' prefix is not allowed in "
159-
"top-level eval calls, \nplease refer to "
160-
"your variables by name without the '@' "
161-
"prefix"
152+
"The '@' prefix is not allowed in top-level eval calls.\n"
153+
"please refer to your variables by name without the '@' prefix."
162154
)
163155

164156
if at_top_of_stack or not_pandas_parser:
@@ -285,13 +277,14 @@ def eval(
285277
See the :ref:`enhancing performance <enhancingperf.eval>` documentation for
286278
more details.
287279
"""
288-
289280
inplace = validate_bool_kwarg(inplace, "inplace")
290281

291282
if truediv is not no_default:
292283
warnings.warn(
293-
"The `truediv` parameter in pd.eval is deprecated and will be "
294-
"removed in a future version.",
284+
(
285+
"The `truediv` parameter in pd.eval is deprecated and "
286+
"will be removed in a future version."
287+
),
295288
FutureWarning,
296289
stacklevel=2,
297290
)

pandas/core/computation/expressions.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,9 @@ def set_use_numexpr(v=True):
4545

4646
# choose what we are going to do
4747
global _evaluate, _where
48-
if not _USE_NUMEXPR:
49-
_evaluate = _evaluate_standard
50-
_where = _where_standard
51-
else:
52-
_evaluate = _evaluate_numexpr
53-
_where = _where_numexpr
48+
49+
_evaluate = _evaluate_numexpr if _USE_NUMEXPR else _evaluate_standard
50+
_where = _where_numexpr if _USE_NUMEXPR else _where_standard
5451

5552

5653
def set_numexpr_threads(n=None):
@@ -63,7 +60,9 @@ def set_numexpr_threads(n=None):
6360

6461

6562
def _evaluate_standard(op, op_str, a, b):
66-
""" standard evaluation """
63+
"""
64+
Standard evaluation.
65+
"""
6766
if _TEST_MODE:
6867
_store_test_result(False)
6968
with np.errstate(all="ignore"):
@@ -176,7 +175,7 @@ def _bool_arith_check(
176175
if op_str in unsupported:
177176
warnings.warn(
178177
f"evaluating in Python space because the {repr(op_str)} "
179-
f"operator is not supported by numexpr for "
178+
"operator is not supported by numexpr for "
180179
f"the bool dtype, use {repr(unsupported[op_str])} instead"
181180
)
182181
return False
@@ -202,7 +201,6 @@ def evaluate(op, op_str, a, b, use_numexpr=True):
202201
use_numexpr : bool, default True
203202
Whether to try to use numexpr.
204203
"""
205-
206204
use_numexpr = use_numexpr and _bool_arith_check(op_str, a, b)
207205
if use_numexpr:
208206
return _evaluate(op, op_str, a, b)
@@ -221,10 +219,7 @@ def where(cond, a, b, use_numexpr=True):
221219
use_numexpr : bool, default True
222220
Whether to try to use numexpr.
223221
"""
224-
225-
if use_numexpr:
226-
return _where(cond, a, b)
227-
return _where_standard(cond, a, b)
222+
return _where(cond, a, b) if use_numexpr else _where_standard(cond, a, b)
228223

229224

230225
def set_test_mode(v=True):

pandas/core/frame.py

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
alignment and a host of useful data manipulation methods having to do with the
99
labeling information
1010
"""
11+
1112
import collections
1213
from collections import abc
1314
from io import StringIO
@@ -258,7 +259,6 @@
258259
259260
Examples
260261
--------
261-
262262
>>> df1 = pd.DataFrame({'lkey': ['foo', 'bar', 'baz', 'foo'],
263263
... 'value': [1, 2, 3, 5]})
264264
>>> df2 = pd.DataFrame({'rkey': ['foo', 'bar', 'baz', 'foo'],
@@ -491,12 +491,12 @@ def __init__(
491491
else:
492492
try:
493493
arr = np.array(data, dtype=dtype, copy=copy)
494-
except (ValueError, TypeError) as e:
494+
except (ValueError, TypeError) as err:
495495
exc = TypeError(
496496
"DataFrame constructor called with "
497-
f"incompatible data and dtype: {e}"
497+
f"incompatible data and dtype: {err}"
498498
)
499-
raise exc from e
499+
raise exc from err
500500

501501
if arr.ndim == 0 and index is not None and columns is not None:
502502
values = cast_scalar_to_array(
@@ -794,7 +794,6 @@ def to_string(
794794
1 2 5
795795
2 3 6
796796
"""
797-
798797
from pandas import option_context
799798

800799
with option_context("display.max_colwidth", max_colwidth):
@@ -1583,7 +1582,6 @@ def from_records(
15831582
-------
15841583
DataFrame
15851584
"""
1586-
15871585
# Make a copy of the input columns so we can modify it
15881586
if columns is not None:
15891587
columns = ensure_index(columns)
@@ -1764,7 +1762,6 @@ def to_records(
17641762
rec.array([(b'a', 1, 0.5 ), (b'b', 2, 0.75)],
17651763
dtype=[('I', 'S1'), ('A', '<i8'), ('B', '<f8')])
17661764
"""
1767-
17681765
if index:
17691766
if isinstance(self.index, ABCMultiIndex):
17701767
# array of tuples to numpy cols. copy copy copy
@@ -2185,7 +2182,6 @@ def to_html(
21852182
--------
21862183
to_string : Convert DataFrame to a string.
21872184
"""
2188-
21892185
if justify is not None and justify not in fmt._VALID_JUSTIFY_PARAMETERS:
21902186
raise ValueError("Invalid value for justify parameter")
21912187

@@ -2359,7 +2355,6 @@ def info(
23592355
dtypes: object(3)
23602356
memory usage: 188.8 MB
23612357
"""
2362-
23632358
if buf is None: # pragma: no cover
23642359
buf = sys.stdout
23652360

@@ -2985,7 +2980,6 @@ def _set_item(self, key, value):
29852980
Series/TimeSeries will be conformed to the DataFrames index to
29862981
ensure homogeneity.
29872982
"""
2988-
29892983
self._ensure_valid_index(value)
29902984
value = self._sanitize_column(key, value)
29912985
NDFrame._set_item(self, key, value)
@@ -3045,8 +3039,7 @@ def _ensure_valid_index(self, value):
30453039
except (ValueError, NotImplementedError, TypeError):
30463040
raise ValueError(
30473041
"Cannot set a frame with no defined index "
3048-
"and a value that cannot be converted to a "
3049-
"Series"
3042+
"and a value that cannot be converted to a Series"
30503043
)
30513044

30523045
self._data = self._data.reindex_axis(
@@ -3414,7 +3407,6 @@ def select_dtypes(self, include=None, exclude=None) -> "DataFrame":
34143407
4 True 1.0
34153408
5 False 2.0
34163409
"""
3417-
34183410
if not is_list_like(include):
34193411
include = (include,) if include is not None else ()
34203412
if not is_list_like(exclude):
@@ -3685,11 +3677,7 @@ def lookup(self, row_labels, col_labels) -> np.ndarray:
36853677
Returns
36863678
-------
36873679
numpy.ndarray
3688-
3689-
Examples
3690-
--------
3691-
values : ndarray
3692-
The found values
3680+
The found values.
36933681
"""
36943682
n = len(row_labels)
36953683
if n != len(col_labels):
@@ -3780,7 +3768,6 @@ def _reindex_multi(self, axes, copy, fill_value) -> "DataFrame":
37803768
"""
37813769
We are guaranteed non-Nones in the axes.
37823770
"""
3783-
37843771
new_index, row_indexer = self.index.reindex(axes["index"])
37853772
new_columns, col_indexer = self.columns.reindex(axes["columns"])
37863773

@@ -4101,7 +4088,6 @@ def rename(
41014088
41024089
Examples
41034090
--------
4104-
41054091
``DataFrame.rename`` supports two calling conventions
41064092
41074093
* ``(index=index_mapper, columns=columns_mapper, ...)``
@@ -5591,7 +5577,6 @@ def combine_first(self, other: "DataFrame") -> "DataFrame":
55915577
55925578
Examples
55935579
--------
5594-
55955580
>>> df1 = pd.DataFrame({'A': [None, 0], 'B': [None, 4]})
55965581
>>> df2 = pd.DataFrame({'A': [1, 1], 'B': [3, 3]})
55975582
>>> df1.combine_first(df2)
@@ -6370,7 +6355,6 @@ def explode(self, column: Union[str, Tuple]) -> "DataFrame":
63706355
3 3 1
63716356
3 4 1
63726357
"""
6373-
63746358
if not (is_scalar(column) or isinstance(column, tuple)):
63756359
raise ValueError("column must be a scalar")
63766360
if not self.columns.is_unique:
@@ -6855,7 +6839,6 @@ def apply(self, func, axis=0, raw=False, result_type=None, args=(), **kwds):
68556839
68566840
Examples
68576841
--------
6858-
68596842
>>> df = pd.DataFrame([[4, 9]] * 3, columns=['A', 'B'])
68606843
>>> df
68616844
A B
@@ -7050,7 +7033,6 @@ def append(
70507033
70517034
Examples
70527035
--------
7053-
70547036
>>> df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
70557037
>>> df
70567038
A B
@@ -8432,7 +8414,6 @@ def isin(self, values) -> "DataFrame":
84328414
84338415
Examples
84348416
--------
8435-
84368417
>>> df = pd.DataFrame({'num_legs': [2, 4], 'num_wings': [2, 0]},
84378418
... index=['falcon', 'dog'])
84388419
>>> df
@@ -8493,7 +8474,7 @@ def isin(self, values) -> "DataFrame":
84938474
raise TypeError(
84948475
"only list-like or dict-like objects are allowed "
84958476
"to be passed to DataFrame.isin(), "
8496-
f"you passed a {repr(type(values).__name__)}"
8477+
f"you passed a '{type(values).__name__}'"
84978478
)
84988479
return DataFrame(
84998480
algorithms.isin(self.values.ravel(), values).reshape(self.shape),

pandas/tests/frame/methods/test_diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_diff(self, datetime_frame):
1515
)
1616

1717
# int dtype
18-
a = 10000000000000000
18+
a = 10_000_000_000_000_000
1919
b = a + 1
2020
s = Series([a, b])
2121

pandas/tests/test_take.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def test_2d_float32(self):
345345

346346
def test_2d_datetime64(self):
347347
# 2005/01/01 - 2006/01/01
348-
arr = np.random.randint(11045376, 11360736, (5, 3)) * 100000000000
348+
arr = np.random.randint(11_045_376, 11_360_736, (5, 3)) * 100_000_000_000
349349
arr = arr.view(dtype="datetime64[ns]")
350350
indexer = [0, 2, -1, 1, -1]
351351

@@ -452,7 +452,7 @@ def test_take_empty(self, allow_fill):
452452
tm.assert_numpy_array_equal(arr, result)
453453

454454
msg = (
455-
r"cannot do a non-empty take from an empty axes.|"
455+
"cannot do a non-empty take from an empty axes.|"
456456
"indices are out-of-bounds"
457457
)
458458
with pytest.raises(IndexError, match=msg):

0 commit comments

Comments
 (0)