|
8 | 8 | import numpy as np |
9 | 9 |
|
10 | 10 | from pandas._libs import Timedelta, Timestamp, internals as libinternals, lib |
11 | | -from pandas._typing import DtypeObj |
| 11 | +from pandas._typing import DtypeObj, Label |
12 | 12 | from pandas.util._validators import validate_bool_kwarg |
13 | 13 |
|
14 | 14 | from pandas.core.dtypes.cast import ( |
@@ -1001,7 +1001,25 @@ def delete(self, item): |
1001 | 1001 | ) |
1002 | 1002 | self._rebuild_blknos_and_blklocs() |
1003 | 1003 |
|
1004 | | - def set(self, item, value): |
| 1004 | + def set(self, item: Label, value): |
| 1005 | + """ |
| 1006 | + Set new item in-place. |
| 1007 | +
|
| 1008 | + Notes |
| 1009 | + ----- |
| 1010 | + Does not consolidate. |
| 1011 | + Adds new Block if not contained in the current items Index. |
| 1012 | + """ |
| 1013 | + try: |
| 1014 | + loc = self.items.get_loc(item) |
| 1015 | + except KeyError: |
| 1016 | + # This item wasn't present, just insert at end |
| 1017 | + self.insert(len(self.items), item, value) |
| 1018 | + return |
| 1019 | + |
| 1020 | + self.iset(loc, value) |
| 1021 | + |
| 1022 | + def iset(self, loc: Union[int, slice, np.ndarray], value): |
1005 | 1023 | """ |
1006 | 1024 | Set new item in-place. Does not consolidate. Adds new Block if not |
1007 | 1025 | contained in the current set of items |
@@ -1034,13 +1052,6 @@ def value_getitem(placement): |
1034 | 1052 | "Shape of new values must be compatible with manager shape" |
1035 | 1053 | ) |
1036 | 1054 |
|
1037 | | - try: |
1038 | | - loc = self.items.get_loc(item) |
1039 | | - except KeyError: |
1040 | | - # This item wasn't present, just insert at end |
1041 | | - self.insert(len(self.items), item, value) |
1042 | | - return |
1043 | | - |
1044 | 1055 | if isinstance(loc, int): |
1045 | 1056 | loc = [loc] |
1046 | 1057 |
|
@@ -1086,7 +1097,7 @@ def value_getitem(placement): |
1086 | 1097 | unfit_mgr_locs = np.concatenate(unfit_mgr_locs) |
1087 | 1098 | unfit_count = len(unfit_mgr_locs) |
1088 | 1099 |
|
1089 | | - new_blocks = [] |
| 1100 | + new_blocks: List[Block] = [] |
1090 | 1101 | if value_is_extension_type: |
1091 | 1102 | # This code (ab-)uses the fact that sparse blocks contain only |
1092 | 1103 | # one item. |
@@ -1145,6 +1156,9 @@ def insert(self, loc: int, item, value, allow_duplicates: bool = False): |
1145 | 1156 | # insert to the axis; this could possibly raise a TypeError |
1146 | 1157 | new_axis = self.items.insert(loc, item) |
1147 | 1158 |
|
| 1159 | + if value.ndim == self.ndim - 1 and not is_extension_array_dtype(value): |
| 1160 | + value = _safe_reshape(value, (1,) + value.shape) |
| 1161 | + |
1148 | 1162 | block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1)) |
1149 | 1163 |
|
1150 | 1164 | for blkno, count in _fast_count_smallints(self._blknos[loc:]): |
|
0 commit comments