Skip to content

Commit e54bd0b

Browse files
dsaxtonKevin D Smith
authored and
Kevin D Smith
committed
TST/CLN: Split out some to_string tests (pandas-dev#37234)
1 parent 370762c commit e54bd0b

File tree

2 files changed

+222
-208
lines changed

2 files changed

+222
-208
lines changed

pandas/tests/io/formats/test_format.py

-208
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,6 @@ def has_expanded_repr(df):
158158

159159
@pytest.mark.filterwarnings("ignore::FutureWarning:.*format")
160160
class TestDataFrameFormatting:
161-
def test_repr_embedded_ndarray(self):
162-
arr = np.empty(10, dtype=[("err", object)])
163-
for i in range(len(arr)):
164-
arr["err"][i] = np.random.randn(i)
165-
166-
df = DataFrame(arr)
167-
repr(df["err"])
168-
repr(df)
169-
df.to_string()
170-
171161
def test_eng_float_formatter(self, float_frame):
172162
df = float_frame
173163
df.loc[5] = 0
@@ -204,13 +194,6 @@ def check(null_counts, result):
204194
check(True, False)
205195
check(False, False)
206196

207-
def test_repr_tuples(self):
208-
buf = StringIO()
209-
210-
df = DataFrame({"tups": list(zip(range(10), range(10)))})
211-
repr(df)
212-
df.to_string(col_space=10, buf=buf)
213-
214197
def test_repr_truncation(self):
215198
max_len = 20
216199
with option_context("display.max_colwidth", max_len):
@@ -534,45 +517,6 @@ def test_str_max_colwidth(self):
534517
"1 foo bar stuff 1"
535518
)
536519

537-
def test_to_string_truncate(self):
538-
# GH 9784 - dont truncate when calling DataFrame.to_string
539-
df = pd.DataFrame(
540-
[
541-
{
542-
"a": "foo",
543-
"b": "bar",
544-
"c": "let's make this a very VERY long line that is longer "
545-
"than the default 50 character limit",
546-
"d": 1,
547-
},
548-
{"a": "foo", "b": "bar", "c": "stuff", "d": 1},
549-
]
550-
)
551-
df.set_index(["a", "b", "c"])
552-
assert df.to_string() == (
553-
" a b "
554-
" c d\n"
555-
"0 foo bar let's make this a very VERY long line t"
556-
"hat is longer than the default 50 character limit 1\n"
557-
"1 foo bar "
558-
" stuff 1"
559-
)
560-
with option_context("max_colwidth", 20):
561-
# the display option has no effect on the to_string method
562-
assert df.to_string() == (
563-
" a b "
564-
" c d\n"
565-
"0 foo bar let's make this a very VERY long line t"
566-
"hat is longer than the default 50 character limit 1\n"
567-
"1 foo bar "
568-
" stuff 1"
569-
)
570-
assert df.to_string(max_colwidth=20) == (
571-
" a b c d\n"
572-
"0 foo bar let's make this ... 1\n"
573-
"1 foo bar stuff 1"
574-
)
575-
576520
def test_auto_detect(self):
577521
term_width, term_height = get_terminal_size()
578522
fac = 1.05 # Arbitrary large factor to exceed term width
@@ -633,95 +577,6 @@ def test_to_string_repr_unicode(self):
633577
finally:
634578
sys.stdin = _stdin
635579

636-
def test_to_string_unicode_columns(self, float_frame):
637-
df = DataFrame({"\u03c3": np.arange(10.0)})
638-
639-
buf = StringIO()
640-
df.to_string(buf=buf)
641-
buf.getvalue()
642-
643-
buf = StringIO()
644-
df.info(buf=buf)
645-
buf.getvalue()
646-
647-
result = float_frame.to_string()
648-
assert isinstance(result, str)
649-
650-
def test_to_string_utf8_columns(self):
651-
n = "\u05d0".encode()
652-
653-
with option_context("display.max_rows", 1):
654-
df = DataFrame([1, 2], columns=[n])
655-
repr(df)
656-
657-
def test_to_string_unicode_two(self):
658-
dm = DataFrame({"c/\u03c3": []})
659-
buf = StringIO()
660-
dm.to_string(buf)
661-
662-
def test_to_string_unicode_three(self):
663-
dm = DataFrame(["\xc2"])
664-
buf = StringIO()
665-
dm.to_string(buf)
666-
667-
def test_to_string_with_formatters(self):
668-
df = DataFrame(
669-
{
670-
"int": [1, 2, 3],
671-
"float": [1.0, 2.0, 3.0],
672-
"object": [(1, 2), True, False],
673-
},
674-
columns=["int", "float", "object"],
675-
)
676-
677-
formatters = [
678-
("int", lambda x: f"0x{x:x}"),
679-
("float", lambda x: f"[{x: 4.1f}]"),
680-
("object", lambda x: f"-{x!s}-"),
681-
]
682-
result = df.to_string(formatters=dict(formatters))
683-
result2 = df.to_string(formatters=list(zip(*formatters))[1])
684-
assert result == (
685-
" int float object\n"
686-
"0 0x1 [ 1.0] -(1, 2)-\n"
687-
"1 0x2 [ 2.0] -True-\n"
688-
"2 0x3 [ 3.0] -False-"
689-
)
690-
assert result == result2
691-
692-
def test_to_string_with_datetime64_monthformatter(self):
693-
months = [datetime(2016, 1, 1), datetime(2016, 2, 2)]
694-
x = DataFrame({"months": months})
695-
696-
def format_func(x):
697-
return x.strftime("%Y-%m")
698-
699-
result = x.to_string(formatters={"months": format_func})
700-
expected = "months\n0 2016-01\n1 2016-02"
701-
assert result.strip() == expected
702-
703-
def test_to_string_with_datetime64_hourformatter(self):
704-
705-
x = DataFrame(
706-
{
707-
"hod": pd.to_datetime(
708-
["10:10:10.100", "12:12:12.120"], format="%H:%M:%S.%f"
709-
)
710-
}
711-
)
712-
713-
def format_func(x):
714-
return x.strftime("%H:%M")
715-
716-
result = x.to_string(formatters={"hod": format_func})
717-
expected = "hod\n0 10:10\n1 12:12"
718-
assert result.strip() == expected
719-
720-
def test_to_string_with_formatters_unicode(self):
721-
df = DataFrame({"c/\u03c3": [1, 2, 3]})
722-
result = df.to_string(formatters={"c/\u03c3": str})
723-
assert result == " c/\u03c3\n" + "0 1\n1 2\n2 3"
724-
725580
def test_east_asian_unicode_false(self):
726581
# not aligned properly because of east asian width
727582

@@ -3398,66 +3253,3 @@ def test_filepath_or_buffer_bad_arg_raises(float_frame, method):
33983253
msg = "buf is not a file name and it has no write method"
33993254
with pytest.raises(TypeError, match=msg):
34003255
getattr(float_frame, method)(buf=object())
3401-
3402-
3403-
@pytest.mark.parametrize(
3404-
"input_array, expected",
3405-
[
3406-
("a", "a"),
3407-
(["a", "b"], "a\nb"),
3408-
([1, "a"], "1\na"),
3409-
(1, "1"),
3410-
([0, -1], " 0\n-1"),
3411-
(1.0, "1.0"),
3412-
([" a", " b"], " a\n b"),
3413-
([".1", "1"], ".1\n 1"),
3414-
(["10", "-10"], " 10\n-10"),
3415-
],
3416-
)
3417-
def test_format_remove_leading_space_series(input_array, expected):
3418-
# GH: 24980
3419-
s = pd.Series(input_array).to_string(index=False)
3420-
assert s == expected
3421-
3422-
3423-
@pytest.mark.parametrize(
3424-
"input_array, expected",
3425-
[
3426-
({"A": ["a"]}, "A\na"),
3427-
({"A": ["a", "b"], "B": ["c", "dd"]}, "A B\na c\nb dd"),
3428-
({"A": ["a", 1], "B": ["aa", 1]}, "A B\na aa\n1 1"),
3429-
],
3430-
)
3431-
def test_format_remove_leading_space_dataframe(input_array, expected):
3432-
# GH: 24980
3433-
df = pd.DataFrame(input_array).to_string(index=False)
3434-
assert df == expected
3435-
3436-
3437-
def test_to_string_complex_number_trims_zeros():
3438-
s = pd.Series([1.000000 + 1.000000j, 1.0 + 1.0j, 1.05 + 1.0j])
3439-
result = s.to_string()
3440-
expected = "0 1.00+1.00j\n1 1.00+1.00j\n2 1.05+1.00j"
3441-
assert result == expected
3442-
3443-
3444-
def test_nullable_float_to_string(float_ea_dtype):
3445-
# https://github.com/pandas-dev/pandas/issues/36775
3446-
dtype = float_ea_dtype
3447-
s = pd.Series([0.0, 1.0, None], dtype=dtype)
3448-
result = s.to_string()
3449-
expected = """0 0.0
3450-
1 1.0
3451-
2 <NA>"""
3452-
assert result == expected
3453-
3454-
3455-
def test_nullable_int_to_string(any_nullable_int_dtype):
3456-
# https://github.com/pandas-dev/pandas/issues/36775
3457-
dtype = any_nullable_int_dtype
3458-
s = pd.Series([0, 1, None], dtype=dtype)
3459-
result = s.to_string()
3460-
expected = """0 0
3461-
1 1
3462-
2 <NA>"""
3463-
assert result == expected

0 commit comments

Comments
 (0)