Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def raise_patsy_error(x):
raise PatsyError("WHEEEEEE")
formula = "raise_patsy_error(X) + Y"
def t_invalid(formula_like, data, depth, exc=PatsyError): # pragma: no cover
if isinstance(depth, int):
depth += 1
fs = [dmatrix, dmatrices]
if have_pandas:
fs += [dmatrix_pandas, dmatrices_pandas]
for f in fs:
try:
f(formula_like, data, depth)
except exc:
pass
else:
raise AssertionError
def eval(self, tree, require_evalexpr=True):
result = None
assert isinstance(tree, ParseNode)
key = (tree.type, len(tree.args))
if key not in self._evaluators:
raise PatsyError("I don't know how to evaluate this "
"'%s' operator" % (tree.type,),
tree.token)
result = self._evaluators[key](self, tree)
if require_evalexpr and not isinstance(result, IntermediateExpr):
if isinstance(result, ModelDesc):
raise PatsyError("~ can only be used once, and "
"only at the top level",
tree)
else:
raise PatsyError("custom operator returned an "
"object that I don't know how to "
"handle", tree)
return result
def incr_dbuilders(formula_like, data_iter_maker, eval_env=0,
NA_action="drop"):
"""Construct two design matrix builders incrementally from a large data
set.
:func:`incr_dbuilders` is to :func:`incr_dbuilder` as :func:`dmatrices` is
to :func:`dmatrix`. See :func:`incr_dbuilder` for details.
"""
eval_env = EvalEnvironment.capture(eval_env, reference=1)
design_infos = _try_incr_builders(formula_like, data_iter_maker, eval_env,
NA_action)
if design_infos is None:
raise PatsyError("bad formula-like object")
if len(design_infos[0].column_names) == 0:
raise PatsyError("model is missing required outcome variables")
return design_infos
and isinstance(formula_like[0], DesignInfo)
and isinstance(formula_like[1], DesignInfo)):
return formula_like
if hasattr(formula_like, "__patsy_get_model_desc__"):
formula_like = formula_like.__patsy_get_model_desc__(eval_env)
if not isinstance(formula_like, ModelDesc):
raise PatsyError("bad value from %r.__patsy_get_model_desc__"
% (formula_like,))
# fallthrough
if not six.PY3 and isinstance(formula_like, unicode):
# Included for the convenience of people who are using py2 with
# __future__.unicode_literals.
try:
formula_like = formula_like.encode("ascii")
except UnicodeEncodeError:
raise PatsyError(
"On Python 2, formula strings must be either 'str' objects, "
"or else 'unicode' objects containing only ascii "
"characters. You passed a unicode string with non-ascii "
"characters. I'm afraid you'll have to either switch to "
"ascii-only, or else upgrade to Python 3.")
if isinstance(formula_like, str):
formula_like = ModelDesc.from_formula(formula_like)
# fallthrough
if isinstance(formula_like, ModelDesc):
assert isinstance(eval_env, EvalEnvironment)
return design_matrix_builders([formula_like.lhs_termlist,
formula_like.rhs_termlist],
data_iter_maker,
eval_env,
NA_action)
else:
The actual contents of the design matrix is identical in both cases, and
in both cases a :class:`DesignInfo` object will be available in a
``.design_info`` attribute on the return value. However, for
``return_type="dataframe"``, any pandas indexes on the input (either in
`data` or directly passed through `formula_like`) will be preserved, which
may be useful for e.g. time-series models.
.. versionadded:: 0.2.0
The ``NA_action`` argument.
"""
eval_env = EvalEnvironment.capture(eval_env, reference=1)
(lhs, rhs) = _do_highlevel_design(formula_like, data, eval_env,
NA_action, return_type)
if lhs.shape[1] != 0:
raise PatsyError("encountered outcome variables for a model "
"that does not expect them")
return rhs
def _eval_number(evaluator, tree):
raise PatsyError("numbers besides '0' and '1' are "
"only allowed with **", tree)
assert di.terms == [t_x, t_y]
assert di.column_name_indexes == {"x1": 0, "x2": 1, "x3": 2, "y": 3}
assert di.term_name_slices == {"x": slice(0, 3), "y": slice(3, 4)}
assert di.term_slices == {t_x: slice(0, 3), t_y: slice(3, 4)}
assert di.describe() == "x + y"
assert di.slice(1) == slice(1, 2)
assert di.slice("x1") == slice(0, 1)
assert di.slice("x2") == slice(1, 2)
assert di.slice("x3") == slice(2, 3)
assert di.slice("x") == slice(0, 3)
assert di.slice(t_x) == slice(0, 3)
assert di.slice("y") == slice(3, 4)
assert di.slice(t_y) == slice(3, 4)
assert di.slice(slice(2, 4)) == slice(2, 4)
assert_raises(PatsyError, di.slice, "asdf")
# smoke test
repr(di)
assert_no_pickling(di)
# One without term objects
di = DesignInfo(["a1", "a2", "a3", "b"])
assert di.column_names == ["a1", "a2", "a3", "b"]
assert di.term_names == ["a1", "a2", "a3", "b"]
assert di.terms is None
assert di.column_name_indexes == {"a1": 0, "a2": 1, "a3": 2, "b": 3}
assert di.term_name_slices == {"a1": slice(0, 1),
"a2": slice(1, 2),
"a3": slice(2, 3),
"b": slice(3, 4)}
m = atleast_2d_column_default(m, preserve_pandas=True)
m = pandas.DataFrame(m)
m.columns = di.column_names
m.design_info = di
return (m, orig_index)
else:
return (DesignMatrix(m, di), orig_index)
rhs, rhs_orig_index = _regularize_matrix(rhs, "x")
if lhs is None:
lhs = np.zeros((rhs.shape[0], 0), dtype=float)
lhs, lhs_orig_index = _regularize_matrix(lhs, "y")
assert isinstance(getattr(lhs, "design_info", None), DesignInfo)
assert isinstance(getattr(rhs, "design_info", None), DesignInfo)
if lhs.shape[0] != rhs.shape[0]:
raise PatsyError("shape mismatch: outcome matrix has %s rows, "
"predictor matrix has %s rows"
% (lhs.shape[0], rhs.shape[0]))
if rhs_orig_index is not None and lhs_orig_index is not None:
if not rhs_orig_index.equals(lhs_orig_index):
raise PatsyError("index mismatch: outcome and "
"predictor have incompatible indexes")
if return_type == "dataframe":
if rhs_orig_index is not None and lhs_orig_index is None:
lhs.index = rhs.index
if rhs_orig_index is None and lhs_orig_index is not None:
rhs.index = lhs.index
return (lhs, rhs)