Skip to content

Commit 25ade6d

Browse files
authored
TST: tighten testing assertions (pandas-dev#38223)
1 parent dffab1f commit 25ade6d

File tree

5 files changed

+21
-33
lines changed

5 files changed

+21
-33
lines changed

pandas/tests/groupby/test_grouping.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,8 @@ def test_groupby_levels_and_columns(self):
307307
# reset_index changes columns dtype to object
308308
by_columns = df.reset_index().groupby(idx_names).mean()
309309

310-
tm.assert_frame_equal(by_levels, by_columns, check_column_type=False)
311-
312-
by_columns.columns = Index(by_columns.columns, dtype=np.int64)
310+
# without casting, by_columns.columns is object-dtype
311+
by_columns.columns = by_columns.columns.astype(np.int64)
313312
tm.assert_frame_equal(by_levels, by_columns)
314313

315314
def test_groupby_categorical_index_and_columns(self, observed):

pandas/tests/indexing/test_loc.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -1782,21 +1782,23 @@ def test_series_getitem_label_list_missing_integer_values():
17821782

17831783

17841784
@pytest.mark.parametrize(
1785-
"columns, column_key, expected_columns, check_column_type",
1785+
"columns, column_key, expected_columns",
17861786
[
1787-
([2011, 2012, 2013], [2011, 2012], [0, 1], True),
1788-
([2011, 2012, "All"], [2011, 2012], [0, 1], False),
1789-
([2011, 2012, "All"], [2011, "All"], [0, 2], True),
1787+
([2011, 2012, 2013], [2011, 2012], [0, 1]),
1788+
([2011, 2012, "All"], [2011, 2012], [0, 1]),
1789+
([2011, 2012, "All"], [2011, "All"], [0, 2]),
17901790
],
17911791
)
1792-
def test_loc_getitem_label_list_integer_labels(
1793-
columns, column_key, expected_columns, check_column_type
1794-
):
1792+
def test_loc_getitem_label_list_integer_labels(columns, column_key, expected_columns):
17951793
# gh-14836
17961794
df = DataFrame(np.random.rand(3, 3), columns=columns, index=list("ABC"))
17971795
expected = df.iloc[:, expected_columns]
17981796
result = df.loc[["A", "B", "C"], column_key]
1799-
tm.assert_frame_equal(result, expected, check_column_type=check_column_type)
1797+
1798+
if df.columns.is_object() and all(isinstance(x, int) for x in column_key):
1799+
expected.columns = expected.columns.astype(int)
1800+
1801+
tm.assert_frame_equal(result, expected, check_column_type=True)
18001802

18011803

18021804
def test_loc_setitem_float_intindex():

pandas/tests/io/excel/test_writers.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -472,12 +472,12 @@ def test_int_types(self, np_type, path):
472472

473473
# Test with convert_float=False comes back as float.
474474
float_frame = df.astype(float)
475+
float_frame.columns = float_frame.columns.astype(float)
476+
float_frame.index = float_frame.index.astype(float)
475477
recons = pd.read_excel(
476478
path, sheet_name="test1", convert_float=False, index_col=0
477479
)
478-
tm.assert_frame_equal(
479-
recons, float_frame, check_index_type=False, check_column_type=False
480-
)
480+
tm.assert_frame_equal(recons, float_frame)
481481

482482
@pytest.mark.parametrize("np_type", [np.float16, np.float32, np.float64])
483483
def test_float_types(self, np_type, path):

pandas/tests/io/json/test_pandas.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -727,9 +727,7 @@ def test_series_with_dtype_datetime(self, dtype, expected):
727727
def test_frame_from_json_precise_float(self):
728728
df = DataFrame([[4.56, 4.56, 4.56], [4.56, 4.56, 4.56]])
729729
result = read_json(df.to_json(), precise_float=True)
730-
tm.assert_frame_equal(
731-
result, df, check_index_type=False, check_column_type=False
732-
)
730+
tm.assert_frame_equal(result, df)
733731

734732
def test_typ(self):
735733

pandas/tests/io/pytables/test_store.py

+5-16
Original file line numberDiff line numberDiff line change
@@ -1631,16 +1631,12 @@ def check_col(key, name, size):
16311631
& (df_new.A > 0)
16321632
& (df_new.B < 0)
16331633
]
1634-
tm.assert_frame_equal(
1635-
result, expected, check_index_type=False, check_freq=False
1636-
)
1634+
tm.assert_frame_equal(result, expected)
16371635

16381636
# yield an empty frame
16391637
result = store.select("df", "string='foo' and string2='cool'")
16401638
expected = df_new[(df_new.string == "foo") & (df_new.string2 == "cool")]
1641-
tm.assert_frame_equal(
1642-
result, expected, check_index_type=False, check_freq=False
1643-
)
1639+
tm.assert_frame_equal(result, expected)
16441640

16451641
with ensure_clean_store(setup_path) as store:
16461642
# doc example
@@ -1660,16 +1656,11 @@ def check_col(key, name, size):
16601656
result = store.select("df_dc", "B>0")
16611657

16621658
expected = df_dc[df_dc.B > 0]
1663-
tm.assert_frame_equal(
1664-
result, expected, check_index_type=False, check_freq=False
1665-
)
1659+
tm.assert_frame_equal(result, expected)
16661660

16671661
result = store.select("df_dc", ["B > 0", "C > 0", "string == foo"])
16681662
expected = df_dc[(df_dc.B > 0) & (df_dc.C > 0) & (df_dc.string == "foo")]
1669-
tm.assert_frame_equal(
1670-
result, expected, check_index_type=False, check_freq=False
1671-
)
1672-
# FIXME: 2020-05-07 freq check randomly fails in the CI
1663+
tm.assert_frame_equal(result, expected)
16731664

16741665
with ensure_clean_store(setup_path) as store:
16751666
# doc example part 2
@@ -2374,9 +2365,7 @@ def test_series(self, setup_path):
23742365
ts3 = Series(
23752366
ts.values, Index(np.asarray(ts.index, dtype=object), dtype=object)
23762367
)
2377-
self._check_roundtrip(
2378-
ts3, tm.assert_series_equal, path=setup_path, check_index_type=False
2379-
)
2368+
self._check_roundtrip(ts3, tm.assert_series_equal, path=setup_path)
23802369

23812370
def test_float_index(self, setup_path):
23822371

0 commit comments

Comments
 (0)