diff --git a/pandas/core/computation/align.py b/pandas/core/computation/align.py index a1b1cffdd1d76..e45d3ca66b6ec 100644 --- a/pandas/core/computation/align.py +++ b/pandas/core/computation/align.py @@ -1,4 +1,5 @@ -"""Core eval alignment algorithms +""" +Core eval alignment algorithms. """ from functools import partial, wraps diff --git a/pandas/core/computation/eval.py b/pandas/core/computation/eval.py index 71e1b6c2a08a9..4cdf4bac61316 100644 --- a/pandas/core/computation/eval.py +++ b/pandas/core/computation/eval.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 - """ Top level ``eval`` module. """ @@ -26,30 +25,29 @@ def _check_engine(engine: Optional[str]) -> str: Parameters ---------- engine : str + String to validate. Raises ------ KeyError - * If an invalid engine is passed + * If an invalid engine is passed. ImportError - * If numexpr was requested but doesn't exist + * If numexpr was requested but doesn't exist. Returns ------- - string engine + str + Engine name. """ from pandas.core.computation.check import _NUMEXPR_INSTALLED if engine is None: - if _NUMEXPR_INSTALLED: - engine = "numexpr" - else: - engine = "python" + engine = "numexpr" if _NUMEXPR_INSTALLED else "python" if engine not in _engines: - valid = list(_engines.keys()) + valid_engines = list(_engines.keys()) raise KeyError( - f"Invalid engine {repr(engine)} passed, valid engines are {valid}" + f"Invalid engine '{engine}' passed, valid engines are {valid_engines}" ) # TODO: validate this in a more general way (thinking of future engines @@ -58,10 +56,8 @@ def _check_engine(engine: Optional[str]) -> str: if engine == "numexpr": if not _NUMEXPR_INSTALLED: raise ImportError( - "'numexpr' is not installed or an " - "unsupported version. Cannot use " - "engine='numexpr' for query/eval " - "if 'numexpr' is not installed" + "'numexpr' is not installed or an unsupported version. Cannot use " + "engine='numexpr' for query/eval if 'numexpr' is not installed" ) return engine @@ -80,11 +76,9 @@ def _check_parser(parser: str): KeyError * If an invalid parser is passed """ - if parser not in _parsers: raise KeyError( - f"Invalid parser {repr(parser)} passed, " - f"valid parsers are {_parsers.keys()}" + f"Invalid parser '{parser}' passed, valid parsers are {_parsers.keys()}" ) @@ -94,8 +88,8 @@ def _check_resolvers(resolvers): if not hasattr(resolver, "__getitem__"): name = type(resolver).__name__ raise TypeError( - f"Resolver of type {repr(name)} does not " - f"implement the __getitem__ method" + f"Resolver of type '{name}' does not " + "implement the __getitem__ method" ) @@ -155,10 +149,8 @@ def _check_for_locals(expr: str, stack_level: int, parser: str): msg = "The '@' prefix is only supported by the pandas parser" elif at_top_of_stack: msg = ( - "The '@' prefix is not allowed in " - "top-level eval calls, \nplease refer to " - "your variables by name without the '@' " - "prefix" + "The '@' prefix is not allowed in top-level eval calls.\n" + "please refer to your variables by name without the '@' prefix." ) if at_top_of_stack or not_pandas_parser: @@ -285,13 +277,14 @@ def eval( See the :ref:`enhancing performance ` documentation for more details. """ - inplace = validate_bool_kwarg(inplace, "inplace") if truediv is not no_default: warnings.warn( - "The `truediv` parameter in pd.eval is deprecated and will be " - "removed in a future version.", + ( + "The `truediv` parameter in pd.eval is deprecated and " + "will be removed in a future version." + ), FutureWarning, stacklevel=2, ) diff --git a/pandas/core/computation/expressions.py b/pandas/core/computation/expressions.py index 7e959889ee997..ada983e9e4fad 100644 --- a/pandas/core/computation/expressions.py +++ b/pandas/core/computation/expressions.py @@ -45,12 +45,9 @@ def set_use_numexpr(v=True): # choose what we are going to do global _evaluate, _where - if not _USE_NUMEXPR: - _evaluate = _evaluate_standard - _where = _where_standard - else: - _evaluate = _evaluate_numexpr - _where = _where_numexpr + + _evaluate = _evaluate_numexpr if _USE_NUMEXPR else _evaluate_standard + _where = _where_numexpr if _USE_NUMEXPR else _where_standard def set_numexpr_threads(n=None): @@ -63,7 +60,9 @@ def set_numexpr_threads(n=None): def _evaluate_standard(op, op_str, a, b): - """ standard evaluation """ + """ + Standard evaluation. + """ if _TEST_MODE: _store_test_result(False) with np.errstate(all="ignore"): @@ -176,7 +175,7 @@ def _bool_arith_check( if op_str in unsupported: warnings.warn( f"evaluating in Python space because the {repr(op_str)} " - f"operator is not supported by numexpr for " + "operator is not supported by numexpr for " f"the bool dtype, use {repr(unsupported[op_str])} instead" ) return False @@ -202,7 +201,6 @@ def evaluate(op, op_str, a, b, use_numexpr=True): use_numexpr : bool, default True Whether to try to use numexpr. """ - use_numexpr = use_numexpr and _bool_arith_check(op_str, a, b) if use_numexpr: return _evaluate(op, op_str, a, b) @@ -221,10 +219,7 @@ def where(cond, a, b, use_numexpr=True): use_numexpr : bool, default True Whether to try to use numexpr. """ - - if use_numexpr: - return _where(cond, a, b) - return _where_standard(cond, a, b) + return _where(cond, a, b) if use_numexpr else _where_standard(cond, a, b) def set_test_mode(v=True): diff --git a/pandas/core/frame.py b/pandas/core/frame.py index fa9a951d6849c..4257083cc8dc5 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8,6 +8,7 @@ alignment and a host of useful data manipulation methods having to do with the labeling information """ + import collections from collections import abc from io import StringIO @@ -258,7 +259,6 @@ Examples -------- - >>> df1 = pd.DataFrame({'lkey': ['foo', 'bar', 'baz', 'foo'], ... 'value': [1, 2, 3, 5]}) >>> df2 = pd.DataFrame({'rkey': ['foo', 'bar', 'baz', 'foo'], @@ -491,12 +491,12 @@ def __init__( else: try: arr = np.array(data, dtype=dtype, copy=copy) - except (ValueError, TypeError) as e: + except (ValueError, TypeError) as err: exc = TypeError( "DataFrame constructor called with " - f"incompatible data and dtype: {e}" + f"incompatible data and dtype: {err}" ) - raise exc from e + raise exc from err if arr.ndim == 0 and index is not None and columns is not None: values = cast_scalar_to_array( @@ -794,7 +794,6 @@ def to_string( 1 2 5 2 3 6 """ - from pandas import option_context with option_context("display.max_colwidth", max_colwidth): @@ -1583,7 +1582,6 @@ def from_records( ------- DataFrame """ - # Make a copy of the input columns so we can modify it if columns is not None: columns = ensure_index(columns) @@ -1764,7 +1762,6 @@ def to_records( rec.array([(b'a', 1, 0.5 ), (b'b', 2, 0.75)], dtype=[('I', 'S1'), ('A', ' "DataFrame": 4 True 1.0 5 False 2.0 """ - if not is_list_like(include): include = (include,) if include is not None else () if not is_list_like(exclude): @@ -3685,11 +3677,7 @@ def lookup(self, row_labels, col_labels) -> np.ndarray: Returns ------- numpy.ndarray - - Examples - -------- - values : ndarray - The found values + The found values. """ n = len(row_labels) if n != len(col_labels): @@ -3780,7 +3768,6 @@ def _reindex_multi(self, axes, copy, fill_value) -> "DataFrame": """ We are guaranteed non-Nones in the axes. """ - new_index, row_indexer = self.index.reindex(axes["index"]) new_columns, col_indexer = self.columns.reindex(axes["columns"]) @@ -4101,7 +4088,6 @@ def rename( Examples -------- - ``DataFrame.rename`` supports two calling conventions * ``(index=index_mapper, columns=columns_mapper, ...)`` @@ -5591,7 +5577,6 @@ def combine_first(self, other: "DataFrame") -> "DataFrame": Examples -------- - >>> df1 = pd.DataFrame({'A': [None, 0], 'B': [None, 4]}) >>> df2 = pd.DataFrame({'A': [1, 1], 'B': [3, 3]}) >>> df1.combine_first(df2) @@ -6370,7 +6355,6 @@ def explode(self, column: Union[str, Tuple]) -> "DataFrame": 3 3 1 3 4 1 """ - if not (is_scalar(column) or isinstance(column, tuple)): raise ValueError("column must be a scalar") if not self.columns.is_unique: @@ -6855,7 +6839,6 @@ def apply(self, func, axis=0, raw=False, result_type=None, args=(), **kwds): Examples -------- - >>> df = pd.DataFrame([[4, 9]] * 3, columns=['A', 'B']) >>> df A B @@ -7050,7 +7033,6 @@ def append( Examples -------- - >>> df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB')) >>> df A B @@ -8432,7 +8414,6 @@ def isin(self, values) -> "DataFrame": Examples -------- - >>> df = pd.DataFrame({'num_legs': [2, 4], 'num_wings': [2, 0]}, ... index=['falcon', 'dog']) >>> df @@ -8493,7 +8474,7 @@ def isin(self, values) -> "DataFrame": raise TypeError( "only list-like or dict-like objects are allowed " "to be passed to DataFrame.isin(), " - f"you passed a {repr(type(values).__name__)}" + f"you passed a '{type(values).__name__}'" ) return DataFrame( algorithms.isin(self.values.ravel(), values).reshape(self.shape), diff --git a/pandas/tests/frame/methods/test_diff.py b/pandas/tests/frame/methods/test_diff.py index 43c25f4c05c2d..ffdb6d41ebda5 100644 --- a/pandas/tests/frame/methods/test_diff.py +++ b/pandas/tests/frame/methods/test_diff.py @@ -15,7 +15,7 @@ def test_diff(self, datetime_frame): ) # int dtype - a = 10000000000000000 + a = 10_000_000_000_000_000 b = a + 1 s = Series([a, b]) diff --git a/pandas/tests/test_take.py b/pandas/tests/test_take.py index 1d2ab9358c01c..2534f1849cf61 100644 --- a/pandas/tests/test_take.py +++ b/pandas/tests/test_take.py @@ -345,7 +345,7 @@ def test_2d_float32(self): def test_2d_datetime64(self): # 2005/01/01 - 2006/01/01 - arr = np.random.randint(11045376, 11360736, (5, 3)) * 100000000000 + arr = np.random.randint(11_045_376, 11_360_736, (5, 3)) * 100_000_000_000 arr = arr.view(dtype="datetime64[ns]") indexer = [0, 2, -1, 1, -1] @@ -452,7 +452,7 @@ def test_take_empty(self, allow_fill): tm.assert_numpy_array_equal(arr, result) msg = ( - r"cannot do a non-empty take from an empty axes.|" + "cannot do a non-empty take from an empty axes.|" "indices are out-of-bounds" ) with pytest.raises(IndexError, match=msg):