本文整理汇总了Python中pandas.eval方法的典型用法代码示例。如果您正苦于以下问题:Python pandas.eval方法的具体用法?Python pandas.eval怎么用?Python pandas.eval使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas
的用法示例。
在下文中一共展示了pandas.eval方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _squared_sim
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def _squared_sim(d, scale, offset=0, origin=0):
if offset < 0:
raise ValueError("The offset must be positive.")
if scale <= 0:
raise ValueError("The scale must be larger than 0. ")
d = (abs(d - origin)).clip(offset, offset + np.sqrt(2) * scale)
# solve y=1-ad^2 given y(d=scale)=0.5
# 1-y = ad^2
# a = (1-y)/d^2
# fill y=0.5 and d = scale
# a = (1-0.5)/scale^2
# a = 1/(2*scale^2)
# y = 1 - 1/2*(d/scale)^2
# d = sqrt(2)*scale is the point where similarity is zero.
expr = '1 - 1/2*exp(2*log((d-offset)/scale))'
return pandas.eval(expr)
示例2: data_processor
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def data_processor(mol2):
pdmol = PandasMol2().read_mol2_from_list(mol2_lines=mol2[1],
mol2_code=mol2[0])
coordinates = pdmol.df.loc[pd.eval(SELECTION[0]), ['x', 'y', 'z']].values
pdmol._df = pdmol._df[pd.eval(SELECTION[1])]
for xyz in coordinates:
distances = pdmol.distance(xyz)
match = ((distances.values >= DISTANCE[0]).any() and
(distances.values <= DISTANCE[1]).any())
if match:
return mol2[0]
return ''
示例3: data_processor_gz
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def data_processor_gz(mol2_gz):
pdmol = PandasMol2().read_mol2_from_list(mol2_lines=mol2_gz[1],
mol2_code=mol2_gz[0])
coordinates = pdmol.df.loc[pd.eval(SELECTION[0]), ['x', 'y', 'z']].values
pdmol._df = pdmol._df[pd.eval(SELECTION[1])]
for xyz in coordinates:
distances = pdmol.distance(xyz)
match = ((distances.values >= DISTANCE[0]).any() and
(distances.values <= DISTANCE[1]).any())
if match:
return mol2_gz[0].decode('utf-8')
return ''
示例4: test_invalid_numexpr_version
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def test_invalid_numexpr_version(engine, parser):
def testit():
a, b = 1, 2 # noqa
res = pd.eval('a + b', engine=engine, parser=parser)
assert res == 3
if engine == 'numexpr':
try:
import numexpr as ne
except ImportError:
pytest.skip("no numexpr")
else:
if (LooseVersion(ne.__version__) <
LooseVersion(_MIN_NUMEXPR_VERSION)):
with pytest.raises(ImportError):
testit()
else:
testit()
else:
testit()
示例5: check_chained_cmp_op
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def check_chained_cmp_op(self, lhs, cmp1, mid, cmp2, rhs):
def check_operands(left, right, cmp_op):
return _eval_single_bin(left, cmp_op, right, self.engine)
lhs_new = check_operands(lhs, mid, cmp1)
rhs_new = check_operands(mid, rhs, cmp2)
if lhs_new is not None and rhs_new is not None:
ex1 = 'lhs {0} mid {1} rhs'.format(cmp1, cmp2)
ex2 = 'lhs {0} mid and mid {1} rhs'.format(cmp1, cmp2)
ex3 = '(lhs {0} mid) & (mid {1} rhs)'.format(cmp1, cmp2)
expected = _eval_single_bin(lhs_new, '&', rhs_new, self.engine)
for ex in (ex1, ex2, ex3):
result = pd.eval(ex, engine=self.engine,
parser=self.parser)
tm.assert_almost_equal(result, expected)
示例6: check_pow
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def check_pow(self, lhs, arith1, rhs):
ex = 'lhs {0} rhs'.format(arith1)
expected = self.get_expected_pow_result(lhs, rhs)
result = pd.eval(ex, engine=self.engine, parser=self.parser)
if (is_scalar(lhs) and is_scalar(rhs) and
_is_py3_complex_incompat(result, expected)):
pytest.raises(AssertionError, tm.assert_numpy_array_equal,
result, expected)
else:
tm.assert_almost_equal(result, expected)
ex = '(lhs {0} rhs) {0} rhs'.format(arith1)
result = pd.eval(ex, engine=self.engine, parser=self.parser)
expected = self.get_expected_pow_result(
self.get_expected_pow_result(lhs, rhs), rhs)
tm.assert_almost_equal(result, expected)
示例7: check_compound_invert_op
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def check_compound_invert_op(self, lhs, cmp1, rhs):
skip_these = 'in', 'not in'
ex = '~(lhs {0} rhs)'.format(cmp1)
if is_scalar(rhs) and cmp1 in skip_these:
pytest.raises(TypeError, pd.eval, ex, engine=self.engine,
parser=self.parser, local_dict={'lhs': lhs,
'rhs': rhs})
else:
# compound
if is_scalar(lhs) and is_scalar(rhs):
lhs, rhs = map(lambda x: np.array([x]), (lhs, rhs))
expected = _eval_single_bin(lhs, cmp1, rhs, self.engine)
if is_scalar(expected):
expected = not expected
else:
expected = ~expected
result = pd.eval(ex, engine=self.engine, parser=self.parser)
tm.assert_almost_equal(expected, result)
# make sure the other engines work the same as this one
for engine in self.current_engines:
ev = pd.eval(ex, engine=self.engine, parser=self.parser)
tm.assert_almost_equal(ev, result)
示例8: test_frame_negate
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def test_frame_negate(self):
expr = self.ex('-')
# float
lhs = DataFrame(randn(5, 2))
expect = -lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
assert_frame_equal(expect, result)
# int
lhs = DataFrame(randint(5, size=(5, 2)))
expect = -lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
assert_frame_equal(expect, result)
# bool doesn't work with numexpr but works elsewhere
lhs = DataFrame(rand(5, 2) > 0.5)
if self.engine == 'numexpr':
with pytest.raises(NotImplementedError):
result = pd.eval(expr, engine=self.engine, parser=self.parser)
else:
expect = -lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
assert_frame_equal(expect, result)
示例9: test_series_negate
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def test_series_negate(self):
expr = self.ex('-')
# float
lhs = Series(randn(5))
expect = -lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
assert_series_equal(expect, result)
# int
lhs = Series(randint(5, size=5))
expect = -lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
assert_series_equal(expect, result)
# bool doesn't work with numexpr but works elsewhere
lhs = Series(rand(5) > 0.5)
if self.engine == 'numexpr':
with pytest.raises(NotImplementedError):
result = pd.eval(expr, engine=self.engine, parser=self.parser)
else:
expect = -lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
assert_series_equal(expect, result)
示例10: test_frame_pos
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def test_frame_pos(self):
expr = self.ex('+')
# float
lhs = DataFrame(randn(5, 2))
expect = lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
assert_frame_equal(expect, result)
# int
lhs = DataFrame(randint(5, size=(5, 2)))
expect = lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
assert_frame_equal(expect, result)
# bool doesn't work with numexpr but works elsewhere
lhs = DataFrame(rand(5, 2) > 0.5)
expect = lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
assert_frame_equal(expect, result)
示例11: test_series_pos
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def test_series_pos(self):
expr = self.ex('+')
# float
lhs = Series(randn(5))
expect = lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
assert_series_equal(expect, result)
# int
lhs = Series(randint(5, size=5))
expect = lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
assert_series_equal(expect, result)
# bool doesn't work with numexpr but works elsewhere
lhs = Series(rand(5) > 0.5)
expect = lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
assert_series_equal(expect, result)
示例12: test_float_truncation
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def test_float_truncation(self):
# GH 14241
exp = '1000000000.006'
result = pd.eval(exp, engine=self.engine, parser=self.parser)
expected = np.float64(exp)
assert result == expected
df = pd.DataFrame({'A': [1000000000.0009,
1000000000.0011,
1000000000.0015]})
cutoff = 1000000000.0006
result = df.query("A < %.4f" % cutoff)
assert result.empty
cutoff = 1000000000.0010
result = df.query("A > %.4f" % cutoff)
expected = df.loc[[1, 2], :]
tm.assert_frame_equal(expected, result)
exact = 1000000000.0011
result = df.query('A == %.4f' % exact)
expected = df.loc[[1], :]
tm.assert_frame_equal(expected, result)
示例13: test_binop_typecasting
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def test_binop_typecasting(self, engine, parser, op, dt):
df = mkdf(5, 3, data_gen_f=f, dtype=dt)
s = 'df {} 3'.format(op)
res = pd.eval(s, engine=engine, parser=parser)
assert df.values.dtype == dt
assert res.values.dtype == dt
assert_frame_equal(res, eval(s))
s = '3 {} df'.format(op)
res = pd.eval(s, engine=engine, parser=parser)
assert df.values.dtype == dt
assert res.values.dtype == dt
assert_frame_equal(res, eval(s))
# -------------------------------------
# Basic and complex alignment
示例14: test_basic_frame_alignment
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def test_basic_frame_alignment(self, engine, parser):
args = product(self.lhs_index_types, self.index_types,
self.index_types)
with warnings.catch_warnings(record=True):
warnings.simplefilter('always', RuntimeWarning)
for lr_idx_type, rr_idx_type, c_idx_type in args:
df = mkdf(10, 10, data_gen_f=f, r_idx_type=lr_idx_type,
c_idx_type=c_idx_type)
df2 = mkdf(20, 10, data_gen_f=f, r_idx_type=rr_idx_type,
c_idx_type=c_idx_type)
# only warns if not monotonic and not sortable
if should_warn(df.index, df2.index):
with tm.assert_produces_warning(RuntimeWarning):
res = pd.eval('df + df2', engine=engine, parser=parser)
else:
res = pd.eval('df + df2', engine=engine, parser=parser)
assert_frame_equal(res, df + df2)
示例15: test_basic_frame_series_alignment
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import eval [as 别名]
def test_basic_frame_series_alignment(self, engine, parser):
def testit(r_idx_type, c_idx_type, index_name):
df = mkdf(10, 10, data_gen_f=f, r_idx_type=r_idx_type,
c_idx_type=c_idx_type)
index = getattr(df, index_name)
s = Series(np.random.randn(5), index[:5])
if should_warn(df.index, s.index):
with tm.assert_produces_warning(RuntimeWarning):
res = pd.eval('df + s', engine=engine, parser=parser)
else:
res = pd.eval('df + s', engine=engine, parser=parser)
if r_idx_type == 'dt' or c_idx_type == 'dt':
expected = df.add(s) if engine == 'numexpr' else df + s
else:
expected = df + s
assert_frame_equal(res, expected)
args = product(self.lhs_index_types, self.index_types,
('index', 'columns'))
with warnings.catch_warnings(record=True):
warnings.simplefilter('always', RuntimeWarning)
for r_idx_type, c_idx_type, index_name in args:
testit(r_idx_type, c_idx_type, index_name)