From d3f02db032403d3b3565a8b8cc35652749724f04 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 28 Feb 2020 20:02:52 -0800 Subject: [PATCH 1/3] Implement BlockManager.iset --- pandas/core/internals/managers.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index e397167e4881f..9808b59fd0719 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -8,7 +8,7 @@ import numpy as np from pandas._libs import Timedelta, Timestamp, internals as libinternals, lib -from pandas._typing import DtypeObj +from pandas._typing import DtypeObj, Label from pandas.util._validators import validate_bool_kwarg from pandas.core.dtypes.cast import ( @@ -995,7 +995,21 @@ def delete(self, item): self._shape = None self._rebuild_blknos_and_blklocs() - def set(self, item, value): + def set(self, item: Label, value): + """ + Set new item in-place. Does not consolidate. Adds new Block if not + contained in the current set of items + """ + try: + loc = self.items.get_loc(item) + except KeyError: + # This item wasn't present, just insert at end + self.insert(len(self.items), item, value) + return + + self.iset(loc, value) + + def iset(self, loc: Union[int, slice, np.ndarray], value): """ Set new item in-place. Does not consolidate. Adds new Block if not contained in the current set of items @@ -1028,13 +1042,6 @@ def value_getitem(placement): "Shape of new values must be compatible with manager shape" ) - try: - loc = self.items.get_loc(item) - except KeyError: - # This item wasn't present, just insert at end - self.insert(len(self.items), item, value) - return - if isinstance(loc, int): loc = [loc] @@ -1139,6 +1146,9 @@ def insert(self, loc: int, item, value, allow_duplicates: bool = False): # insert to the axis; this could possibly raise a TypeError new_axis = self.items.insert(loc, item) + if value.ndim == self.ndim - 1 and not is_extension_array_dtype(value): + value = _safe_reshape(value, (1,) + value.shape) + block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1)) for blkno, count in _fast_count_smallints(self._blknos[loc:]): From e052681d83782f83ef07ded098490397a136e8aa Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 29 Feb 2020 10:28:26 -0800 Subject: [PATCH 2/3] mypy fixup --- pandas/core/internals/managers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 9808b59fd0719..d0fd570bd8f0a 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1087,7 +1087,7 @@ def value_getitem(placement): unfit_mgr_locs = np.concatenate(unfit_mgr_locs) unfit_count = len(unfit_mgr_locs) - new_blocks = [] + new_blocks: List[Block] = [] if value_is_extension_type: # This code (ab-)uses the fact that sparse blocks contain only # one item. From b042110aac023ec590b8eb7fdf4be44714d84dbb Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 2 Mar 2020 17:49:36 -0800 Subject: [PATCH 3/3] pep257 fixup --- pandas/core/internals/managers.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index ad4faed3f2283..bb3254446bd3b 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -998,8 +998,12 @@ def delete(self, item): def set(self, item: Label, value): """ - Set new item in-place. Does not consolidate. Adds new Block if not - contained in the current set of items + Set new item in-place. + + Notes + ----- + Does not consolidate. + Adds new Block if not contained in the current items Index. """ try: loc = self.items.get_loc(item)