Skip to content

Commit 905a1fc

Browse files
committed
simplify
1 parent cb62419 commit 905a1fc

File tree

1 file changed

+27
-53
lines changed

1 file changed

+27
-53
lines changed

Diff for: pandas/tests/io/excel/test_readers.py

+27-53
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,16 @@ def df_ref(datapath):
128128
return df_ref
129129

130130

131-
def adjust_expected(expected: DataFrame, read_ext: str, engine: str | None) -> None:
132-
expected.index.name = None
133-
131+
def get_exp_unit(read_ext: str, engine: str | None) -> str:
134132
unit = "us"
135133
if (read_ext == ".ods") ^ (engine == "calamine"):
136-
# TODO: why is .ods & calamine a separate special case?
137134
unit = "s"
135+
return unit
136+
137+
138+
def adjust_expected(expected: DataFrame, read_ext: str, engine: str | None) -> None:
139+
expected.index.name = None
140+
unit = get_exp_unit(read_ext, engine)
138141
expected.index = expected.index.as_unit(unit)
139142

140143

@@ -307,11 +310,6 @@ def test_usecols_diff_positional_int_columns_order(
307310

308311
expected = df_ref[["A", "C"]]
309312
adjust_expected(expected, read_ext, engine)
310-
if engine == "calamine":
311-
unit = "s"
312-
if read_ext == ".ods": # WTF?
313-
unit = "us"
314-
expected.index = expected.index.as_unit(unit)
315313

316314
result = pd.read_excel(
317315
"test1" + read_ext, sheet_name="Sheet1", index_col=0, usecols=usecols
@@ -455,24 +453,26 @@ def test_excel_table(self, request, engine, read_ext, df_ref):
455453
def test_reader_special_dtypes(self, request, engine, read_ext):
456454
xfail_datetimes_with_pyxlsb(engine, request)
457455

456+
unit = get_exp_unit(read_ext, engine)
458457
expected = DataFrame.from_dict(
459458
{
460459
"IntCol": [1, 2, -3, 4, 0],
461460
"FloatCol": [1.25, 2.25, 1.83, 1.92, 0.0000000005],
462461
"BoolCol": [True, False, True, True, False],
463462
"StrCol": [1, 2, 3, 4, 5],
464463
"Str2Col": ["a", 3, "c", "d", "e"],
465-
"DateCol": [
466-
datetime(2013, 10, 30),
467-
datetime(2013, 10, 31),
468-
datetime(1905, 1, 1),
469-
datetime(2013, 12, 14),
470-
datetime(2015, 3, 14),
471-
],
464+
"DateCol": Index(
465+
[
466+
datetime(2013, 10, 30),
467+
datetime(2013, 10, 31),
468+
datetime(1905, 1, 1),
469+
datetime(2013, 12, 14),
470+
datetime(2015, 3, 14),
471+
],
472+
dtype=f"M8[{unit}]",
473+
),
472474
},
473475
)
474-
if (read_ext == ".ods") ^ (engine == "calamine"):
475-
expected["DateCol"] = expected["DateCol"].astype("M8[s]")
476476
basename = "test_types"
477477

478478
# should read in correctly and infer types
@@ -632,8 +632,8 @@ def test_dtype_backend(self, read_ext, dtype_backend, engine):
632632
expected["j"] = ArrowExtensionArray(pa.array([None, None]))
633633
else:
634634
expected = df
635-
if not ((read_ext == ".ods") ^ (engine == "calamine")):
636-
expected["i"] = expected["i"].astype("M8[us]")
635+
unit = get_exp_unit(read_ext, engine)
636+
expected["i"] = expected["i"].astype(f"M8[{unit}]")
637637

638638
tm.assert_frame_equal(result, expected)
639639

@@ -1025,9 +1025,7 @@ def test_read_excel_multiindex(self, request, engine, read_ext):
10251025
if engine == "calamine" and read_ext == ".ods":
10261026
request.applymarker(pytest.mark.xfail(reason="Last test fails in calamine"))
10271027

1028-
unit = "us"
1029-
if (read_ext == ".ods") ^ (engine == "calamine"):
1030-
unit = "s"
1028+
unit = get_exp_unit(read_ext, engine)
10311029

10321030
mi = MultiIndex.from_product([["foo", "bar"], ["a", "b"]])
10331031
mi_file = "testmultiindex" + read_ext
@@ -1122,9 +1120,9 @@ def test_read_excel_multiindex_blank_after_name(
11221120

11231121
mi_file = "testmultiindex" + read_ext
11241122
mi = MultiIndex.from_product([["foo", "bar"], ["a", "b"]], names=["c1", "c2"])
1125-
unit = "us"
1126-
if (read_ext == ".ods") ^ (engine == "calamine"):
1127-
unit = "s"
1123+
1124+
unit = get_exp_unit(read_ext, engine)
1125+
11281126
expected = DataFrame(
11291127
[
11301128
[1, 2.5, pd.Timestamp("2015-01-01").as_unit(unit), True],
@@ -1241,13 +1239,7 @@ def test_read_excel_skiprows(self, request, engine, read_ext):
12411239
# GH 4903
12421240
xfail_datetimes_with_pyxlsb(engine, request)
12431241

1244-
unit = "us"
1245-
if read_ext == ".ods":
1246-
unit = "s"
1247-
if engine == "calamine":
1248-
unit = "us"
1249-
elif engine == "calamine":
1250-
unit = "s"
1242+
unit = get_exp_unit(read_ext, engine)
12511243

12521244
actual = pd.read_excel(
12531245
"testskiprows" + read_ext, sheet_name="skiprows_list", skiprows=[0, 2]
@@ -1300,13 +1292,7 @@ def test_read_excel_skiprows(self, request, engine, read_ext):
13001292
def test_read_excel_skiprows_callable_not_in(self, request, engine, read_ext):
13011293
# GH 4903
13021294
xfail_datetimes_with_pyxlsb(engine, request)
1303-
unit = "us"
1304-
if read_ext == ".ods":
1305-
unit = "s"
1306-
if engine == "calamine":
1307-
unit = "us" # WTF?
1308-
elif engine == "calamine":
1309-
unit = "s"
1295+
unit = get_exp_unit(read_ext, engine)
13101296

13111297
actual = pd.read_excel(
13121298
"testskiprows" + read_ext,
@@ -1578,11 +1564,6 @@ def test_excel_table_sheet_by_index(self, request, engine, read_ext, df_ref):
15781564

15791565
expected = df_ref
15801566
adjust_expected(expected, read_ext, engine)
1581-
if engine == "calamine":
1582-
unit = "s"
1583-
if read_ext == ".ods":
1584-
unit = "us"
1585-
expected.index = expected.index.as_unit(unit)
15861567

15871568
with pd.ExcelFile("test1" + read_ext) as excel:
15881569
df1 = pd.read_excel(excel, sheet_name=0, index_col=0)
@@ -1610,11 +1591,6 @@ def test_sheet_name(self, request, engine, read_ext, df_ref):
16101591

16111592
expected = df_ref
16121593
adjust_expected(expected, read_ext, engine)
1613-
if engine == "calamine":
1614-
unit = "s"
1615-
if read_ext == ".ods":
1616-
unit = "us"
1617-
expected.index = expected.index.as_unit(unit)
16181594

16191595
filename = "test1"
16201596
sheet_name = "Sheet1"
@@ -1706,9 +1682,7 @@ def test_read_datetime_multiindex(self, request, engine, read_ext):
17061682
f = "test_datetime_mi" + read_ext
17071683
with pd.ExcelFile(f) as excel:
17081684
actual = pd.read_excel(excel, header=[0, 1], index_col=0, engine=engine)
1709-
unit = "us"
1710-
if (read_ext == ".ods") ^ (engine == "calamine"):
1711-
unit = "s"
1685+
unit = get_exp_unit(read_ext, engine)
17121686
expected_column_index = MultiIndex.from_tuples(
17131687
[
17141688
(

0 commit comments

Comments
 (0)