From 21e64880355462e908490d8cd5bbd807f0165819 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 26 Nov 2021 19:40:44 -0800 Subject: [PATCH] CLN/TST: address FIXMEs --- pandas/_testing/_warnings.py | 4 +-- pandas/core/arrays/interval.py | 10 +------ pandas/tests/base/test_unique.py | 14 +++------- pandas/tests/extension/base/constructors.py | 13 ++++------ pandas/tests/extension/test_datetime.py | 16 +++++------- pandas/tests/extension/test_sparse.py | 14 +++++----- pandas/tests/frame/indexing/test_indexing.py | 8 +----- pandas/tests/frame/methods/test_diff.py | 26 +++++++++---------- pandas/tests/frame/methods/test_transpose.py | 2 +- pandas/tests/frame/methods/test_truncate.py | 8 +++--- pandas/tests/frame/test_block_internals.py | 3 +-- .../indexes/categorical/test_category.py | 5 +--- pandas/tests/indexes/test_base.py | 5 ---- pandas/tests/io/pytables/test_append.py | 4 +-- pandas/tests/series/test_constructors.py | 6 ----- 15 files changed, 44 insertions(+), 94 deletions(-) diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py index 5f01996d0390d..f66614bd02a3f 100644 --- a/pandas/_testing/_warnings.py +++ b/pandas/_testing/_warnings.py @@ -151,8 +151,8 @@ def _assert_caught_no_extra_warnings( if actual_warning.category == ResourceWarning and unclosed in str( actual_warning.message ): - # FIXME: kludge because pytest.filterwarnings does not - # suppress these, xref GH#38630 + # FIXME(GH#38630): kludge because pytest.filterwarnings does not + # suppress these continue extra_warnings.append( diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 96a76aa930f86..e1347391b2bdd 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -1195,15 +1195,7 @@ def length(self): Return an Index with entries denoting the length of each Interval in the IntervalArray. """ - try: - return self.right - self.left - except TypeError as err: - # length not defined for some types, e.g. string - msg = ( - "IntervalArray contains Intervals without defined length, " - "e.g. Intervals with string endpoints" - ) - raise TypeError(msg) from err + return self.right - self.left @property def mid(self): diff --git a/pandas/tests/base/test_unique.py b/pandas/tests/base/test_unique.py index 95e07583bab66..31f2aebcba4ba 100644 --- a/pandas/tests/base/test_unique.py +++ b/pandas/tests/base/test_unique.py @@ -1,12 +1,7 @@ import numpy as np import pytest -from pandas._libs import iNaT - -from pandas.core.dtypes.common import ( - is_datetime64tz_dtype, - needs_i8_conversion, -) +from pandas.core.dtypes.common import is_datetime64tz_dtype import pandas as pd from pandas import NumericIndex @@ -49,11 +44,8 @@ def test_unique_null(null_obj, index_or_series_obj): elif isinstance(obj, pd.MultiIndex): pytest.skip(f"MultiIndex can't hold '{null_obj}'") - values = obj.values - if needs_i8_conversion(obj.dtype): - values[0:2] = iNaT - else: - values[0:2] = null_obj + values = obj._values + values[0:2] = null_obj klass = type(obj) repeated_values = np.repeat(values, range(1, len(values) + 1)) diff --git a/pandas/tests/extension/base/constructors.py b/pandas/tests/extension/base/constructors.py index 6e4ed7b77cad8..4ba315eeaeb15 100644 --- a/pandas/tests/extension/base/constructors.py +++ b/pandas/tests/extension/base/constructors.py @@ -3,10 +3,7 @@ import pandas as pd from pandas.api.extensions import ExtensionArray -from pandas.core.internals.blocks import ( - DatetimeTZBlock, - ExtensionBlock, -) +from pandas.core.internals.blocks import EABackedBlock from pandas.tests.extension.base.base import BaseExtensionTests @@ -29,14 +26,14 @@ def test_series_constructor(self, data): assert result.dtype == data.dtype assert len(result) == len(data) if hasattr(result._mgr, "blocks"): - assert isinstance(result._mgr.blocks[0], (ExtensionBlock, DatetimeTZBlock)) + assert isinstance(result._mgr.blocks[0], EABackedBlock) assert result._mgr.array is data # Series[EA] is unboxed / boxed correctly result2 = pd.Series(result) assert result2.dtype == data.dtype if hasattr(result._mgr, "blocks"): - assert isinstance(result2._mgr.blocks[0], (ExtensionBlock, DatetimeTZBlock)) + assert isinstance(result2._mgr.blocks[0], EABackedBlock) def test_series_constructor_no_data_with_index(self, dtype, na_value): result = pd.Series(index=[1, 2, 3], dtype=dtype) @@ -71,7 +68,7 @@ def test_dataframe_constructor_from_dict(self, data, from_series): assert result.dtypes["A"] == data.dtype assert result.shape == (len(data), 1) if hasattr(result._mgr, "blocks"): - assert isinstance(result._mgr.blocks[0], (ExtensionBlock, DatetimeTZBlock)) + assert isinstance(result._mgr.blocks[0], EABackedBlock) assert isinstance(result._mgr.arrays[0], ExtensionArray) def test_dataframe_from_series(self, data): @@ -79,7 +76,7 @@ def test_dataframe_from_series(self, data): assert result.dtypes[0] == data.dtype assert result.shape == (len(data), 1) if hasattr(result._mgr, "blocks"): - assert isinstance(result._mgr.blocks[0], (ExtensionBlock, DatetimeTZBlock)) + assert isinstance(result._mgr.blocks[0], EABackedBlock) assert isinstance(result._mgr.arrays[0], ExtensionArray) def test_series_given_mismatched_index_raises(self, data): diff --git a/pandas/tests/extension/test_datetime.py b/pandas/tests/extension/test_datetime.py index de5a6b7a5bb06..f13b24fabaf34 100644 --- a/pandas/tests/extension/test_datetime.py +++ b/pandas/tests/extension/test_datetime.py @@ -108,10 +108,6 @@ class TestGetitem(BaseDatetimeTests, base.BaseGetitemTests): class TestMethods(BaseDatetimeTests, base.BaseMethodsTests): - @pytest.mark.skip(reason="Incorrect expected") - def test_value_counts(self, all_data, dropna): - pass - def test_combine_add(self, data_repeated): # Timestamp.__add__(Timestamp) not defined pass @@ -140,23 +136,23 @@ def test_arith_frame_with_scalar(self, data, all_arithmetic_operators): def test_arith_series_with_scalar(self, data, all_arithmetic_operators): if all_arithmetic_operators in self.implements: - s = pd.Series(data) - self.check_opname(s, all_arithmetic_operators, s.iloc[0], exc=None) + ser = pd.Series(data) + self.check_opname(ser, all_arithmetic_operators, ser.iloc[0], exc=None) else: # ... but not the rest. super().test_arith_series_with_scalar(data, all_arithmetic_operators) def test_add_series_with_extension_array(self, data): # Datetime + Datetime not implemented - s = pd.Series(data) + ser = pd.Series(data) msg = "cannot add DatetimeArray and DatetimeArray" with pytest.raises(TypeError, match=msg): - s + data + ser + data def test_arith_series_with_array(self, data, all_arithmetic_operators): if all_arithmetic_operators in self.implements: - s = pd.Series(data) - self.check_opname(s, all_arithmetic_operators, s.iloc[0], exc=None) + ser = pd.Series(data) + self.check_opname(ser, all_arithmetic_operators, ser.iloc[0], exc=None) else: # ... but not the rest. super().test_arith_series_with_scalar(data, all_arithmetic_operators) diff --git a/pandas/tests/extension/test_sparse.py b/pandas/tests/extension/test_sparse.py index 012a3fbb12cac..1f22feff018ec 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -179,12 +179,12 @@ def test_transpose(self, data): class TestGetitem(BaseSparseTests, base.BaseGetitemTests): def test_get(self, data): - s = pd.Series(data, index=[2 * i for i in range(len(data))]) - if np.isnan(s.values.fill_value): - assert np.isnan(s.get(4)) and np.isnan(s.iloc[2]) + ser = pd.Series(data, index=[2 * i for i in range(len(data))]) + if np.isnan(ser.values.fill_value): + assert np.isnan(ser.get(4)) and np.isnan(ser.iloc[2]) else: - assert s.get(4) == s.iloc[2] - assert s.get(2) == s.iloc[1] + assert ser.get(4) == ser.iloc[2] + assert ser.get(2) == ser.iloc[1] def test_reindex(self, data, na_value): self._check_unsupported(data) @@ -454,8 +454,8 @@ def _compare_other(self, s, data, comparison_op, other): tm.assert_series_equal(result, expected) # series - s = pd.Series(data) - result = op(s, other) + ser = pd.Series(data) + result = op(ser, other) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index 942da38dc5a26..40e6500fce64b 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -800,7 +800,7 @@ def test_setitem_single_column_mixed_datetime(self): assert df["timestamp"].dtype == np.object_ assert df.loc["b", "timestamp"] == iNaT - # allow this syntax + # allow this syntax (as of GH#3216) df.loc["c", "timestamp"] = np.nan assert isna(df.loc["c", "timestamp"]) @@ -808,12 +808,6 @@ def test_setitem_single_column_mixed_datetime(self): df.loc["d", :] = np.nan assert not isna(df.loc["c", :]).all() - # FIXME: don't leave commented-out - # as of GH 3216 this will now work! - # try to set with a list like item - # pytest.raises( - # Exception, df.loc.__setitem__, ('d', 'timestamp'), [np.nan]) - def test_setitem_mixed_datetime(self): # GH 9336 expected = DataFrame( diff --git a/pandas/tests/frame/methods/test_diff.py b/pandas/tests/frame/methods/test_diff.py index fe227db894feb..f61529659e9d5 100644 --- a/pandas/tests/frame/methods/test_diff.py +++ b/pandas/tests/frame/methods/test_diff.py @@ -17,33 +17,31 @@ def test_diff_requires_integer(self): with pytest.raises(ValueError, match="periods must be an integer"): df.diff(1.5) - def test_diff_allows_np_integer(self): - # np.int64 is OK GH#44572 - df = DataFrame(np.random.randn(2, 2)) - res = df.diff(np.int64(1)) - expected = df.diff(1) - tm.assert_frame_equal(res, expected) - - def test_diff(self, datetime_frame): - the_diff = datetime_frame.diff(1) + # GH#44572 np.int64 is accepted + @pytest.mark.parametrize("num", [1, np.int64(1)]) + def test_diff(self, datetime_frame, num): + df = datetime_frame + the_diff = df.diff(num) - tm.assert_series_equal( - the_diff["A"], datetime_frame["A"] - datetime_frame["A"].shift(1) - ) + expected = df["A"] - df["A"].shift(num) + tm.assert_series_equal(the_diff["A"], expected) + def test_diff_int_dtype(self): # int dtype a = 10_000_000_000_000_000 b = a + 1 - s = Series([a, b]) + ser = Series([a, b]) - rs = DataFrame({"s": s}).diff() + rs = DataFrame({"s": ser}).diff() assert rs.s[1] == 1 + def test_diff_mixed_numeric(self, datetime_frame): # mixed numeric tf = datetime_frame.astype("float32") the_diff = tf.diff(1) tm.assert_series_equal(the_diff["A"], tf["A"] - tf["A"].shift(1)) + def test_diff_axis1_nonconsolidated(self): # GH#10907 df = DataFrame({"y": Series([2]), "z": Series([3])}) df.insert(0, "x", 1) diff --git a/pandas/tests/frame/methods/test_transpose.py b/pandas/tests/frame/methods/test_transpose.py index 59de1ab0c1ce9..7fca752f2a21e 100644 --- a/pandas/tests/frame/methods/test_transpose.py +++ b/pandas/tests/frame/methods/test_transpose.py @@ -115,4 +115,4 @@ def test_transpose_get_view_dt64tzget_view(self): assert result._mgr.nblocks == 1 rtrip = result._mgr.blocks[0].values - assert np.shares_memory(arr._data, rtrip._data) + assert np.shares_memory(arr._ndarray, rtrip._ndarray) diff --git a/pandas/tests/frame/methods/test_truncate.py b/pandas/tests/frame/methods/test_truncate.py index 54b87a330f67a..33ba29c65cebb 100644 --- a/pandas/tests/frame/methods/test_truncate.py +++ b/pandas/tests/frame/methods/test_truncate.py @@ -85,18 +85,16 @@ def test_truncate_nonsortedindex(self, frame_or_series): obj.truncate(before=3, after=9) def test_sort_values_nonsortedindex(self): - # TODO: belongs elsewhere? - rng = date_range("2011-01-01", "2012-01-01", freq="W") ts = DataFrame( {"A": np.random.randn(len(rng)), "B": np.random.randn(len(rng))}, index=rng ) + decreasing = ts.sort_values("A", ascending=False) + msg = "truncate requires a sorted index" with pytest.raises(ValueError, match=msg): - ts.sort_values("A", ascending=False).truncate( - before="2011-11", after="2011-12" - ) + decreasing.truncate(before="2011-11", after="2011-12") def test_truncate_nonsortedindex_axis1(self): # GH#17935 diff --git a/pandas/tests/frame/test_block_internals.py b/pandas/tests/frame/test_block_internals.py index 2b8eff036d60a..01a8982c5fe16 100644 --- a/pandas/tests/frame/test_block_internals.py +++ b/pandas/tests/frame/test_block_internals.py @@ -352,8 +352,7 @@ def test_stale_cached_series_bug_473(self): assert pd.isna(Y["g"]["c"]) def test_strange_column_corruption_issue(self): - # FIXME: dont leave commented-out - # (wesm) Unclear how exactly this is related to internal matters + # TODO(wesm): Unclear how exactly this is related to internal matters df = DataFrame(index=[0, 1]) df[0] = np.nan wasCol = {} diff --git a/pandas/tests/indexes/categorical/test_category.py b/pandas/tests/indexes/categorical/test_category.py index 6a9f7c2a80922..2ae6ce99b4ee8 100644 --- a/pandas/tests/indexes/categorical/test_category.py +++ b/pandas/tests/indexes/categorical/test_category.py @@ -264,13 +264,10 @@ def test_ensure_copied_data(self, index): # # Must be tested separately from other indexes because # self.values is not an ndarray. - # GH#29918 Index.base has been removed - # FIXME: is this test still meaningful? - _base = lambda ar: ar if getattr(ar, "base", None) is None else ar.base result = CategoricalIndex(index.values, copy=True) tm.assert_index_equal(index, result) - assert _base(index.values) is not _base(result.values) + assert not np.shares_memory(result._data._codes, index._data._codes) result = CategoricalIndex(index.values, copy=False) assert result._data._codes is index._data._codes diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 59ec66ecc1fe9..5df1f83028133 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -82,11 +82,6 @@ def test_constructor_copy(self, index): arr[0] = "SOMEBIGLONGSTRING" assert new_index[0] != "SOMEBIGLONGSTRING" - # FIXME: dont leave commented-out - # what to do here? - # arr = np.array(5.) - # pytest.raises(Exception, arr.view, Index) - @pytest.mark.parametrize("cast_as_obj", [True, False]) @pytest.mark.parametrize( "index", diff --git a/pandas/tests/io/pytables/test_append.py b/pandas/tests/io/pytables/test_append.py index 5845bdc5df437..6cfe8148ad034 100644 --- a/pandas/tests/io/pytables/test_append.py +++ b/pandas/tests/io/pytables/test_append.py @@ -896,7 +896,6 @@ def test_append_to_multiple_dropna(setup_path): tm.assert_index_equal(store.select("df1").index, store.select("df2").index) -@pytest.mark.xfail(reason="append_to_multiple_dropna_false is not raising as failed") def test_append_to_multiple_dropna_false(setup_path): df1 = tm.makeTimeDataFrame() df2 = tm.makeTimeDataFrame().rename(columns="{}_2".format) @@ -910,8 +909,7 @@ def test_append_to_multiple_dropna_false(setup_path): {"df1a": ["A", "B"], "df2a": None}, df, selector="df1a", dropna=False ) - # TODO Update error message to desired message for this case - msg = "Cannot select as multiple after appending with dropna=False" + msg = "all tables must have exactly the same nrows!" with pytest.raises(ValueError, match=msg): store.select_as_multiple(["df1a", "df2a"]) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 692c040a33ff8..8023713dfcf39 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1397,12 +1397,6 @@ def test_constructor_dtype_timedelta64(self): td = Series([np.timedelta64(1, "s")]) assert td.dtype == "timedelta64[ns]" - # FIXME: dont leave commented-out - # these are frequency conversion astypes - # for t in ['s', 'D', 'us', 'ms']: - # with pytest.raises(TypeError): - # td.astype('m8[%s]' % t) - # valid astype with tm.assert_produces_warning(FutureWarning): # astype(int64) deprecated