Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLN: __repr__ tests #55798

Merged
merged 6 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions pandas/tests/frame/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ def test_getitem_periodindex(self):
ts = df[rng[0]]
tm.assert_series_equal(ts, df.iloc[:, 0])

# GH#1211; smoketest unrelated to the rest of this test
repr(df)

ts = df["1/1/2000"]
tm.assert_series_equal(ts, df.iloc[:, 0])

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

tm.assert_frame_equal(result, expected)
result.dtypes
str(result)

def test_getitem_boolean_frame_with_duplicate_columns(self, df_dup_cols):
# where
Expand All @@ -388,8 +383,6 @@ def test_getitem_boolean_frame_with_duplicate_columns(self, df_dup_cols):
result = df[df > 6]

tm.assert_frame_equal(result, expected)
result.dtypes
str(result)

def test_getitem_empty_frame_with_boolean(self):
# Test for issue GH#11859
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,6 @@ def test_setitem_None(self, float_frame):
float_frame.loc[:, None], float_frame["A"], check_names=False
)
tm.assert_series_equal(float_frame[None], float_frame["A"], check_names=False)
repr(float_frame)

def test_loc_setitem_boolean_mask_allfalse(self):
# GH 9596
Expand Down
2 changes: 0 additions & 2 deletions pandas/tests/frame/indexing/test_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ def test_insert_column_bug_4032(self):
df.insert(0, "a", [1, 2])
result = df.rename(columns={})

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

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

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

Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,8 +868,6 @@ def test_setitem_with_expansion_categorical_dtype(self):

# setting with a Categorical
df["D"] = cat
str(df)

result = df.dtypes
expected = Series(
[np.dtype("int32"), CategoricalDtype(categories=labels, ordered=False)],
Expand All @@ -879,8 +877,6 @@ def test_setitem_with_expansion_categorical_dtype(self):

# setting with a Series
df["E"] = ser
str(df)

result = df.dtypes
expected = Series(
[
Expand Down
2 changes: 0 additions & 2 deletions pandas/tests/frame/methods/test_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,6 @@ def test_rename_with_duplicate_columns(self):
# TODO: can we construct this without merge?
k = merge(df4, df5, how="inner", left_index=True, right_index=True)
result = k.rename(columns={"TClose_x": "TClose", "TClose_y": "QT_Close"})
str(result)
result.dtypes

expected = DataFrame(
[[0.0454, 22.02, 0.0422, 20130331, 600809, "饡驦", 30.01]],
Expand Down
2 changes: 0 additions & 2 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,6 @@ def test_arithmetic_with_duplicate_columns(self, op):
df.columns = ["A", "A"]
result = getattr(df, op)(df)
tm.assert_frame_equal(result, expected)
str(result)
result.dtypes

@pytest.mark.parametrize("level", [0, None])
def test_broadcast_multiindex(self, level):
Expand Down
51 changes: 19 additions & 32 deletions pandas/tests/frame/test_nonunique_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@
import pandas._testing as tm


def check(result, expected=None):
if expected is not None:
tm.assert_frame_equal(result, expected)
result.dtypes
str(result)


class TestDataFrameNonuniqueIndexes:
def test_setattr_columns_vs_construct_with_columns(self):
# assignment
Expand All @@ -26,7 +19,7 @@ def test_setattr_columns_vs_construct_with_columns(self):
df = DataFrame(arr, columns=["A", "A"])
df.columns = idx
expected = DataFrame(arr, columns=idx)
check(df, expected)
tm.assert_frame_equal(df, expected)

def test_setattr_columns_vs_construct_with_columns_datetimeindx(self):
idx = date_range("20130101", periods=4, freq="QE-NOV")
Expand All @@ -35,7 +28,7 @@ def test_setattr_columns_vs_construct_with_columns_datetimeindx(self):
)
df.columns = idx
expected = DataFrame([[1, 1, 1, 5], [1, 1, 2, 5], [2, 1, 3, 5]], columns=idx)
check(df, expected)
tm.assert_frame_equal(df, expected)

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

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

# set (non-dup)
df["foo2"] = 4
expected = DataFrame(
[[1, 1, 1, 5, "bah", 4], [1, 1, 2, 5, "bah", 4], [2, 1, 3, 5, "bah", 4]],
columns=["foo", "bar", "foo", "hello", "string", "foo2"],
)
check(df, expected)
tm.assert_frame_equal(df, expected)
df["foo2"] = 3

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

# try to delete again (its not consolidated)
del df["hello"]
expected = DataFrame(
[[1, 1, "bah", 3], [1, 2, "bah", 3], [2, 3, "bah", 3]],
columns=["foo", "foo", "string", "foo2"],
)
check(df, expected)
tm.assert_frame_equal(df, expected)

# consolidate
df = df._consolidate()
expected = DataFrame(
[[1, 1, "bah", 3], [1, 2, "bah", 3], [2, 3, "bah", 3]],
columns=["foo", "foo", "string", "foo2"],
)
check(df, expected)
tm.assert_frame_equal(df, expected)

# insert
df.insert(2, "new_col", 5.0)
expected = DataFrame(
[[1, 1, 5.0, "bah", 3], [1, 2, 5.0, "bah", 3], [2, 3, 5.0, "bah", 3]],
columns=["foo", "foo", "new_col", "string", "foo2"],
)
check(df, expected)
tm.assert_frame_equal(df, expected)

# insert a dup
with pytest.raises(ValueError, match="cannot insert"):
Expand All @@ -114,7 +107,7 @@ def test_insert_with_duplicate_columns(self):
],
columns=["foo", "foo", "new_col", "new_col", "string", "foo2"],
)
check(df, expected)
tm.assert_frame_equal(df, expected)

# delete (dup)
del df["foo"]
Expand All @@ -130,18 +123,17 @@ def test_dup_across_dtypes(self):
[[1, 1, 1.0, 5], [1, 1, 2.0, 5], [2, 1, 3.0, 5]],
columns=["foo", "bar", "foo", "hello"],
)
check(df)

df["foo2"] = 7.0
expected = DataFrame(
[[1, 1, 1.0, 5, 7.0], [1, 1, 2.0, 5, 7.0], [2, 1, 3.0, 5, 7.0]],
columns=["foo", "bar", "foo", "hello", "foo2"],
)
check(df, expected)
tm.assert_frame_equal(df, expected)

result = df["foo"]
expected = DataFrame([[1, 1.0], [1, 2.0], [2, 3.0]], columns=["foo", "foo"])
check(result, expected)
tm.assert_frame_equal(result, expected)

# multiple replacements
df["foo"] = "string"
Expand All @@ -153,13 +145,13 @@ def test_dup_across_dtypes(self):
],
columns=["foo", "bar", "foo", "hello", "foo2"],
)
check(df, expected)
tm.assert_frame_equal(df, expected)

del df["foo"]
expected = DataFrame(
[[1, 5, 7.0], [1, 5, 7.0], [1, 5, 7.0]], columns=["bar", "hello", "foo2"]
)
check(df, expected)
tm.assert_frame_equal(df, expected)

def test_column_dups_indexes(self):
# check column dups with index equal and not equal to df's index
Expand All @@ -176,7 +168,7 @@ def test_column_dups_indexes(self):
columns=["A", "B", "A"],
)
this_df["A"] = index
check(this_df, expected_df)
tm.assert_frame_equal(this_df, expected_df)

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

df["that"] = 1.0
check(df, expected)
tm.assert_frame_equal(df, expected)

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

df["that"] = 1
check(df, expected)
tm.assert_frame_equal(df, expected)

def test_dup_columns_comparisons(self):
# equality
Expand Down Expand Up @@ -231,7 +223,7 @@ def test_mixed_column_selection(self):
)
expected = pd.concat([dfbool["one"], dfbool["three"], dfbool["one"]], axis=1)
result = dfbool[["one", "three", "one"]]
check(result, expected)
tm.assert_frame_equal(result, expected)

def test_multi_axis_dups(self):
# multi-axis dups
Expand All @@ -251,29 +243,26 @@ def test_multi_axis_dups(self):
)
z = df[["A", "C", "A"]]
result = z.loc[["a", "c", "a"]]
check(result, expected)
tm.assert_frame_equal(result, expected)

def test_columns_with_dups(self):
# GH 3468 related

# basic
df = DataFrame([[1, 2]], columns=["a", "a"])
df.columns = ["a", "a.1"]
str(df)
expected = DataFrame([[1, 2]], columns=["a", "a.1"])
tm.assert_frame_equal(df, expected)

df = DataFrame([[1, 2, 3]], columns=["b", "a", "a"])
df.columns = ["b", "a", "a.1"]
str(df)
expected = DataFrame([[1, 2, 3]], columns=["b", "a", "a.1"])
tm.assert_frame_equal(df, expected)

def test_columns_with_dup_index(self):
# with a dup index
df = DataFrame([[1, 2]], columns=["a", "a"])
df.columns = ["b", "b"]
str(df)
expected = DataFrame([[1, 2]], columns=["b", "b"])
tm.assert_frame_equal(df, expected)

Expand All @@ -284,7 +273,6 @@ def test_multi_dtype(self):
columns=["a", "a", "b", "b", "d", "c", "c"],
)
df.columns = list("ABCDEFG")
str(df)
expected = DataFrame(
[[1, 2, 1.0, 2.0, 3.0, "foo", "bar"]], columns=list("ABCDEFG")
)
Expand All @@ -293,7 +281,6 @@ def test_multi_dtype(self):
def test_multi_dtype2(self):
df = DataFrame([[1, 2, "foo", "bar"]], columns=["a", "a", "a", "a"])
df.columns = ["a", "a.1", "a.2", "a.3"]
str(df)
expected = DataFrame([[1, 2, "foo", "bar"]], columns=["a", "a.1", "a.2", "a.3"])
tm.assert_frame_equal(df, expected)

Expand Down
38 changes: 38 additions & 0 deletions pandas/tests/frame/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
from pandas import (
NA,
Categorical,
CategoricalIndex,
DataFrame,
IntervalIndex,
MultiIndex,
NaT,
PeriodIndex,
Expand All @@ -26,6 +28,21 @@


class TestDataFrameRepr:
def test_repr_should_return_str(self):
# https://docs.python.org/3/reference/datamodel.html#object.__repr__
# "...The return value must be a string object."

# (str on py2.x, str (unicode) on py3)

data = [8, 5, 3, 5]
index1 = ["\u03c3", "\u03c4", "\u03c5", "\u03c6"]
cols = ["\u03c8"]
df = DataFrame(data, columns=cols, index=index1)
assert type(df.__repr__()) is str # noqa: E721

ser = df[cols[0]]
assert type(ser.__repr__()) is str # noqa: E721

def test_repr_bytes_61_lines(self):
# GH#12857
lets = list("ACDEFGHIJKLMNOP")
Expand Down Expand Up @@ -291,6 +308,27 @@ def test_latex_repr(self):
# GH 12182
assert df._repr_latex_() is None

def test_repr_with_datetimeindex(self):
df = DataFrame({"A": [1, 2, 3]}, index=date_range("2000", periods=3))
result = repr(df)
expected = " A\n2000-01-01 1\n2000-01-02 2\n2000-01-03 3"
assert result == expected

def test_repr_with_intervalindex(self):
# https://github.com/pandas-dev/pandas/pull/24134/files
df = DataFrame(
{"A": [1, 2, 3, 4]}, index=IntervalIndex.from_breaks([0, 1, 2, 3, 4])
)
result = repr(df)
expected = " A\n(0, 1] 1\n(1, 2] 2\n(2, 3] 3\n(3, 4] 4"
assert result == expected

def test_repr_with_categorical_index(self):
df = DataFrame({"A": [1, 2, 3]}, index=CategoricalIndex(["a", "b", "c"]))
result = repr(df)
expected = " A\na 1\nb 2\nc 3"
assert result == expected

def test_repr_categorical_dates_periods(self):
# normal DataFrame
dt = date_range("2011-01-01 09:00", freq="h", periods=5, tz="US/Eastern")
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/base_class/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@


class TestIndexRendering:
def test_repr_is_valid_construction_code(self):
# for the case of Index, where the repr is traditional rather than
# stylized
idx = Index(["a", "b"])
res = eval(repr(idx))
tm.assert_index_equal(res, idx)

@pytest.mark.parametrize(
"index,expected",
[
Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/indexes/categorical/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,6 @@ def test_ensure_copied_data(self):
result = CategoricalIndex(index.values, copy=False)
assert result._data._codes is index._data._codes

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


class TestCategoricalIndex2:
def test_view_i8(self):
Expand Down
Loading
Loading