本文整理汇总了Python中pandas.core.internals.make_block方法的典型用法代码示例。如果您正苦于以下问题:Python internals.make_block方法的具体用法?Python internals.make_block怎么用?Python internals.make_block使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.core.internals
的用法示例。
在下文中一共展示了internals.make_block方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: count
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import make_block [as 别名]
def count(self):
""" Compute count of group, excluding missing values """
from pandas.core.dtypes.missing import _isna_ndarraylike as _isna
data, _ = self._get_data_to_aggregate()
ids, _, ngroups = self.grouper.group_info
mask = ids != -1
val = ((mask & ~_isna(np.atleast_2d(blk.get_values())))
for blk in data.blocks)
loc = (blk.mgr_locs for blk in data.blocks)
counter = partial(
lib.count_level_2d, labels=ids, max_bin=ngroups, axis=1)
blk = map(make_block, map(counter, val), loc)
return self._wrap_agged_blocks(data.items, list(blk))
示例2: count
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import make_block [as 别名]
def count(self):
""" Compute count of group, excluding missing values """
from pandas.core.dtypes.missing import _isna_ndarraylike as isna
data, _ = self._get_data_to_aggregate()
ids, _, ngroups = self.grouper.group_info
mask = ids != -1
val = ((mask & ~isna(np.atleast_2d(blk.get_values())))
for blk in data.blocks)
loc = (blk.mgr_locs for blk in data.blocks)
counter = partial(count_level_2d, labels=ids, max_bin=ngroups, axis=1)
blk = map(make_block, map(counter, val), loc)
return self._wrap_agged_blocks(data.items, list(blk))
示例3: read
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import make_block [as 别名]
def read(self, **kwargs):
self.validate_read(kwargs)
axes = []
for i in range(self.ndim):
ax = self.read_index('axis%d' % i)
axes.append(ax)
items = axes[0]
blocks = []
for i in range(self.nblocks):
blk_items = self.read_index('block%d_items' % i)
values = self.read_array('block%d_values' % i)
blk = make_block(values, blk_items, items)
blocks.append(blk)
return self.obj_type(BlockManager(blocks, axes))
示例4: count
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import make_block [as 别名]
def count(self):
""" Compute count of group, excluding missing values """
from functools import partial
from pandas.core.dtypes.missing import _isna_ndarraylike as isna
data, _ = self._get_data_to_aggregate()
ids, _, ngroups = self.grouper.group_info
mask = ids != -1
val = ((mask & ~isna(np.atleast_2d(blk.get_values())))
for blk in data.blocks)
loc = (blk.mgr_locs for blk in data.blocks)
counter = partial(count_level_2d, labels=ids, max_bin=ngroups, axis=1)
blk = map(make_block, map(counter, val), loc)
return self._wrap_agged_blocks(data.items, list(blk))
示例5: count
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import make_block [as 别名]
def count(self):
""" Compute count of group, excluding missing values """
from functools import partial
from pandas.core.dtypes.missing import _isna_ndarraylike as isna
data, _ = self._get_data_to_aggregate()
ids, _, ngroups = self.grouper.group_info
mask = ids != -1
val = ((mask & ~isna(blk.get_values())) for blk in data.blocks)
loc = (blk.mgr_locs for blk in data.blocks)
counter = partial(count_level_2d, labels=ids, max_bin=ngroups, axis=1)
blk = map(make_block, map(counter, val), loc)
return self._wrap_agged_blocks(data.items, list(blk))
示例6: test_merge
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import make_block [as 别名]
def test_merge(self):
avals = randn(2, 10)
bvals = randn(2, 10)
ref_cols = Index(['e', 'a', 'b', 'd', 'f'])
ablock = make_block(avals, ref_cols.get_indexer(['e', 'b']))
bblock = make_block(bvals, ref_cols.get_indexer(['a', 'd']))
merged = ablock.merge(bblock)
tm.assert_numpy_array_equal(merged.mgr_locs.as_array,
np.array([0, 1, 2, 3], dtype=np.int64))
tm.assert_numpy_array_equal(merged.values[[0, 2]], np.array(avals))
tm.assert_numpy_array_equal(merged.values[[1, 3]], np.array(bvals))
# TODO: merge with mixed type?
示例7: test_get
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import make_block [as 别名]
def test_get(self):
cols = Index(list('abc'))
values = np.random.rand(3, 3)
block = make_block(values=values.copy(), placement=np.arange(3))
mgr = BlockManager(blocks=[block], axes=[cols, np.arange(3)])
assert_almost_equal(mgr.get('a', fastpath=False), values[0])
assert_almost_equal(mgr.get('b', fastpath=False), values[1])
assert_almost_equal(mgr.get('c', fastpath=False), values[2])
assert_almost_equal(mgr.get('a').internal_values(), values[0])
assert_almost_equal(mgr.get('b').internal_values(), values[1])
assert_almost_equal(mgr.get('c').internal_values(), values[2])
示例8: test_deprecated_fastpath
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import make_block [as 别名]
def test_deprecated_fastpath():
# GH#19265
values = np.random.rand(3, 3)
with tm.assert_produces_warning(DeprecationWarning,
check_stacklevel=False):
make_block(values, placement=np.arange(3), fastpath=True)
示例9: test_validate_ndim
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import make_block [as 别名]
def test_validate_ndim():
values = np.array([1.0, 2.0])
placement = slice(2)
msg = r"Wrong number of dimensions. values.ndim != ndim \[1 != 2\]"
with pytest.raises(ValueError, match=msg):
make_block(values, placement, ndim=2)
示例10: _unstack_frame
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import make_block [as 别名]
def _unstack_frame(obj, level):
from pandas.core.internals import BlockManager, make_block
if obj._is_mixed_type:
unstacker = _Unstacker(np.empty(obj.shape, dtype=bool), # dummy
obj.index, level=level,
value_columns=obj.columns)
new_columns = unstacker.get_new_columns()
new_index = unstacker.get_new_index()
new_axes = [new_columns, new_index]
new_blocks = []
mask_blocks = []
for blk in obj._data.blocks:
bunstacker = _Unstacker(blk.values.T, obj.index, level=level,
value_columns=blk.items)
new_items = bunstacker.get_new_columns()
new_values, mask = bunstacker.get_new_values()
mblk = make_block(mask.T, new_items, new_columns)
mask_blocks.append(mblk)
newb = make_block(new_values.T, new_items, new_columns)
new_blocks.append(newb)
result = DataFrame(BlockManager(new_blocks, new_axes))
mask_frame = DataFrame(BlockManager(mask_blocks, new_axes))
return result.ix[:, mask_frame.sum(0) > 0]
else:
unstacker = _Unstacker(obj.values, obj.index, level=level,
value_columns=obj.columns)
return unstacker.get_result()
示例11: block2d_to_blocknd
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import make_block [as 别名]
def block2d_to_blocknd(values, items, shape, labels, ref_items=None):
""" pivot to the labels shape """
from pandas.core.internals import make_block
panel_shape = (len(items),) + shape
# TODO: lexsort depth needs to be 2!!
# Create observation selection vector using major and minor
# labels, for converting to panel format.
selector = factor_indexer(shape[1:], labels)
mask = np.zeros(np.prod(shape), dtype=bool)
mask.put(selector, True)
if mask.all():
pvalues = np.empty(panel_shape, dtype=values.dtype)
else:
dtype, fill_value = _maybe_promote(values.dtype)
pvalues = np.empty(panel_shape, dtype=dtype)
pvalues.fill(fill_value)
values = values
for i in range(len(items)):
pvalues[i].flat[mask] = values[:, i]
if ref_items is None:
ref_items = items
return make_block(pvalues, items, ref_items)
示例12: _cython_agg_blocks
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import make_block [as 别名]
def _cython_agg_blocks(self, how, numeric_only=True):
data, agg_axis = self._get_data_to_aggregate()
new_blocks = []
for block in data.blocks:
values = block.values
is_numeric = is_numeric_dtype(values.dtype)
if numeric_only and not is_numeric:
continue
if is_numeric:
values = com.ensure_float(values)
result, _ = self.grouper.aggregate(values, how, axis=agg_axis)
# see if we can cast the block back to the original dtype
result = block._try_cast_result(result)
newb = make_block(result, block.items, block.ref_items)
new_blocks.append(newb)
if len(new_blocks) == 0:
raise DataError('No numeric types to aggregate')
return new_blocks