Skip to content

Adjust format tests for new string option #56535

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

Merged
merged 1 commit into from
Dec 18, 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
5 changes: 4 additions & 1 deletion pandas/tests/io/formats/style/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pandas import (
DataFrame,
MultiIndex,
Series,
option_context,
)

Expand All @@ -22,7 +23,9 @@

@pytest.fixture
def df():
return DataFrame({"A": [0, 1], "B": [-0.61, -1.22], "C": ["ab", "cd"]})
return DataFrame(
{"A": [0, 1], "B": [-0.61, -1.22], "C": Series(["ab", "cd"], dtype=object)}
)


@pytest.fixture
Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/io/formats/style/test_to_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

import pytest

from pandas import DataFrame
from pandas import (
DataFrame,
Series,
)

pytest.importorskip("jinja2")
from pandas.io.formats.style import Styler


@pytest.fixture
def df():
return DataFrame({"A": [0, 1], "B": [-0.61, -1.22], "C": ["ab", "cd"]})
return DataFrame(
{"A": [0, 1], "B": [-0.61, -1.22], "C": Series(["ab", "cd"], dtype=object)}
)


@pytest.fixture
Expand Down
17 changes: 11 additions & 6 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import numpy as np
import pytest

from pandas._config import using_pyarrow_string_dtype

import pandas as pd
from pandas import (
DataFrame,
Expand Down Expand Up @@ -892,7 +894,7 @@ def test_truncate_with_different_dtypes(self, dtype):

def test_truncate_with_different_dtypes2(self):
# 12045
df = DataFrame({"text": ["some words"] + [None] * 9})
df = DataFrame({"text": ["some words"] + [None] * 9}, dtype=object)

with option_context("display.max_rows", 8, "display.max_columns", 3):
result = str(df)
Expand Down Expand Up @@ -1295,9 +1297,9 @@ def test_float_trim_zeros(self):
([3.50, None, "3.50"], "0 3.5\n1 None\n2 3.50\ndtype: object"),
],
)
def test_repr_str_float_truncation(self, data, expected):
def test_repr_str_float_truncation(self, data, expected, using_infer_string):
# GH#38708
series = Series(data)
series = Series(data, dtype=object if "3.50" in data else None)
result = repr(series)
assert result == expected

Expand Down Expand Up @@ -1394,6 +1396,9 @@ def test_unicode_name_in_footer(self):
sf = fmt.SeriesFormatter(s, name="\u05e2\u05d1\u05e8\u05d9\u05ea")
sf._get_footer() # should not raise exception

@pytest.mark.xfail(
using_pyarrow_string_dtype(), reason="Fixup when arrow is default"
)
def test_east_asian_unicode_series(self):
# not aligned properly because of east asian width

Expand Down Expand Up @@ -1762,15 +1767,15 @@ def test_consistent_format(self):
assert res == exp

def chck_ncols(self, s):
with option_context("display.max_rows", 10):
res = repr(s)
lines = res.split("\n")
lines = [
line for line in repr(s).split("\n") if not re.match(r"[^\.]*\.+", line)
][:-1]
ncolsizes = len({len(line.strip()) for line in lines})
assert ncolsizes == 1

@pytest.mark.xfail(
using_pyarrow_string_dtype(), reason="change when arrow is default"
)
def test_format_explicit(self):
test_sers = gen_series_formatting()
with option_context("display.max_rows", 4, "display.show_dimensions", False):
Expand Down
14 changes: 7 additions & 7 deletions pandas/tests/io/formats/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ def test_na_rep_truncated(self):
def test_to_csv_errors(self, errors):
# GH 22610
data = ["\ud800foo"]
ser = pd.Series(data, index=Index(data))
ser = pd.Series(data, index=Index(data, dtype=object), dtype=object)
with tm.ensure_clean("test.csv") as path:
ser.to_csv(path, errors=errors)
# No use in reading back the data as it is not the same anymore
Expand All @@ -682,8 +682,8 @@ def test_to_csv_binary_handle(self, mode):
"""
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
columns=Index(list("ABCD")),
index=Index([f"i-{i}" for i in range(30)]),
)
with tm.ensure_clean() as path:
with open(path, mode="w+b") as handle:
Expand Down Expand Up @@ -720,8 +720,8 @@ def test_to_csv_iterative_compression_name(compression):
# GH 38714
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
columns=Index(list("ABCD")),
index=Index([f"i-{i}" for i in range(30)]),
)
with tm.ensure_clean() as path:
df.to_csv(path, compression=compression, chunksize=1)
Expand All @@ -734,8 +734,8 @@ def test_to_csv_iterative_compression_buffer(compression):
# GH 38714
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
columns=Index(list("ABCD")),
index=Index([f"i-{i}" for i in range(30)]),
)
with io.BytesIO() as buffer:
df.to_csv(buffer, compression=compression, chunksize=1)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def test_to_html_multiindex_odd_even_truncate(max_rows, expected, datapath):
(
DataFrame(
[[0, 1], [2, 3], [4, 5], [6, 7]],
columns=["foo", None],
columns=Index(["foo", None], dtype=object),
index=np.arange(4),
),
{"__index__": lambda x: "abcd"[x]},
Expand Down Expand Up @@ -771,7 +771,7 @@ def test_to_html_render_links(render_links, expected, datapath):
[0, "https://pandas.pydata.org/?q1=a&q2=b", "pydata.org"],
[0, "www.pydata.org", "pydata.org"],
]
df = DataFrame(data, columns=["foo", "bar", None])
df = DataFrame(data, columns=Index(["foo", "bar", None], dtype=object))

result = df.to_html(render_links=render_links)
expected = expected_html(datapath, expected)
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/io/formats/test_to_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import numpy as np
import pytest

from pandas._config import using_pyarrow_string_dtype

from pandas import (
CategoricalIndex,
DataFrame,
Expand Down Expand Up @@ -849,6 +851,7 @@ def test_to_string(self):
frame.to_string()

# TODO: split or simplify this test?
@pytest.mark.xfail(using_pyarrow_string_dtype(), reason="fix when arrow is default")
def test_to_string_index_with_nan(self):
# GH#2850
df = DataFrame(
Expand Down