Skip to content

Commit

Permalink
fixup! Move .unstack() logic onto BlockManager and Block
Browse files Browse the repository at this point in the history
  • Loading branch information
kernc committed Jul 12, 2017
1 parent e5a6ce9 commit 45cf34d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 2 additions & 0 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ def maybe_to_categorical(array):
""" coerce to a categorical if a series is given """
if isinstance(array, (ABCSeries, ABCCategoricalIndex)):
return array._values
elif isinstance(array, np.ndarray):
return Categorical(array)
return array


Expand Down
9 changes: 4 additions & 5 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4082,20 +4082,19 @@ def unstack(self, unstacker):
new_columns = dummy.get_new_columns()
new_index = dummy.get_new_index()
new_blocks = []
mask_blocks = np.zeros_like(new_columns, dtype=bool)
mask_columns = np.zeros_like(new_columns, dtype=bool)

for blk in self.blocks:
bunstacker = unstacker(
blk.values.T, value_columns=self.items[blk.mgr_locs.indexer])
new_items = bunstacker.get_new_columns()
new_values, mask = bunstacker.get_new_values()
new_placement = new_columns.get_indexer(new_items)
mask_blocks[new_placement] = mask.any(0)
mask_columns[new_placement] = mask.any(0)
new_blocks.extend(blk._unstack(new_values.T, new_placement))

new_blocks = [b for b, keep in zip(new_blocks, mask_blocks) if keep]
new_axes = [new_columns[mask_blocks], new_index]
bm = BlockManager(new_blocks, new_axes)
bm = BlockManager(new_blocks, [new_columns, new_index])
bm = bm.take(mask_columns.nonzero()[0], axis=0)
return bm


Expand Down

0 comments on commit 45cf34d

Please sign in to comment.