From 793462914010b06d2982af301885a9350282982a Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 10 Oct 2020 09:51:16 -0700 Subject: [PATCH] CLN: collected cleanups, warning suppression in tests (#37021) --- pandas/core/indexes/base.py | 4 +--- pandas/core/internals/blocks.py | 8 +++----- pandas/core/internals/concat.py | 4 ++-- pandas/io/pytables.py | 9 +++++++-- pandas/tests/extension/test_external_block.py | 9 --------- .../tests/frame/apply/test_frame_transform.py | 20 +++++++++++-------- pandas/tests/frame/test_api.py | 4 +++- pandas/tests/indexes/common.py | 2 -- pandas/tests/io/__init__.py | 17 ++++++++++++++++ pandas/tests/io/excel/__init__.py | 14 +++++++++---- pandas/tests/io/excel/test_readers.py | 2 -- pandas/tests/io/test_common.py | 5 ----- 12 files changed, 55 insertions(+), 43 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 31a81c544a5b3..e185976be9df0 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5404,9 +5404,7 @@ def _cmp_method(self, other, op): with np.errstate(all="ignore"): result = ops.comparison_op(self._values, np.asarray(other), op) - if is_bool_dtype(result): - return result - return ops.invalid_comparison(self, other, op) + return result @classmethod def _add_numeric_methods_binary(cls): diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 3a4bdd54ad717..c9869500469f4 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -501,9 +501,7 @@ def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"] # no need to downcast our float # unless indicated - if downcast is None and ( - self.is_float or self.is_timedelta or self.is_datetime - ): + if downcast is None and (self.is_float or self.is_datelike): return blocks return extend_blocks([b.downcast(downcast) for b in blocks]) @@ -638,7 +636,7 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise"): if isinstance(values, np.ndarray): values = values.reshape(self.shape) - newb = make_block(values, placement=self.mgr_locs, ndim=self.ndim) + newb = self.make_block(values) if newb.is_numeric and self.is_numeric: if newb.shape != self.shape: @@ -2484,7 +2482,7 @@ def f(mask, val, idx): blocks = self.split_and_operate(None, f, False) else: values = f(None, self.values.ravel(), None) - blocks = [make_block(values, ndim=self.ndim, placement=self.mgr_locs)] + blocks = [self.make_block(values)] return blocks diff --git a/pandas/core/internals/concat.py b/pandas/core/internals/concat.py index 7ad058cfeb83c..8d54f88558066 100644 --- a/pandas/core/internals/concat.py +++ b/pandas/core/internals/concat.py @@ -187,7 +187,7 @@ def __repr__(self) -> str: return f"{type(self).__name__}({repr(self.block)}, {self.indexers})" @cache_readonly - def needs_filling(self): + def needs_filling(self) -> bool: for indexer in self.indexers.values(): # FIXME: cache results of indexer == -1 checks. if (indexer == -1).any(): @@ -206,7 +206,7 @@ def dtype(self): return get_dtype(maybe_promote(self.block.dtype, self.block.fill_value)[0]) @cache_readonly - def is_na(self): + def is_na(self) -> bool: if self.block is None: return True diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 3e3330fa4378f..2903ede1d5c0b 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -4728,8 +4728,13 @@ def _set_tz( assert values.tz is None or values.tz == tz if tz is not None: - name = getattr(values, "name", None) - values = values.ravel() + if isinstance(values, DatetimeIndex): + name = values.name + values = values.asi8 + else: + name = None + values = values.ravel() + tz = _ensure_decoded(tz) values = DatetimeIndex(values, name=name) values = values.tz_localize("UTC").tz_convert(tz) diff --git a/pandas/tests/extension/test_external_block.py b/pandas/tests/extension/test_external_block.py index 1843126898f3d..e98545daaf049 100644 --- a/pandas/tests/extension/test_external_block.py +++ b/pandas/tests/extension/test_external_block.py @@ -11,15 +11,6 @@ class CustomBlock(ExtensionBlock): _holder = np.ndarray _can_hold_na = False - def concat_same_type(self, to_concat, placement=None): - """ - Always concatenate disregarding self.ndim as the values are - always 1D in this custom Block - """ - values = np.concatenate([blk.values for blk in to_concat]) - placement = self.mgr_locs if self.ndim == 2 else slice(len(values)) - return self.make_block_same_class(values, placement=placement) - @pytest.fixture def df(): diff --git a/pandas/tests/frame/apply/test_frame_transform.py b/pandas/tests/frame/apply/test_frame_transform.py index 01c6fd4ec08f0..1b259ddbd41dc 100644 --- a/pandas/tests/frame/apply/test_frame_transform.py +++ b/pandas/tests/frame/apply/test_frame_transform.py @@ -168,14 +168,18 @@ def test_transform_bad_dtype(op): if op in ("backfill", "shift", "pad", "bfill", "ffill"): pytest.xfail("Transform function works on any datatype") msg = "Transform function failed" - with pytest.raises(ValueError, match=msg): - df.transform(op) - with pytest.raises(ValueError, match=msg): - df.transform([op]) - with pytest.raises(ValueError, match=msg): - df.transform({"A": op}) - with pytest.raises(ValueError, match=msg): - df.transform({"A": [op]}) + + # tshift is deprecated + warn = None if op != "tshift" else FutureWarning + with tm.assert_produces_warning(warn, check_stacklevel=False): + with pytest.raises(ValueError, match=msg): + df.transform(op) + with pytest.raises(ValueError, match=msg): + df.transform([op]) + with pytest.raises(ValueError, match=msg): + df.transform({"A": op}) + with pytest.raises(ValueError, match=msg): + df.transform({"A": [op]}) @pytest.mark.parametrize("op", transformation_kernels) diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index 8b5d0c7ade56c..f5d1808f367e7 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -605,7 +605,9 @@ def test_constructor_expanddim_lookup(self): # raise NotImplementedError df = DataFrame() - inspect.getmembers(df) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + # _AXIS_NUMBERS, _AXIS_NAMES lookups + inspect.getmembers(df) with pytest.raises(NotImplementedError, match="Not supported for DataFrames!"): df._constructor_expanddim(np.arange(27).reshape(3, 3, 3)) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 73d2e99d3ff5e..bc178c138341f 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -613,8 +613,6 @@ def test_equals(self, index): def test_equals_op(self): # GH9947, GH10637 index_a = self.create_index() - if isinstance(index_a, PeriodIndex): - pytest.skip("Skip check for PeriodIndex") n = len(index_a) index_b = index_a[0:-1] diff --git a/pandas/tests/io/__init__.py b/pandas/tests/io/__init__.py index e69de29bb2d1d..c5e867f45b92d 100644 --- a/pandas/tests/io/__init__.py +++ b/pandas/tests/io/__init__.py @@ -0,0 +1,17 @@ +import pytest + +pytestmark = [ + # fastparquet + pytest.mark.filterwarnings( + "ignore:PY_SSIZE_T_CLEAN will be required.*:DeprecationWarning" + ), + # xlrd + pytest.mark.filterwarnings( + "ignore:This method will be removed in future versions:DeprecationWarning" + ), + pytest.mark.filterwarnings( + "ignore:This method will be removed in future versions. " + r"Use 'tree.iter\(\)' or 'list\(tree.iter\(\)\)' instead." + ":PendingDeprecationWarning" + ), +] diff --git a/pandas/tests/io/excel/__init__.py b/pandas/tests/io/excel/__init__.py index 550172329fc57..419761cbe1d6d 100644 --- a/pandas/tests/io/excel/__init__.py +++ b/pandas/tests/io/excel/__init__.py @@ -1,6 +1,12 @@ import pytest -pytestmark = pytest.mark.filterwarnings( - # Looks like tree.getiterator is deprecated in favor of tree.iter - "ignore:This method will be removed in future versions:PendingDeprecationWarning" -) +pytestmark = [ + pytest.mark.filterwarnings( + # Looks like tree.getiterator is deprecated in favor of tree.iter + "ignore:This method will be removed in future versions:" + "PendingDeprecationWarning" + ), + pytest.mark.filterwarnings( + "ignore:This method will be removed in future versions:DeprecationWarning" + ), +] diff --git a/pandas/tests/io/excel/test_readers.py b/pandas/tests/io/excel/test_readers.py index 4bdcc5b327fa7..800b4c79b9c09 100644 --- a/pandas/tests/io/excel/test_readers.py +++ b/pandas/tests/io/excel/test_readers.py @@ -635,8 +635,6 @@ def test_read_from_s3_url(self, read_ext, s3_resource, s3so): tm.assert_frame_equal(url_table, local_table) @pytest.mark.slow - # ignore warning from old xlrd - @pytest.mark.filterwarnings("ignore:This metho:PendingDeprecationWarning") def test_read_from_file_url(self, read_ext, datapath): # FILE diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index ede8d61490778..2a6f3d1ad9380 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -245,11 +245,6 @@ def test_read_expands_user_home_dir( ), ], ) - @pytest.mark.filterwarnings( - "ignore:This method will be removed in future versions. " - r"Use 'tree.iter\(\)' or 'list\(tree.iter\(\)\)' instead." - ":PendingDeprecationWarning" - ) def test_read_fspath_all(self, reader, module, path, datapath): pytest.importorskip(module) path = datapath(*path)