From 8868abfa777a68d0e9a011fd496774b29f90caae Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 13 Mar 2020 14:27:51 -0700 Subject: [PATCH 1/2] CLN: remove unused kwargs from BlockManager.downcast --- pandas/core/internals/blocks.py | 16 +++++++++------- pandas/core/internals/managers.py | 26 ++++++++++++++++++++------ pandas/tests/generic/test_generic.py | 17 ++--------------- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 6d4ee6222933c..d562d8dc08f23 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -403,7 +403,9 @@ def _split_op_result(self, result) -> List["Block"]: return [result] - def fillna(self, value, limit=None, inplace: bool = False, downcast=None): + def fillna( + self, value, limit=None, inplace: bool = False, downcast=None + ) -> List["Block"]: """ fillna on the block with the value. If we fail, then convert to ObjectBlock and try again @@ -417,9 +419,9 @@ def fillna(self, value, limit=None, inplace: bool = False, downcast=None): if not self._can_hold_na: if inplace: - return self + return [self] else: - return self.copy() + return [self.copy()] if self._can_hold_element(value): # equivalent: _try_coerce_args(value) would not raise @@ -428,7 +430,7 @@ def fillna(self, value, limit=None, inplace: bool = False, downcast=None): # we can't process the value, but nothing to do if not mask.any(): - return self if inplace else self.copy() + return [self] if inplace else [self.copy()] # operate column-by-column def f(mask, val, idx): @@ -442,7 +444,7 @@ def f(mask, val, idx): return self.split_and_operate(None, f, inplace) - def split_and_operate(self, mask, f, inplace: bool): + def split_and_operate(self, mask, f, inplace: bool) -> List["Block"]: """ split the block per-column, and apply the callable f per-column, return a new block for each. Handle @@ -1191,7 +1193,7 @@ def _interpolate_with_fill( fill_value=None, coerce=False, downcast=None, - ): + ) -> List["Block"]: """ fillna but using the interpolate machinery """ inplace = validate_bool_kwarg(inplace, "inplace") @@ -1233,7 +1235,7 @@ def _interpolate( inplace=False, downcast=None, **kwargs, - ): + ) -> List["Block"]: """ interpolate using scipy wrappers """ inplace = validate_bool_kwarg(inplace, "inplace") data = self.values if inplace else self.values.copy() diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 0bba8de6682ea..2a239746daac7 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -565,8 +565,8 @@ def isna(self, func) -> "BlockManager": def where(self, **kwargs) -> "BlockManager": return self.apply("where", **kwargs) - def setitem(self, **kwargs) -> "BlockManager": - return self.apply("setitem", **kwargs) + def setitem(self, indexer, value) -> "BlockManager": + return self.apply("setitem", indexer=indexer, value=value) def putmask(self, **kwargs): return self.apply("putmask", **kwargs) @@ -583,16 +583,30 @@ def shift(self, **kwargs) -> "BlockManager": def fillna(self, **kwargs) -> "BlockManager": return self.apply("fillna", **kwargs) - def downcast(self, **kwargs) -> "BlockManager": - return self.apply("downcast", **kwargs) + def downcast(self) -> "BlockManager": + return self.apply("downcast") def astype( self, dtype, copy: bool = False, errors: str = "raise" ) -> "BlockManager": return self.apply("astype", dtype=dtype, copy=copy, errors=errors) - def convert(self, **kwargs) -> "BlockManager": - return self.apply("convert", **kwargs) + def convert( + self, + copy: bool = True, + datetime: bool = True, + numeric: bool = True, + timedelta: bool = True, + coerce: bool = False, + ) -> "BlockManager": + return self.apply( + "convert", + copy=copy, + datetime=datetime, + numeric=numeric, + timedelta=timedelta, + coerce=coerce, + ) def replace(self, value, **kwargs) -> "BlockManager": assert np.ndim(value) == 0, value diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index 1b6cb8447c76d..9c759ff1414fe 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -159,27 +159,14 @@ def test_downcast(self): o = self._construct(shape=4, value=9, dtype=np.int64) result = o.copy() - result._data = o._data.downcast(dtypes="infer") + result._data = o._data.downcast() self._compare(result, o) - o = self._construct(shape=4, value=9.0) - expected = o.astype(np.int64) - result = o.copy() - result._data = o._data.downcast(dtypes="infer") - self._compare(result, expected) - o = self._construct(shape=4, value=9.5) result = o.copy() - result._data = o._data.downcast(dtypes="infer") + result._data = o._data.downcast() self._compare(result, o) - # are close - o = self._construct(shape=4, value=9.000000000005) - result = o.copy() - result._data = o._data.downcast(dtypes="infer") - expected = o.astype(np.int64) - self._compare(result, expected) - def test_constructor_compound_dtypes(self): # see gh-5191 # Compound dtypes should raise NotImplementedError. From 9fc781af5a92a38407ca26cc0115578448d3463a Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 13 Mar 2020 15:36:25 -0700 Subject: [PATCH 2/2] mypy fixup --- pandas/core/internals/blocks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index d562d8dc08f23..ea1f394aacd5f 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1243,7 +1243,7 @@ def _interpolate( # only deal with floats if not self.is_float: if not self.is_integer: - return self + return [self] data = data.astype(np.float64) if fill_value is None: