From a30b8bbf076f3e3e27a070a5ad417e14db363286 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sun, 17 Dec 2023 18:46:26 +0100 Subject: [PATCH] Adjust pivot tests for new string option --- pandas/conftest.py | 4 ++-- pandas/tests/io/excel/test_readers.py | 17 +++++++++++------ pandas/tests/io/excel/test_writers.py | 20 ++++++++++---------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index 7c829ed4b8cb9..7bfd1b35f5314 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -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")), ) diff --git a/pandas/tests/io/excel/test_readers.py b/pandas/tests/io/excel/test_readers.py index fe9ad8de5765f..15712f36da4ca 100644 --- a/pandas/tests/io/excel/test_readers.py +++ b/pandas/tests/io/excel/test_readers.py @@ -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" @@ -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), } ), ), @@ -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) diff --git a/pandas/tests/io/excel/test_writers.py b/pandas/tests/io/excel/test_writers.py index 6c91fcf39c30c..8c003723c1c71 100644 --- a/pandas/tests/io/excel/test_writers.py +++ b/pandas/tests/io/excel/test_writers.py @@ -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)], @@ -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"), ) @@ -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) @@ -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) @@ -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") @@ -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( @@ -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) @@ -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)