本文整理汇总了Python中pandas.api.types.is_scalar方法的典型用法代码示例。如果您正苦于以下问题:Python types.is_scalar方法的具体用法?Python types.is_scalar怎么用?Python types.is_scalar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.api.types
的用法示例。
在下文中一共展示了types.is_scalar方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _parse_tuple
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_scalar [as 别名]
def _parse_tuple(tup):
"""Unpack the user input for getitem and setitem and compute ndim
loc[a] -> ([a], :), 1D
loc[[a,b],] -> ([a,b], :),
loc[a,b] -> ([a], [b]), 0D
"""
row_loc, col_loc = slice(None), slice(None)
if is_tuple(tup):
row_loc = tup[0]
if len(tup) == 2:
col_loc = tup[1]
if len(tup) > 2:
raise IndexingError("Too many indexers")
else:
row_loc = tup
ndim = _compute_ndim(row_loc, col_loc)
row_scaler = is_scalar(row_loc)
col_scaler = is_scalar(col_loc)
row_loc = [row_loc] if row_scaler else row_loc
col_loc = [col_loc] if col_scaler else col_loc
return row_loc, col_loc, ndim, row_scaler, col_scaler
示例2: test_iloc_setitem_with_scalar_index
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_scalar [as 别名]
def test_iloc_setitem_with_scalar_index(self, indexer, value):
# GH #19474
# assigning like "df.iloc[0, [0]] = ['Z']" should be evaluated
# elementwisely, not using "setter('A', ['Z'])".
df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])
df.iloc[0, indexer] = value
result = df.iloc[0, 0]
assert is_scalar(result) and result == 'Z'
示例3: test_loc_setitem_with_scalar_index
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_scalar [as 别名]
def test_loc_setitem_with_scalar_index(self, indexer, value):
# GH #19474
# assigning like "df.loc[0, ['A']] = ['Z']" should be evaluated
# elementwisely, not using "setter('A', ['Z'])".
df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])
df.loc[0, indexer] = value
result = df.loc[0, 'A']
assert is_scalar(result) and result == 'Z'
示例4: test_searchsorted_numeric_dtypes_scalar
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_scalar [as 别名]
def test_searchsorted_numeric_dtypes_scalar(self):
s = Series([1, 2, 90, 1000, 3e9])
r = s.searchsorted(30)
assert is_scalar(r)
assert r == 2
r = s.searchsorted([30])
e = np.array([2], dtype=np.intp)
tm.assert_numpy_array_equal(r, e)
示例5: test_search_sorted_datetime64_scalar
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_scalar [as 别名]
def test_search_sorted_datetime64_scalar(self):
s = Series(pd.date_range('20120101', periods=10, freq='2D'))
v = pd.Timestamp('20120102')
r = s.searchsorted(v)
assert is_scalar(r)
assert r == 1
示例6: __call__
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_scalar [as 别名]
def __call__(self, arg):
if is_scalar(arg):
ret = pd.to_datetime(arg, errors=self._errors, dayfirst=self._dayfirst,
yearfirst=self._yearfirst, utc=self._utc,
format=self._format, exact=self._exact,
unit=self._unit, infer_datetime_format=self._infer_datetime_format,
origin=self._origin, cache=self._cache)
return astensor(ret)
dtype = np.datetime64(1, 'ns').dtype
if isinstance(arg, (pd.Series, SERIES_TYPE)):
arg = asseries(arg)
return self.new_series([arg], shape=arg.shape,
dtype=dtype, index_value=arg.index_value,
name=arg.name)
if is_dict_like(arg) or isinstance(arg, DATAFRAME_TYPE):
arg = asdataframe(arg)
columns = arg.columns_value.to_pandas().tolist()
if sorted(columns) != sorted(['year', 'month', 'day']):
missing = ','.join(c for c in ['day', 'month', 'year'] if c not in columns)
raise ValueError('to assemble mappings requires at least '
'that [year, month, day] be specified: [{}] is missing'.format(missing))
return self.new_series([arg], shape=(arg.shape[0],),
dtype=dtype, index_value=arg.index_value)
elif isinstance(arg, (pd.Index, INDEX_TYPE)):
arg = asindex(arg)
return self.new_index([arg], shape=arg.shape,
dtype=dtype,
index_value=parse_index(pd.Index([], dtype=dtype),
self._params, arg),
name=arg.name)
else:
arg = astensor(arg)
if arg.ndim != 1:
raise TypeError('arg must be a string, datetime, '
'list, tuple, 1-d tensor, or Series')
return self.new_index([arg], shape=arg.shape,
dtype=dtype,
index_value=parse_index(pd.Index([], dtype=dtype),
self._params, arg))
示例7: set_axis
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_scalar [as 别名]
def set_axis(self, labels, axis=0, inplace=False):
"""Assign desired index to given axis.
Args:
labels (pandas.Index or list-like): The Index to assign.
axis (string or int): The axis to reassign.
inplace (bool): Whether to make these modifications inplace.
Returns:
If inplace is False, returns a new DataFrame, otherwise None.
"""
if is_scalar(labels):
warnings.warn(
'set_axis now takes "labels" as first argument, and '
'"axis" as named parameter. The old form, with "axis" as '
'first parameter and "labels" as second, is still supported '
"but will be deprecated in a future version of pandas.",
FutureWarning,
stacklevel=2,
)
labels, axis = axis, labels
if inplace:
setattr(self, pandas.DataFrame()._get_axis_name(axis), labels)
else:
obj = self.copy()
obj.set_axis(labels, axis=axis, inplace=True)
return obj
示例8: _compute_ndim
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_scalar [as 别名]
def _compute_ndim(row_loc, col_loc):
"""Compute the ndim of result from locators
"""
row_scaler = is_scalar(row_loc) or is_tuple(row_loc)
col_scaler = is_scalar(col_loc) or is_tuple(col_loc)
if row_scaler and col_scaler:
ndim = 0
elif row_scaler ^ col_scaler:
ndim = 1
else:
ndim = 2
return ndim
示例9: _check_op_integer
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_scalar [as 别名]
def _check_op_integer(self, result, expected, mask, s, op_name, other):
# check comparisions that are resulting in integer dtypes
# to compare properly, we convert the expected
# to float, mask to nans and convert infs
# if we have uints then we process as uints
# then conert to float
# and we ultimately want to create a IntArray
# for comparisons
fill_value = 0
# mod/rmod turn floating 0 into NaN while
# integer works as expected (no nan)
if op_name in ['__mod__', '__rmod__']:
if is_scalar(other):
if other == 0:
expected[s.values == 0] = 0
else:
expected = expected.fillna(0)
else:
expected[(s.values == 0) &
((expected == 0) | expected.isna())] = 0
try:
expected[(expected == np.inf) | (expected == -np.inf)] = fill_value
original = expected
expected = expected.astype(s.dtype)
except ValueError:
expected = expected.astype(float)
expected[(expected == np.inf) | (expected == -np.inf)] = fill_value
original = expected
expected = expected.astype(s.dtype)
expected[mask] = np.nan
# assert that the expected astype is ok
# (skip for unsigned as they have wrap around)
if not s.dtype.is_unsigned_integer:
original = pd.Series(original)
# we need to fill with 0's to emulate what an astype('int') does
# (truncation) for certain ops
if op_name in ['__rtruediv__', '__rdiv__']:
mask |= original.isna()
original = original.fillna(0).astype('int')
original = original.astype('float')
original[mask] = np.nan
tm.assert_series_equal(original, expected.astype('float'))
# assert our expected result
tm.assert_series_equal(result, expected)
示例10: test_searchsorted
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_scalar [as 别名]
def test_searchsorted(self):
# https://github.com/pandas-dev/pandas/issues/8420
# https://github.com/pandas-dev/pandas/issues/14522
c1 = Categorical(['cheese', 'milk', 'apple', 'bread', 'bread'],
categories=['cheese', 'milk', 'apple', 'bread'],
ordered=True)
s1 = Series(c1)
c2 = Categorical(['cheese', 'milk', 'apple', 'bread', 'bread'],
categories=['cheese', 'milk', 'apple', 'bread'],
ordered=False)
s2 = Series(c2)
# Searching for single item argument, side='left' (default)
res_cat = c1.searchsorted('apple')
assert res_cat == 2
assert is_scalar(res_cat)
res_ser = s1.searchsorted('apple')
assert res_ser == 2
assert is_scalar(res_ser)
# Searching for single item array, side='left' (default)
res_cat = c1.searchsorted(['bread'])
res_ser = s1.searchsorted(['bread'])
exp = np.array([3], dtype=np.intp)
tm.assert_numpy_array_equal(res_cat, exp)
tm.assert_numpy_array_equal(res_ser, exp)
# Searching for several items array, side='right'
res_cat = c1.searchsorted(['apple', 'bread'], side='right')
res_ser = s1.searchsorted(['apple', 'bread'], side='right')
exp = np.array([3, 5], dtype=np.intp)
tm.assert_numpy_array_equal(res_cat, exp)
tm.assert_numpy_array_equal(res_ser, exp)
# Searching for a single value that is not from the Categorical
pytest.raises(KeyError, lambda: c1.searchsorted('cucumber'))
pytest.raises(KeyError, lambda: s1.searchsorted('cucumber'))
# Searching for multiple values one of each is not from the Categorical
pytest.raises(KeyError,
lambda: c1.searchsorted(['bread', 'cucumber']))
pytest.raises(KeyError,
lambda: s1.searchsorted(['bread', 'cucumber']))
# searchsorted call for unordered Categorical
pytest.raises(ValueError, lambda: c2.searchsorted('apple'))
pytest.raises(ValueError, lambda: s2.searchsorted('apple'))
示例11: test_searchsorted_monotonic
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_scalar [as 别名]
def test_searchsorted_monotonic(indices):
# GH17271
# not implemented for tuple searches in MultiIndex
# or Intervals searches in IntervalIndex
if isinstance(indices, (MultiIndex, IntervalIndex)):
return
# nothing to test if the index is empty
if indices.empty:
return
value = indices[0]
# determine the expected results (handle dupes for 'right')
expected_left, expected_right = 0, (indices == value).argmin()
if expected_right == 0:
# all values are the same, expected_right should be length
expected_right = len(indices)
# test _searchsorted_monotonic in all cases
# test searchsorted only for increasing
if indices.is_monotonic_increasing:
ssm_left = indices._searchsorted_monotonic(value, side='left')
assert is_scalar(ssm_left)
assert expected_left == ssm_left
ssm_right = indices._searchsorted_monotonic(value, side='right')
assert is_scalar(ssm_right)
assert expected_right == ssm_right
ss_left = indices.searchsorted(value, side='left')
assert is_scalar(ss_left)
assert expected_left == ss_left
ss_right = indices.searchsorted(value, side='right')
assert is_scalar(ss_right)
assert expected_right == ss_right
elif indices.is_monotonic_decreasing:
ssm_left = indices._searchsorted_monotonic(value, side='left')
assert is_scalar(ssm_left)
assert expected_left == ssm_left
ssm_right = indices._searchsorted_monotonic(value, side='right')
assert is_scalar(ssm_right)
assert expected_right == ssm_right
else:
# non-monotonic should raise.
with pytest.raises(ValueError):
indices._searchsorted_monotonic(value, side='left')