Skip to content

CLN/STY: various code cleanups #31162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pandas/core/computation/align.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Core eval alignment algorithms
"""
Core eval alignment algorithms.
"""

from functools import partial, wraps
Expand Down
45 changes: 19 additions & 26 deletions pandas/core/computation/eval.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3

"""
Top level ``eval`` module.
"""
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()}"
)


Expand All @@ -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"
)


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -285,13 +277,14 @@ def eval(
See the :ref:`enhancing performance <enhancingperf.eval>` 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,
)
Expand Down
21 changes: 8 additions & 13 deletions pandas/core/computation/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"):
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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):
Expand Down
33 changes: 7 additions & 26 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1764,7 +1762,6 @@ def to_records(
rec.array([(b'a', 1, 0.5 ), (b'b', 2, 0.75)],
dtype=[('I', 'S1'), ('A', '<i8'), ('B', '<f8')])
"""

if index:
if isinstance(self.index, ABCMultiIndex):
# array of tuples to numpy cols. copy copy copy
Expand Down Expand Up @@ -2185,7 +2182,6 @@ def to_html(
--------
to_string : Convert DataFrame to a string.
"""

if justify is not None and justify not in fmt._VALID_JUSTIFY_PARAMETERS:
raise ValueError("Invalid value for justify parameter")

Expand Down Expand Up @@ -2359,7 +2355,6 @@ def info(
dtypes: object(3)
memory usage: 188.8 MB
"""

if buf is None: # pragma: no cover
buf = sys.stdout

Expand Down Expand Up @@ -2985,7 +2980,6 @@ def _set_item(self, key, value):
Series/TimeSeries will be conformed to the DataFrames index to
ensure homogeneity.
"""

self._ensure_valid_index(value)
value = self._sanitize_column(key, value)
NDFrame._set_item(self, key, value)
Expand Down Expand Up @@ -3045,8 +3039,7 @@ def _ensure_valid_index(self, value):
except (ValueError, NotImplementedError, TypeError):
raise ValueError(
"Cannot set a frame with no defined index "
"and a value that cannot be converted to a "
"Series"
"and a value that cannot be converted to a Series"
)

self._data = self._data.reindex_axis(
Expand Down Expand Up @@ -3414,7 +3407,6 @@ def select_dtypes(self, include=None, exclude=None) -> "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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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"])

Expand Down Expand Up @@ -4101,7 +4088,6 @@ def rename(

Examples
--------

``DataFrame.rename`` supports two calling conventions

* ``(index=index_mapper, columns=columns_mapper, ...)``
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -7050,7 +7033,6 @@ def append(

Examples
--------

>>> df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
>>> df
A B
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/test_take.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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):
Expand Down