本文整理汇总了Python中pandas.core.internals.BlockManager方法的典型用法代码示例。如果您正苦于以下问题:Python internals.BlockManager方法的具体用法?Python internals.BlockManager怎么用?Python internals.BlockManager使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.core.internals
的用法示例。
在下文中一共展示了internals.BlockManager方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_duplicate_ref_loc_failure
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [as 别名]
def test_duplicate_ref_loc_failure(self):
tmp_mgr = create_mgr('a:bool; a: f8')
axes, blocks = tmp_mgr.axes, tmp_mgr.blocks
blocks[0].mgr_locs = np.array([0])
blocks[1].mgr_locs = np.array([0])
# test trying to create block manager with overlapping ref locs
with pytest.raises(AssertionError):
BlockManager(blocks, axes)
blocks[0].mgr_locs = np.array([0])
blocks[1].mgr_locs = np.array([1])
mgr = BlockManager(blocks, axes)
mgr.iget(1)
示例2: test_equals_block_order_different_dtypes
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [as 别名]
def test_equals_block_order_different_dtypes(self):
# GH 9330
mgr_strings = [
"a:i8;b:f8", # basic case
"a:i8;b:f8;c:c8;d:b", # many types
"a:i8;e:dt;f:td;g:string", # more types
"a:i8;b:category;c:category2;d:category2", # categories
"c:sparse;d:sparse_na;b:f8", # sparse
]
for mgr_string in mgr_strings:
bm = create_mgr(mgr_string)
block_perms = itertools.permutations(bm.blocks)
for bm_perm in block_perms:
bm_this = BlockManager(bm_perm, bm.axes)
assert bm.equals(bm_this)
assert bm_this.equals(bm)
示例3: _extend_blocks
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [as 别名]
def _extend_blocks(result, blocks=None):
""" return a new extended blocks, givin the result """
from pandas.core.internals import BlockManager
if blocks is None:
blocks = []
if isinstance(result, list):
for r in result:
if isinstance(r, list):
blocks.extend(r)
else:
blocks.append(r)
elif isinstance(result, BlockManager):
blocks.extend(result.blocks)
else:
blocks.append(result)
return blocks
示例4: _init_mgr
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [as 别名]
def _init_mgr(self, mgr, axes=None, dtype=None, copy=False):
""" passed a manager and a axes dict """
for a, axe in axes.items():
if axe is not None:
mgr = mgr.reindex_axis(
axe, axis=self._get_block_manager_axis(a), copy=False)
# do not copy BlockManager unless explicitly done
if copy and dtype is None:
mgr = mgr.copy()
elif dtype is not None:
# avoid copy if we can
if len(mgr.blocks) > 1 or mgr.blocks[0].values.dtype != dtype:
mgr = mgr.astype(dtype)
return mgr
#----------------------------------------------------------------------
# Construction
示例5: _wrap_agged_blocks
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [as 别名]
def _wrap_agged_blocks(self, blocks):
obj = self._obj_with_exclusions
new_axes = list(obj._data.axes)
# more kludge
if self.axis == 0:
new_axes[0], new_axes[1] = new_axes[1], self.grouper.result_index
else:
new_axes[self.axis] = self.grouper.result_index
mgr = BlockManager(blocks, new_axes)
new_obj = type(obj)(mgr)
return self._post_process_cython_aggregate(new_obj)
示例6: read
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [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))
示例7: df
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [as 别名]
def df():
df1 = pd.DataFrame({'a': [1, 2, 3]})
blocks = df1._data.blocks
values = np.arange(3, dtype='int64')
custom_block = CustomBlock(values, placement=slice(1, 2))
blocks = blocks + (custom_block,)
block_manager = BlockManager(blocks, [pd.Index(['a', 'b']), df1.index])
return pd.DataFrame(block_manager)
示例8: test_custom_repr
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [as 别名]
def test_custom_repr():
values = np.arange(3, dtype='int64')
# series
block = CustomBlock(values, placement=slice(0, 3))
s = pd.Series(SingleBlockManager(block, pd.RangeIndex(3)))
assert repr(s) == '0 Val: 0\n1 Val: 1\n2 Val: 2\ndtype: int64'
# dataframe
block = CustomBlock(values, placement=slice(0, 1))
blk_mgr = BlockManager([block], [['col'], range(3)])
df = pd.DataFrame(blk_mgr)
assert repr(df) == ' col\n0 Val: 0\n1 Val: 1\n2 Val: 2'
示例9: test_get
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [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])
示例10: test_equals
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [as 别名]
def test_equals(self):
# unique items
bm1 = create_mgr('a,b,c: i8-1; d,e,f: i8-2')
bm2 = BlockManager(bm1.blocks[::-1], bm1.axes)
assert bm1.equals(bm2)
bm1 = create_mgr('a,a,a: i8-1; b,b,b: i8-2')
bm2 = BlockManager(bm1.blocks[::-1], bm1.axes)
assert bm1.equals(bm2)
示例11: _unstack
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [as 别名]
def _unstack(self, unstacker_func, new_columns, n_rows, fill_value):
"""Return a list of unstacked blocks of self
Parameters
----------
unstacker_func : callable
Partially applied unstacker.
new_columns : Index
All columns of the unstacked BlockManager.
n_rows : int
Only used in ExtensionBlock.unstack
fill_value : int
Only used in ExtensionBlock.unstack
Returns
-------
blocks : list of Block
New blocks of unstacked values.
mask : array_like of bool
The mask of columns of `blocks` we should keep.
"""
unstacker = unstacker_func(self.values.T)
new_items = unstacker.get_new_columns()
new_placement = new_columns.get_indexer(new_items)
new_values, mask = unstacker.get_new_values()
mask = mask.any(0)
new_values = new_values.T[mask]
new_placement = new_placement[mask]
blocks = [make_block(new_values, placement=new_placement)]
return blocks, mask
示例12: _get_unstack_items
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [as 别名]
def _get_unstack_items(self, unstacker, new_columns):
"""
Get the placement, values, and mask for a Block unstack.
This is shared between ObjectBlock and ExtensionBlock. They
differ in that ObjectBlock passes the values, while ExtensionBlock
passes the dummy ndarray of positions to be used by a take
later.
Parameters
----------
unstacker : pandas.core.reshape.reshape._Unstacker
new_columns : Index
All columns of the unstacked BlockManager.
Returns
-------
new_placement : ndarray[int]
The placement of the new columns in `new_columns`.
new_values : Union[ndarray, ExtensionArray]
The first return value from _Unstacker.get_new_values.
mask : ndarray[bool]
The second return value from _Unstacker.get_new_values.
"""
# shared with ExtensionBlock
new_items = unstacker.get_new_columns()
new_placement = new_columns.get_indexer(new_items)
new_values, mask = unstacker.get_new_values()
mask = mask.any(0)
return new_placement, new_values, mask
示例13: dict_to_manager
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [as 别名]
def dict_to_manager(sdict, columns, index):
""" create and return the block manager from a dict of series, columns, index """
# from BlockManager perspective
axes = [_ensure_index(columns), _ensure_index(index)]
return create_block_manager_from_arrays([sdict[c] for c in columns], columns, axes)
示例14: _from_axes
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [as 别名]
def _from_axes(cls, data, axes):
# for construction from BlockManager
if isinstance(data, BlockManager):
return cls(data)
else:
if cls._AXIS_REVERSED:
axes = axes[::-1]
d = cls._construct_axes_dict_from(cls, axes, copy=False)
return cls(data, **d)
示例15: as_blocks
# 需要导入模块: from pandas.core import internals [as 别名]
# 或者: from pandas.core.internals import BlockManager [as 别名]
def as_blocks(self, columns=None):
"""
Convert the frame to a dict of dtype -> Constructor Types that each has
a homogeneous dtype.
are presented in sorted order unless a specific list of columns is
provided.
NOTE: the dtypes of the blocks WILL BE PRESERVED HERE (unlike in
as_matrix)
Parameters
----------
columns : array-like
Specific column order
Returns
-------
values : a list of Object
"""
self._consolidate_inplace()
bd = dict()
for b in self._data.blocks:
b = b.reindex_items_from(columns or b.items)
bd[str(b.dtype)] = self._constructor(
BlockManager([b], [b.items, self.index])).__finalize__(self)
return bd