本文整理汇总了Python中pandas.core.dtypes.common.is_scalar方法的典型用法代码示例。如果您正苦于以下问题:Python common.is_scalar方法的具体用法?Python common.is_scalar怎么用?Python common.is_scalar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.core.dtypes.common
的用法示例。
在下文中一共展示了common.is_scalar方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_pow
# 需要导入模块: from pandas.core.dtypes import common [as 别名]
# 或者: from pandas.core.dtypes.common import is_scalar [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)
示例2: check_compound_invert_op
# 需要导入模块: from pandas.core.dtypes import common [as 别名]
# 或者: from pandas.core.dtypes.common import is_scalar [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)
示例3: check_compound_invert_op
# 需要导入模块: from pandas.core.dtypes import common [as 别名]
# 或者: from pandas.core.dtypes.common import is_scalar [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:
tm.skip_if_no_ne(engine)
ev = pd.eval(ex, engine=self.engine, parser=self.parser)
tm.assert_almost_equal(ev, result)
示例4: _construct
# 需要导入模块: from pandas.core.dtypes import common [as 别名]
# 或者: from pandas.core.dtypes.common import is_scalar [as 别名]
def _construct(self, shape, value=None, dtype=None, **kwargs):
""" construct an object for the given shape
if value is specified use that if its a scalar
if value is an array, repeat it as needed """
if isinstance(shape, int):
shape = tuple([shape] * self._ndim)
if value is not None:
if is_scalar(value):
if value == 'empty':
arr = None
# remove the info axis
kwargs.pop(self._typ._info_axis_name, None)
else:
arr = np.empty(shape, dtype=dtype)
arr.fill(value)
else:
fshape = np.prod(shape)
arr = value.ravel()
new_shape = fshape / arr.shape[0]
if fshape % arr.shape[0] != 0:
raise Exception("invalid value passed in _construct")
arr = np.repeat(arr, new_shape).reshape(shape)
else:
arr = np.random.randn(*shape)
return self._typ(arr, dtype=dtype, **kwargs)
示例5: check_complex_cmp_op
# 需要导入模块: from pandas.core.dtypes import common [as 别名]
# 或者: from pandas.core.dtypes.common import is_scalar [as 别名]
def check_complex_cmp_op(self, lhs, cmp1, rhs, binop, cmp2):
skip_these = _scalar_skip
ex = '(lhs {cmp1} rhs) {binop} (lhs {cmp2} rhs)'.format(cmp1=cmp1,
binop=binop,
cmp2=cmp2)
scalar_with_in_notin = (is_scalar(rhs) and (cmp1 in skip_these or
cmp2 in skip_these))
if scalar_with_in_notin:
with pytest.raises(TypeError):
pd.eval(ex, engine=self.engine, parser=self.parser)
with pytest.raises(TypeError):
pd.eval(ex, engine=self.engine, parser=self.parser,
local_dict={'lhs': lhs, 'rhs': rhs})
else:
lhs_new = _eval_single_bin(lhs, cmp1, rhs, self.engine)
rhs_new = _eval_single_bin(lhs, cmp2, rhs, self.engine)
if (isinstance(lhs_new, Series) and
isinstance(rhs_new, DataFrame) and
binop in _series_frame_incompatible):
pass
# TODO: the code below should be added back when left and right
# hand side bool ops are fixed.
#
# try:
# pytest.raises(Exception, pd.eval, ex,
# local_dict={'lhs': lhs, 'rhs': rhs},
# engine=self.engine, parser=self.parser)
# except AssertionError:
# import ipdb
#
# ipdb.set_trace()
# raise
else:
expected = _eval_single_bin(
lhs_new, binop, rhs_new, self.engine)
result = pd.eval(ex, engine=self.engine, parser=self.parser)
self.check_equal(result, expected)
示例6: test_identical
# 需要导入模块: from pandas.core.dtypes import common [as 别名]
# 或者: from pandas.core.dtypes.common import is_scalar [as 别名]
def test_identical(self):
# see gh-10546
x = 1
result = pd.eval('x', engine=self.engine, parser=self.parser)
assert result == 1
assert is_scalar(result)
x = 1.5
result = pd.eval('x', engine=self.engine, parser=self.parser)
assert result == 1.5
assert is_scalar(result)
x = False
result = pd.eval('x', engine=self.engine, parser=self.parser)
assert not result
assert is_bool(result)
assert is_scalar(result)
x = np.array([1])
result = pd.eval('x', engine=self.engine, parser=self.parser)
tm.assert_numpy_array_equal(result, np.array([1]))
assert result.shape == (1, )
x = np.array([1.5])
result = pd.eval('x', engine=self.engine, parser=self.parser)
tm.assert_numpy_array_equal(result, np.array([1.5]))
assert result.shape == (1, )
x = np.array([False]) # noqa
result = pd.eval('x', engine=self.engine, parser=self.parser)
tm.assert_numpy_array_equal(result, np.array([False]))
assert result.shape == (1, )
示例7: test_complex
# 需要导入模块: from pandas.core.dtypes import common [as 别名]
# 或者: from pandas.core.dtypes.common import is_scalar [as 别名]
def test_complex(self, value, expected):
result = isna(value)
if is_scalar(result):
assert result is expected
else:
tm.assert_numpy_array_equal(result, expected)
示例8: __init__
# 需要导入模块: from pandas.core.dtypes import common [as 别名]
# 或者: from pandas.core.dtypes.common import is_scalar [as 别名]
def __init__(self, data=None, index=None, sparse_index=None, kind='block',
fill_value=None, name=None, dtype=None, copy=False,
fastpath=False):
# TODO: Most of this should be refactored and shared with Series
# 1. BlockManager -> array
# 2. Series.index, Series.name, index, name reconciliation
# 3. Implicit reindexing
# 4. Implicit broadcasting
# 5. Dict construction
if data is None:
data = []
elif isinstance(data, SingleBlockManager):
index = data.index
data = data.blocks[0].values
elif isinstance(data, (ABCSeries, ABCSparseSeries)):
index = data.index if index is None else index
dtype = data.dtype if dtype is None else dtype
name = data.name if name is None else name
if index is not None:
data = data.reindex(index)
elif isinstance(data, compat.Mapping):
data, index = Series()._init_dict(data, index=index)
elif is_scalar(data) and index is not None:
data = np.full(len(index), fill_value=data)
super(SparseSeries, self).__init__(
SparseArray(data,
sparse_index=sparse_index,
kind=kind,
dtype=dtype,
fill_value=fill_value,
copy=copy),
index=index, name=name,
copy=False, fastpath=fastpath
)
示例9: _convert_by
# 需要导入模块: from pandas.core.dtypes import common [as 别名]
# 或者: from pandas.core.dtypes.common import is_scalar [as 别名]
def _convert_by(by):
if by is None:
by = []
elif (is_scalar(by) or
isinstance(by, (np.ndarray, Index, ABCSeries, Grouper)) or
hasattr(by, '__call__')):
by = [by]
else:
by = list(by)
return by
示例10: is_scalar
# 需要导入模块: from pandas.core.dtypes import common [as 别名]
# 或者: from pandas.core.dtypes.common import is_scalar [as 别名]
def is_scalar(self):
return is_scalar(self._value)
示例11: _disallow_scalar_only_bool_ops
# 需要导入模块: from pandas.core.dtypes import common [as 别名]
# 或者: from pandas.core.dtypes.common import is_scalar [as 别名]
def _disallow_scalar_only_bool_ops(self):
if ((self.lhs.is_scalar or self.rhs.is_scalar) and
self.op in _bool_ops_dict and
(not (issubclass(self.rhs.return_type, (bool, np.bool_)) and
issubclass(self.lhs.return_type, (bool, np.bool_))))):
raise NotImplementedError("cannot evaluate scalar only bool ops")
示例12: _gotitem
# 需要导入模块: from pandas.core.dtypes import common [as 别名]
# 或者: from pandas.core.dtypes.common import is_scalar [as 别名]
def _gotitem(self, key, ndim, subset=None):
"""
Sub-classes to define. Return a sliced object.
Parameters
----------
key : string / list of selections
ndim : 1,2
requested ndim of result
subset : object, default None
subset to act on
"""
# create a new object to prevent aliasing
if subset is None:
subset = self.obj
# we need to make a shallow copy of ourselves
# with the same groupby
kwargs = {attr: getattr(self, attr) for attr in self._attributes}
# Try to select from a DataFrame, falling back to a Series
try:
groupby = self._groupby[key]
except IndexError:
groupby = self._groupby
self = self.__class__(subset,
groupby=groupby,
parent=self,
**kwargs)
self._reset_cache()
if subset.ndim == 2:
if is_scalar(key) and key in subset or is_list_like(key):
self._selection = key
return self
# special case to prevent duplicate plots when catching exceptions when
# forwarding methods from NDFrames
示例13: _format_value
# 需要导入模块: from pandas.core.dtypes import common [as 别名]
# 或者: from pandas.core.dtypes.common import is_scalar [as 别名]
def _format_value(self, val):
if is_scalar(val) and missing.isna(val):
val = self.na_rep
elif is_float(val):
if missing.isposinf_scalar(val):
val = self.inf_rep
elif missing.isneginf_scalar(val):
val = '-{inf}'.format(inf=self.inf_rep)
elif self.float_format is not None:
val = float(self.float_format % val)
return val
示例14: convert_values
# 需要导入模块: from pandas.core.dtypes import common [as 别名]
# 或者: from pandas.core.dtypes.common import is_scalar [as 别名]
def convert_values(self):
"""Convert datetimes to a comparable value in an expression.
"""
def stringify(value):
if self.encoding is not None:
encoder = partial(pprint_thing_encoded,
encoding=self.encoding)
else:
encoder = pprint_thing
return encoder(value)
lhs, rhs = self.lhs, self.rhs
if is_term(lhs) and lhs.is_datetime and is_term(rhs) and rhs.is_scalar:
v = rhs.value
if isinstance(v, (int, float)):
v = stringify(v)
v = pd.Timestamp(_ensure_decoded(v))
if v.tz is not None:
v = v.tz_convert('UTC')
self.rhs.update(v)
if is_term(rhs) and rhs.is_datetime and is_term(lhs) and lhs.is_scalar:
v = lhs.value
if isinstance(v, (int, float)):
v = stringify(v)
v = pd.Timestamp(_ensure_decoded(v))
if v.tz is not None:
v = v.tz_convert('UTC')
self.lhs.update(v)