Skip to content

CLN: remove dead code in pandas.computation #6448

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
Feb 23, 2014
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
81 changes: 4 additions & 77 deletions pandas/computation/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,6 @@ def _zip_axes_from_type(typ, new_axes):
return axes


def _maybe_promote_shape(values, naxes):
# test to see if we have an array else leave since must be a number
if not isinstance(values, np.ndarray):
return values

ndims = values.ndim
if ndims > naxes:
raise AssertionError('cannot have more dims than axes, '
'{0} > {1}'.format(ndims, naxes))
if ndims == naxes:
return values

ndim, nax = range(ndims), range(naxes)

axes_slice = [slice(None)] * naxes

# set difference of numaxes and ndims
slices = list(set(nax) - set(ndim))

if ndims == naxes:
if slices:
raise AssertionError('slices should be empty if ndims == naxes '
'{0}'.format(slices))
else:
if not slices:
raise AssertionError('slices should NOT be empty if ndim != naxes '
'{0}'.format(slices))

for sl in slices:
axes_slice[sl] = np.newaxis

return values[tuple(axes_slice)]


def _any_pandas_objects(terms):
"""Check a sequence of terms for instances of PandasObject."""
return any(isinstance(term.value, pd.core.generic.PandasObject)
Expand All @@ -83,12 +49,7 @@ def wrapper(terms):

term_values = (term.value for term in terms)

# only scalars or indexes
if all(isinstance(term.value, pd.Index) or term.isscalar for term in
terms):
return _result_type_many(*term_values), None

# no pandas objects
# we don't have any pandas objects
if not _any_pandas_objects(terms):
return _result_type_many(*term_values), None

Expand Down Expand Up @@ -148,44 +109,13 @@ def _align_core(terms):
f = partial(ti.reindex_axis, reindexer, axis=axis,
copy=False)

# need to fill if we have a bool dtype/array
if (isinstance(ti, (np.ndarray, pd.Series))
and ti.dtype == object
and pd.lib.is_bool_array(ti.values)):
r = f(fill_value=True)
else:
r = f()

terms[i].update(r)
terms[i].update(f())

res = _maybe_promote_shape(terms[i].value.T if transpose else
terms[i].value, naxes)
res = res.T if transpose else res

try:
v = res.values
except AttributeError:
v = res
terms[i].update(v)
terms[i].update(terms[i].value.values)

return typ, _zip_axes_from_type(typ, axes)


def _filter_terms(flat):
# numeric literals
literals = frozenset(filter(lambda x: isinstance(x, Constant), flat))

# these are strings which are variable names
names = frozenset(flat) - literals

# literals are not names and names are not literals, so intersection should
# be empty
if literals & names:
raise ValueError('literals cannot be names and names cannot be '
'literals')
return names, literals


def _align(terms):
"""Align a set of terms"""
try:
Expand Down Expand Up @@ -231,10 +161,7 @@ def _reconstruct_object(typ, obj, axes, dtype):
except AttributeError:
pass

try:
res_t = np.result_type(obj.dtype, dtype)
except AttributeError:
res_t = dtype
res_t = np.result_type(obj.dtype, dtype)

if (not isinstance(typ, partial) and
issubclass(typ, pd.core.generic.PandasObject)):
Expand Down
5 changes: 1 addition & 4 deletions pandas/computation/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ def _result_type_many(*arrays_and_dtypes):
try:
return np.result_type(*arrays_and_dtypes)
except ValueError:
# length 0 or length > NPY_MAXARGS both throw a ValueError, so check
# which one we're dealing with
if len(arrays_and_dtypes) == 0:
raise ValueError('at least one array or dtype is required')
# we have > NPY_MAXARGS terms in our expression
return reduce(np.result_type, arrays_and_dtypes)


Expand Down
1 change: 0 additions & 1 deletion pandas/computation/engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ def _evaluate(self):
raise UndefinedVariableError(msg)



class PythonEngine(AbstractEngine):

"""Evaluate an expression in Python space.
Expand Down
4 changes: 2 additions & 2 deletions pandas/computation/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def _check_resolvers(resolvers):
for resolver in resolvers:
if not hasattr(resolver, '__getitem__'):
name = type(resolver).__name__
raise AttributeError('Resolver of type {0!r} must implement '
'the __getitem__ method'.format(name))
raise TypeError('Resolver of type %r does not implement '
'the __getitem__ method' % name)


def _check_expression(expr):
Expand Down
9 changes: 1 addition & 8 deletions pandas/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,6 @@ def visit(self, node, **kwargs):
if isinstance(node, string_types):
clean = self.preparser(node)
node = ast.fix_missing_locations(ast.parse(clean))
elif not isinstance(node, ast.AST):
raise TypeError("Cannot visit objects of type {0!r}"
"".format(node.__class__.__name__))

method = 'visit_' + node.__class__.__name__
visitor = getattr(self, method)
Expand Down Expand Up @@ -533,7 +530,7 @@ def visit_Call(self, node, side=None, **kwargs):

args = [self.visit(targ).value for targ in node.args]
if node.starargs is not None:
args = args + self.visit(node.starargs).value
args += self.visit(node.starargs).value

keywords = {}
for key in node.keywords:
Expand Down Expand Up @@ -651,10 +648,6 @@ def parse(self):
"""Parse an expression"""
return self._visitor.visit(self.expr)

def align(self):
"""align a set of Terms"""
return self.terms.align(self.env)

@property
def names(self):
"""Get the names in an expression"""
Expand Down
26 changes: 7 additions & 19 deletions pandas/computation/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ class UndefinedVariableError(NameError):

"""NameError subclass for local variables."""

def __init__(self, *args):
msg = 'name {0!r} is not defined'
subbed = args[0].replace(_LOCAL_TAG, '')
if subbed != args[0]:
subbed = '@' + subbed
def __init__(self, name, is_local):
if is_local:
msg = 'local variable {0!r} is not defined'
super(UndefinedVariableError, self).__init__(msg.format(subbed))
else:
msg = 'name {0!r} is not defined'
super(UndefinedVariableError, self).__init__(msg.format(name))


class Term(StringMixin):
Expand Down Expand Up @@ -73,11 +72,6 @@ def _resolve_name(self):
res = self.env.resolve(self.local_name, is_local=self.is_local)
self.update(res)

if res is None:
if not isinstance(key, string_types):
return key
raise UndefinedVariableError(key)

if hasattr(res, 'ndim') and res.ndim > 2:
raise NotImplementedError("N-dimensional objects, where N > 2,"
" are not supported with eval")
Expand All @@ -97,10 +91,7 @@ def update(self, value):

# if it's a variable name (otherwise a constant)
if isinstance(key, string_types):
try:
self.env.swapkey(self.local_name, key, new_value=value)
except KeyError:
raise UndefinedVariableError(key)
self.env.swapkey(self.local_name, key, new_value=value)

self.value = value

Expand Down Expand Up @@ -156,10 +147,7 @@ def name(self, new_name):

@property
def ndim(self):
try:
return self._value.ndim
except AttributeError:
return 0
return self._value.ndim


class Constant(Term):
Expand Down
13 changes: 0 additions & 13 deletions pandas/computation/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ def __init__(self, value, env, side=None, encoding=None):
def _resolve_name(self):
return self._name

@property
def name(self):
return self._value


class BinOp(ops.BinOp):

Expand Down Expand Up @@ -233,9 +229,6 @@ def format(self):

def evaluate(self):

if not isinstance(self.lhs, string_types):
return self

if not self.is_valid:
raise ValueError("query term is not valid [%s]" % self)

Expand Down Expand Up @@ -307,9 +300,6 @@ def format(self):

def evaluate(self):

if not isinstance(self.lhs, string_types):
return self

if not self.is_valid:
raise ValueError("query term is not valid [%s]" % self)

Expand Down Expand Up @@ -390,9 +380,6 @@ def visit_UnaryOp(self, node, **kwargs):
elif isinstance(node.op, ast.UAdd):
raise NotImplementedError('Unary addition not supported')

def visit_USub(self, node, **kwargs):
return self.const_type(-self.visit(node.operand).value, self.env)

def visit_Index(self, node, **kwargs):
return self.visit(node.value).value

Expand Down
8 changes: 2 additions & 6 deletions pandas/computation/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def resolve(self, key, is_local):
# e.g., df[df > 0]
return self.temps[key]
except KeyError:
raise UndefinedVariableError(key)
raise UndefinedVariableError(key, is_local)

def swapkey(self, old_key, new_key, new_value=None):
"""Replace a variable name, with a potentially new value.
Expand All @@ -209,12 +209,8 @@ def swapkey(self, old_key, new_key, new_value=None):

for mapping in maps:
if old_key in mapping:
if new_value is None:
mapping[new_key] = mapping.pop(old_key)
else:
mapping[new_key] = new_value
mapping[new_key] = new_value
return
raise KeyError(old_key)

def _get_vars(self, stack, scopes):
"""Get specifically scoped variables from a list of stack frames.
Expand Down
Loading