Skip to content

Commit 2f689ff

Browse files
authored
CLN: __repr__ tests (#55798)
* misplaced tests * CLN: repr smoke tests * misplaced testS * parametrize smoke tests * CLN: smoke test repr * misplaced tests
1 parent d1b2c44 commit 2f689ff

30 files changed

+120
-214
lines changed

pandas/tests/frame/indexing/test_getitem.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ def test_getitem_periodindex(self):
4242
ts = df[rng[0]]
4343
tm.assert_series_equal(ts, df.iloc[:, 0])
4444

45-
# GH#1211; smoketest unrelated to the rest of this test
46-
repr(df)
47-
4845
ts = df["1/1/2000"]
4946
tm.assert_series_equal(ts, df.iloc[:, 0])
5047

@@ -372,8 +369,6 @@ def test_getitem_boolean_series_with_duplicate_columns(self, df_dup_cols):
372369
result = df[df.C > 6]
373370

374371
tm.assert_frame_equal(result, expected)
375-
result.dtypes
376-
str(result)
377372

378373
def test_getitem_boolean_frame_with_duplicate_columns(self, df_dup_cols):
379374
# where
@@ -388,8 +383,6 @@ def test_getitem_boolean_frame_with_duplicate_columns(self, df_dup_cols):
388383
result = df[df > 6]
389384

390385
tm.assert_frame_equal(result, expected)
391-
result.dtypes
392-
str(result)
393386

394387
def test_getitem_empty_frame_with_boolean(self):
395388
# Test for issue GH#11859

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,6 @@ def test_setitem_None(self, float_frame):
511511
float_frame.loc[:, None], float_frame["A"], check_names=False
512512
)
513513
tm.assert_series_equal(float_frame[None], float_frame["A"], check_names=False)
514-
repr(float_frame)
515514

516515
def test_loc_setitem_boolean_mask_allfalse(self):
517516
# GH 9596

pandas/tests/frame/indexing/test_insert.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,12 @@ def test_insert_column_bug_4032(self):
5151
df.insert(0, "a", [1, 2])
5252
result = df.rename(columns={})
5353

54-
str(result)
5554
expected = DataFrame([[1, 1.1], [2, 2.2]], columns=["a", "b"])
5655
tm.assert_frame_equal(result, expected)
5756

5857
df.insert(0, "c", [1.3, 2.3])
5958
result = df.rename(columns={})
6059

61-
str(result)
6260
expected = DataFrame([[1.3, 1, 1.1], [2.3, 2, 2.2]], columns=["c", "a", "b"])
6361
tm.assert_frame_equal(result, expected)
6462

pandas/tests/frame/indexing/test_setitem.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -868,8 +868,6 @@ def test_setitem_with_expansion_categorical_dtype(self):
868868

869869
# setting with a Categorical
870870
df["D"] = cat
871-
str(df)
872-
873871
result = df.dtypes
874872
expected = Series(
875873
[np.dtype("int32"), CategoricalDtype(categories=labels, ordered=False)],
@@ -879,8 +877,6 @@ def test_setitem_with_expansion_categorical_dtype(self):
879877

880878
# setting with a Series
881879
df["E"] = ser
882-
str(df)
883-
884880
result = df.dtypes
885881
expected = Series(
886882
[

pandas/tests/frame/methods/test_rename.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,6 @@ def test_rename_with_duplicate_columns(self):
387387
# TODO: can we construct this without merge?
388388
k = merge(df4, df5, how="inner", left_index=True, right_index=True)
389389
result = k.rename(columns={"TClose_x": "TClose", "TClose_y": "QT_Close"})
390-
str(result)
391-
result.dtypes
392390

393391
expected = DataFrame(
394392
[[0.0454, 22.02, 0.0422, 20130331, 600809, "饡驦", 30.01]],

pandas/tests/frame/test_arithmetic.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,6 @@ def test_arithmetic_with_duplicate_columns(self, op):
689689
df.columns = ["A", "A"]
690690
result = getattr(df, op)(df)
691691
tm.assert_frame_equal(result, expected)
692-
str(result)
693-
result.dtypes
694692

695693
@pytest.mark.parametrize("level", [0, None])
696694
def test_broadcast_multiindex(self, level):

pandas/tests/frame/test_nonunique_indexes.py

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@
1010
import pandas._testing as tm
1111

1212

13-
def check(result, expected=None):
14-
if expected is not None:
15-
tm.assert_frame_equal(result, expected)
16-
result.dtypes
17-
str(result)
18-
19-
2013
class TestDataFrameNonuniqueIndexes:
2114
def test_setattr_columns_vs_construct_with_columns(self):
2215
# assignment
@@ -26,7 +19,7 @@ def test_setattr_columns_vs_construct_with_columns(self):
2619
df = DataFrame(arr, columns=["A", "A"])
2720
df.columns = idx
2821
expected = DataFrame(arr, columns=idx)
29-
check(df, expected)
22+
tm.assert_frame_equal(df, expected)
3023

3124
def test_setattr_columns_vs_construct_with_columns_datetimeindx(self):
3225
idx = date_range("20130101", periods=4, freq="QE-NOV")
@@ -35,7 +28,7 @@ def test_setattr_columns_vs_construct_with_columns_datetimeindx(self):
3528
)
3629
df.columns = idx
3730
expected = DataFrame([[1, 1, 1, 5], [1, 1, 2, 5], [2, 1, 3, 5]], columns=idx)
38-
check(df, expected)
31+
tm.assert_frame_equal(df, expected)
3932

4033
def test_insert_with_duplicate_columns(self):
4134
# insert
@@ -48,7 +41,7 @@ def test_insert_with_duplicate_columns(self):
4841
[[1, 1, 1, 5, "bah"], [1, 1, 2, 5, "bah"], [2, 1, 3, 5, "bah"]],
4942
columns=["foo", "bar", "foo", "hello", "string"],
5043
)
51-
check(df, expected)
44+
tm.assert_frame_equal(df, expected)
5245
with pytest.raises(ValueError, match="Length of value"):
5346
df.insert(0, "AnotherColumn", range(len(df.index) - 1))
5447

@@ -58,15 +51,15 @@ def test_insert_with_duplicate_columns(self):
5851
[[1, 1, 1, 5, "bah", 3], [1, 1, 2, 5, "bah", 3], [2, 1, 3, 5, "bah", 3]],
5952
columns=["foo", "bar", "foo", "hello", "string", "foo2"],
6053
)
61-
check(df, expected)
54+
tm.assert_frame_equal(df, expected)
6255

6356
# set (non-dup)
6457
df["foo2"] = 4
6558
expected = DataFrame(
6659
[[1, 1, 1, 5, "bah", 4], [1, 1, 2, 5, "bah", 4], [2, 1, 3, 5, "bah", 4]],
6760
columns=["foo", "bar", "foo", "hello", "string", "foo2"],
6861
)
69-
check(df, expected)
62+
tm.assert_frame_equal(df, expected)
7063
df["foo2"] = 3
7164

7265
# delete (non dup)
@@ -75,31 +68,31 @@ def test_insert_with_duplicate_columns(self):
7568
[[1, 1, 5, "bah", 3], [1, 2, 5, "bah", 3], [2, 3, 5, "bah", 3]],
7669
columns=["foo", "foo", "hello", "string", "foo2"],
7770
)
78-
check(df, expected)
71+
tm.assert_frame_equal(df, expected)
7972

8073
# try to delete again (its not consolidated)
8174
del df["hello"]
8275
expected = DataFrame(
8376
[[1, 1, "bah", 3], [1, 2, "bah", 3], [2, 3, "bah", 3]],
8477
columns=["foo", "foo", "string", "foo2"],
8578
)
86-
check(df, expected)
79+
tm.assert_frame_equal(df, expected)
8780

8881
# consolidate
8982
df = df._consolidate()
9083
expected = DataFrame(
9184
[[1, 1, "bah", 3], [1, 2, "bah", 3], [2, 3, "bah", 3]],
9285
columns=["foo", "foo", "string", "foo2"],
9386
)
94-
check(df, expected)
87+
tm.assert_frame_equal(df, expected)
9588

9689
# insert
9790
df.insert(2, "new_col", 5.0)
9891
expected = DataFrame(
9992
[[1, 1, 5.0, "bah", 3], [1, 2, 5.0, "bah", 3], [2, 3, 5.0, "bah", 3]],
10093
columns=["foo", "foo", "new_col", "string", "foo2"],
10194
)
102-
check(df, expected)
95+
tm.assert_frame_equal(df, expected)
10396

10497
# insert a dup
10598
with pytest.raises(ValueError, match="cannot insert"):
@@ -114,7 +107,7 @@ def test_insert_with_duplicate_columns(self):
114107
],
115108
columns=["foo", "foo", "new_col", "new_col", "string", "foo2"],
116109
)
117-
check(df, expected)
110+
tm.assert_frame_equal(df, expected)
118111

119112
# delete (dup)
120113
del df["foo"]
@@ -130,18 +123,17 @@ def test_dup_across_dtypes(self):
130123
[[1, 1, 1.0, 5], [1, 1, 2.0, 5], [2, 1, 3.0, 5]],
131124
columns=["foo", "bar", "foo", "hello"],
132125
)
133-
check(df)
134126

135127
df["foo2"] = 7.0
136128
expected = DataFrame(
137129
[[1, 1, 1.0, 5, 7.0], [1, 1, 2.0, 5, 7.0], [2, 1, 3.0, 5, 7.0]],
138130
columns=["foo", "bar", "foo", "hello", "foo2"],
139131
)
140-
check(df, expected)
132+
tm.assert_frame_equal(df, expected)
141133

142134
result = df["foo"]
143135
expected = DataFrame([[1, 1.0], [1, 2.0], [2, 3.0]], columns=["foo", "foo"])
144-
check(result, expected)
136+
tm.assert_frame_equal(result, expected)
145137

146138
# multiple replacements
147139
df["foo"] = "string"
@@ -153,13 +145,13 @@ def test_dup_across_dtypes(self):
153145
],
154146
columns=["foo", "bar", "foo", "hello", "foo2"],
155147
)
156-
check(df, expected)
148+
tm.assert_frame_equal(df, expected)
157149

158150
del df["foo"]
159151
expected = DataFrame(
160152
[[1, 5, 7.0], [1, 5, 7.0], [1, 5, 7.0]], columns=["bar", "hello", "foo2"]
161153
)
162-
check(df, expected)
154+
tm.assert_frame_equal(df, expected)
163155

164156
def test_column_dups_indexes(self):
165157
# check column dups with index equal and not equal to df's index
@@ -176,7 +168,7 @@ def test_column_dups_indexes(self):
176168
columns=["A", "B", "A"],
177169
)
178170
this_df["A"] = index
179-
check(this_df, expected_df)
171+
tm.assert_frame_equal(this_df, expected_df)
180172

181173
def test_changing_dtypes_with_duplicate_columns(self):
182174
# multiple assignments that change dtypes
@@ -188,15 +180,15 @@ def test_changing_dtypes_with_duplicate_columns(self):
188180
expected = DataFrame(1.0, index=range(5), columns=["that", "that"])
189181

190182
df["that"] = 1.0
191-
check(df, expected)
183+
tm.assert_frame_equal(df, expected)
192184

193185
df = DataFrame(
194186
np.random.default_rng(2).random((5, 2)), columns=["that", "that"]
195187
)
196188
expected = DataFrame(1, index=range(5), columns=["that", "that"])
197189

198190
df["that"] = 1
199-
check(df, expected)
191+
tm.assert_frame_equal(df, expected)
200192

201193
def test_dup_columns_comparisons(self):
202194
# equality
@@ -231,7 +223,7 @@ def test_mixed_column_selection(self):
231223
)
232224
expected = pd.concat([dfbool["one"], dfbool["three"], dfbool["one"]], axis=1)
233225
result = dfbool[["one", "three", "one"]]
234-
check(result, expected)
226+
tm.assert_frame_equal(result, expected)
235227

236228
def test_multi_axis_dups(self):
237229
# multi-axis dups
@@ -251,29 +243,26 @@ def test_multi_axis_dups(self):
251243
)
252244
z = df[["A", "C", "A"]]
253245
result = z.loc[["a", "c", "a"]]
254-
check(result, expected)
246+
tm.assert_frame_equal(result, expected)
255247

256248
def test_columns_with_dups(self):
257249
# GH 3468 related
258250

259251
# basic
260252
df = DataFrame([[1, 2]], columns=["a", "a"])
261253
df.columns = ["a", "a.1"]
262-
str(df)
263254
expected = DataFrame([[1, 2]], columns=["a", "a.1"])
264255
tm.assert_frame_equal(df, expected)
265256

266257
df = DataFrame([[1, 2, 3]], columns=["b", "a", "a"])
267258
df.columns = ["b", "a", "a.1"]
268-
str(df)
269259
expected = DataFrame([[1, 2, 3]], columns=["b", "a", "a.1"])
270260
tm.assert_frame_equal(df, expected)
271261

272262
def test_columns_with_dup_index(self):
273263
# with a dup index
274264
df = DataFrame([[1, 2]], columns=["a", "a"])
275265
df.columns = ["b", "b"]
276-
str(df)
277266
expected = DataFrame([[1, 2]], columns=["b", "b"])
278267
tm.assert_frame_equal(df, expected)
279268

@@ -284,7 +273,6 @@ def test_multi_dtype(self):
284273
columns=["a", "a", "b", "b", "d", "c", "c"],
285274
)
286275
df.columns = list("ABCDEFG")
287-
str(df)
288276
expected = DataFrame(
289277
[[1, 2, 1.0, 2.0, 3.0, "foo", "bar"]], columns=list("ABCDEFG")
290278
)
@@ -293,7 +281,6 @@ def test_multi_dtype(self):
293281
def test_multi_dtype2(self):
294282
df = DataFrame([[1, 2, "foo", "bar"]], columns=["a", "a", "a", "a"])
295283
df.columns = ["a", "a.1", "a.2", "a.3"]
296-
str(df)
297284
expected = DataFrame([[1, 2, "foo", "bar"]], columns=["a", "a.1", "a.2", "a.3"])
298285
tm.assert_frame_equal(df, expected)
299286

pandas/tests/frame/test_repr.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
from pandas import (
1111
NA,
1212
Categorical,
13+
CategoricalIndex,
1314
DataFrame,
15+
IntervalIndex,
1416
MultiIndex,
1517
NaT,
1618
PeriodIndex,
@@ -26,6 +28,21 @@
2628

2729

2830
class TestDataFrameRepr:
31+
def test_repr_should_return_str(self):
32+
# https://docs.python.org/3/reference/datamodel.html#object.__repr__
33+
# "...The return value must be a string object."
34+
35+
# (str on py2.x, str (unicode) on py3)
36+
37+
data = [8, 5, 3, 5]
38+
index1 = ["\u03c3", "\u03c4", "\u03c5", "\u03c6"]
39+
cols = ["\u03c8"]
40+
df = DataFrame(data, columns=cols, index=index1)
41+
assert type(df.__repr__()) is str # noqa: E721
42+
43+
ser = df[cols[0]]
44+
assert type(ser.__repr__()) is str # noqa: E721
45+
2946
def test_repr_bytes_61_lines(self):
3047
# GH#12857
3148
lets = list("ACDEFGHIJKLMNOP")
@@ -291,6 +308,27 @@ def test_latex_repr(self):
291308
# GH 12182
292309
assert df._repr_latex_() is None
293310

311+
def test_repr_with_datetimeindex(self):
312+
df = DataFrame({"A": [1, 2, 3]}, index=date_range("2000", periods=3))
313+
result = repr(df)
314+
expected = " A\n2000-01-01 1\n2000-01-02 2\n2000-01-03 3"
315+
assert result == expected
316+
317+
def test_repr_with_intervalindex(self):
318+
# https://github.com/pandas-dev/pandas/pull/24134/files
319+
df = DataFrame(
320+
{"A": [1, 2, 3, 4]}, index=IntervalIndex.from_breaks([0, 1, 2, 3, 4])
321+
)
322+
result = repr(df)
323+
expected = " A\n(0, 1] 1\n(1, 2] 2\n(2, 3] 3\n(3, 4] 4"
324+
assert result == expected
325+
326+
def test_repr_with_categorical_index(self):
327+
df = DataFrame({"A": [1, 2, 3]}, index=CategoricalIndex(["a", "b", "c"]))
328+
result = repr(df)
329+
expected = " A\na 1\nb 2\nc 3"
330+
assert result == expected
331+
294332
def test_repr_categorical_dates_periods(self):
295333
# normal DataFrame
296334
dt = date_range("2011-01-01 09:00", freq="h", periods=5, tz="US/Eastern")

pandas/tests/indexes/base_class/test_formats.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99

1010
class TestIndexRendering:
11+
def test_repr_is_valid_construction_code(self):
12+
# for the case of Index, where the repr is traditional rather than
13+
# stylized
14+
idx = Index(["a", "b"])
15+
res = eval(repr(idx))
16+
tm.assert_index_equal(res, idx)
17+
1118
@pytest.mark.parametrize(
1219
"index,expected",
1320
[

pandas/tests/indexes/categorical/test_category.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,6 @@ def test_ensure_copied_data(self):
257257
result = CategoricalIndex(index.values, copy=False)
258258
assert result._data._codes is index._data._codes
259259

260-
def test_frame_repr(self):
261-
df = pd.DataFrame({"A": [1, 2, 3]}, index=CategoricalIndex(["a", "b", "c"]))
262-
result = repr(df)
263-
expected = " A\na 1\nb 2\nc 3"
264-
assert result == expected
265-
266260

267261
class TestCategoricalIndex2:
268262
def test_view_i8(self):

0 commit comments

Comments
 (0)