|
11 | 11 | from pandas.core.common import (_possibly_downcast_to_dtype, isnull,
|
12 | 12 | _NS_DTYPE, _TD_DTYPE, ABCSeries, is_list_like,
|
13 | 13 | ABCSparseSeries, _infer_dtype_from_scalar,
|
14 |
| - _is_null_datelike_scalar, |
| 14 | + _is_null_datelike_scalar, _maybe_promote, |
15 | 15 | is_timedelta64_dtype, is_datetime64_dtype,
|
16 | 16 | _possibly_infer_to_datetimelike, array_equivalent)
|
17 | 17 | from pandas.core.index import Index, MultiIndex, _ensure_index
|
@@ -177,6 +177,24 @@ def _slice(self, slicer):
|
177 | 177 | """ return a slice of my values """
|
178 | 178 | return self.values[slicer]
|
179 | 179 |
|
| 180 | + def reshape_nd(self, labels, shape, ref_items): |
| 181 | + """ |
| 182 | + Parameters |
| 183 | + ---------- |
| 184 | + labels : list of new axis labels |
| 185 | + shape : new shape |
| 186 | + ref_items : new ref_items |
| 187 | +
|
| 188 | + return a new block that is transformed to a nd block |
| 189 | + """ |
| 190 | + |
| 191 | + return _block2d_to_blocknd( |
| 192 | + values=self.get_values().T, |
| 193 | + placement=self.mgr_locs, |
| 194 | + shape=shape, |
| 195 | + labels=labels, |
| 196 | + ref_items=ref_items) |
| 197 | + |
180 | 198 | def getitem_block(self, slicer, new_mgr_locs=None):
|
181 | 199 | """
|
182 | 200 | Perform __getitem__-like, return result as block.
|
@@ -2573,6 +2591,10 @@ def comp(s):
|
2573 | 2591 | bm._consolidate_inplace()
|
2574 | 2592 | return bm
|
2575 | 2593 |
|
| 2594 | + def reshape_nd(self, axes, **kwargs): |
| 2595 | + """ a 2d-nd reshape operation on a BlockManager """ |
| 2596 | + return self.apply('reshape_nd', axes=axes, **kwargs) |
| 2597 | + |
2576 | 2598 | def is_consolidated(self):
|
2577 | 2599 | """
|
2578 | 2600 | Return True if more than one block with the same dtype
|
@@ -3895,6 +3917,43 @@ def _concat_indexes(indexes):
|
3895 | 3917 | return indexes[0].append(indexes[1:])
|
3896 | 3918 |
|
3897 | 3919 |
|
| 3920 | +def _block2d_to_blocknd(values, placement, shape, labels, ref_items): |
| 3921 | + """ pivot to the labels shape """ |
| 3922 | + from pandas.core.internals import make_block |
| 3923 | + |
| 3924 | + panel_shape = (len(placement),) + shape |
| 3925 | + |
| 3926 | + # TODO: lexsort depth needs to be 2!! |
| 3927 | + |
| 3928 | + # Create observation selection vector using major and minor |
| 3929 | + # labels, for converting to panel format. |
| 3930 | + selector = _factor_indexer(shape[1:], labels) |
| 3931 | + mask = np.zeros(np.prod(shape), dtype=bool) |
| 3932 | + mask.put(selector, True) |
| 3933 | + |
| 3934 | + if mask.all(): |
| 3935 | + pvalues = np.empty(panel_shape, dtype=values.dtype) |
| 3936 | + else: |
| 3937 | + dtype, fill_value = _maybe_promote(values.dtype) |
| 3938 | + pvalues = np.empty(panel_shape, dtype=dtype) |
| 3939 | + pvalues.fill(fill_value) |
| 3940 | + |
| 3941 | + values = values |
| 3942 | + for i in range(len(placement)): |
| 3943 | + pvalues[i].flat[mask] = values[:, i] |
| 3944 | + |
| 3945 | + return make_block(pvalues, placement=placement) |
| 3946 | + |
| 3947 | + |
| 3948 | +def _factor_indexer(shape, labels): |
| 3949 | + """ |
| 3950 | + given a tuple of shape and a list of Categorical labels, return the |
| 3951 | + expanded label indexer |
| 3952 | + """ |
| 3953 | + mult = np.array(shape)[::-1].cumprod()[::-1] |
| 3954 | + return com._ensure_platform_int( |
| 3955 | + np.sum(np.array(labels).T * np.append(mult, [1]), axis=1).T) |
| 3956 | + |
3898 | 3957 | def _get_blkno_placements(blknos, blk_count, group=True):
|
3899 | 3958 | """
|
3900 | 3959 |
|
|
0 commit comments