Skip to content

TST: Adjust excel tests for new string option #56534

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 2 commits 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
4 changes: 2 additions & 2 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,8 @@ def float_frame() -> DataFrame:
"""
return DataFrame(
np.random.default_rng(2).standard_normal((30, 4)),
index=Index([f"foo_{i}" for i in range(30)], dtype=object),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"foo_{i}" for i in range(30)]),
columns=Index(list("ABCD")),
)


Expand Down
17 changes: 11 additions & 6 deletions pandas/tests/io/excel/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ def test_reader_dtype(self, read_ext):

expected["a"] = expected["a"].astype("float64")
expected["b"] = expected["b"].astype("float32")
expected["c"] = ["001", "002", "003", "004"]
expected["c"] = Series(["001", "002", "003", "004"], dtype=object)
tm.assert_frame_equal(actual, expected)

msg = "Unable to convert column d to type int64"
Expand All @@ -577,8 +577,8 @@ def test_reader_dtype(self, read_ext):
{
"a": Series([1, 2, 3, 4], dtype="float64"),
"b": Series([2.5, 3.5, 4.5, 5.5], dtype="float32"),
"c": ["001", "002", "003", "004"],
"d": ["1", "2", np.nan, "4"],
"c": Series(["001", "002", "003", "004"], dtype=object),
"d": Series(["1", "2", np.nan, "4"], dtype=object),
}
),
),
Expand Down Expand Up @@ -694,15 +694,20 @@ def test_dtype_backend_string(self, read_ext, string_storage):
)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("dtypes, exp_value", [({}, "1"), ({"a.1": "int64"}, 1)])
@pytest.mark.parametrize("dtypes, exp_value", [({}, 1), ({"a.1": "int64"}, 1)])
def test_dtype_mangle_dup_cols(self, read_ext, dtypes, exp_value):
# GH#35211
basename = "df_mangle_dup_col_dtypes"
dtype_dict = {"a": str, **dtypes}
dtype_dict = {"a": object, **dtypes}
dtype_dict_copy = dtype_dict.copy()
# GH#42462
result = pd.read_excel(basename + read_ext, dtype=dtype_dict)
expected = DataFrame({"a": ["1"], "a.1": [exp_value]})
expected = DataFrame(
{
"a": Series([1], dtype=object),
"a.1": Series([exp_value], dtype=object if not dtypes else None),
}
)
assert dtype_dict == dtype_dict_copy, "dtype dict changed"
tm.assert_frame_equal(result, expected)

Expand Down
20 changes: 10 additions & 10 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,14 @@ def test_excel_multindex_roundtrip(
check_names = bool(r_idx_names) or r_idx_levels <= 1

if c_idx_levels == 1:
columns = Index(list("abcde"), dtype=object)
columns = Index(list("abcde"))
else:
columns = MultiIndex.from_arrays(
[range(5) for _ in range(c_idx_levels)],
names=[f"{c_idx_names}-{i}" for i in range(c_idx_levels)],
)
if r_idx_levels == 1:
index = Index(list("ghijk"), dtype=object)
index = Index(list("ghijk"))
else:
index = MultiIndex.from_arrays(
[range(5) for _ in range(r_idx_levels)],
Expand Down Expand Up @@ -475,7 +475,7 @@ def test_ts_frame(self, path):
unit = get_exp_unit(path)
df = DataFrame(
np.random.default_rng(2).standard_normal((5, 4)),
columns=Index(list("ABCD"), dtype=object),
columns=Index(list("ABCD")),
index=date_range("2000-01-01", periods=5, freq="B"),
)

Expand Down Expand Up @@ -556,7 +556,7 @@ def test_sheets(self, frame, path):
unit = get_exp_unit(path)
tsframe = DataFrame(
np.random.default_rng(2).standard_normal((5, 4)),
columns=Index(list("ABCD"), dtype=object),
columns=Index(list("ABCD")),
index=date_range("2000-01-01", periods=5, freq="B"),
)
index = pd.DatetimeIndex(np.asarray(tsframe.index), freq=None)
Expand Down Expand Up @@ -684,7 +684,7 @@ def test_excel_roundtrip_datetime(self, merge_cells, path):
# freq does not round-trip
tsframe = DataFrame(
np.random.default_rng(2).standard_normal((5, 4)),
columns=Index(list("ABCD"), dtype=object),
columns=Index(list("ABCD")),
index=date_range("2000-01-01", periods=5, freq="B"),
)
index = pd.DatetimeIndex(np.asarray(tsframe.index), freq=None)
Expand Down Expand Up @@ -812,7 +812,7 @@ def test_to_excel_periodindex(self, path):
# xp has a PeriodIndex
df = DataFrame(
np.random.default_rng(2).standard_normal((5, 4)),
columns=Index(list("ABCD"), dtype=object),
columns=Index(list("ABCD")),
index=date_range("2000-01-01", periods=5, freq="B"),
)
xp = df.resample("ME").mean().to_period("M")
Expand Down Expand Up @@ -882,7 +882,7 @@ def test_to_excel_multiindex_dates(self, merge_cells, path):
unit = get_exp_unit(path)
tsframe = DataFrame(
np.random.default_rng(2).standard_normal((5, 4)),
columns=Index(list("ABCD"), dtype=object),
columns=Index(list("ABCD")),
index=date_range("2000-01-01", periods=5, freq="B"),
)
tsframe.index = MultiIndex.from_arrays(
Expand Down Expand Up @@ -1310,7 +1310,7 @@ def test_freeze_panes(self, path):
def test_path_path_lib(self, engine, ext):
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
columns=Index(list("ABCD")),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
writer = partial(df.to_excel, engine=engine)
Expand All @@ -1322,8 +1322,8 @@ def test_path_path_lib(self, engine, ext):
def test_path_local_path(self, engine, ext):
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)]),
)
writer = partial(df.to_excel, engine=engine)

Expand Down