Skip to content

Commit 3740a60

Browse files
committed
simplify
1 parent 269dba9 commit 3740a60

File tree

1 file changed

+27
-53
lines changed

1 file changed

+27
-53
lines changed

pandas/tests/io/excel/test_readers.py

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,16 @@ def df_ref(datapath):
130130
return df_ref
131131

132132

133-
def adjust_expected(expected: DataFrame, read_ext: str, engine: str | None) -> None:
134-
expected.index.name = None
135-
133+
def get_exp_unit(read_ext: str, engine: str | None) -> str:
136134
unit = "us"
137135
if (read_ext == ".ods") ^ (engine == "calamine"):
138-
# TODO: why is .ods & calamine a separate special case?
139136
unit = "s"
137+
return unit
138+
139+
140+
def adjust_expected(expected: DataFrame, read_ext: str, engine: str | None) -> None:
141+
expected.index.name = None
142+
unit = get_exp_unit(read_ext, engine)
140143
expected.index = expected.index.as_unit(unit)
141144

142145

@@ -309,11 +312,6 @@ def test_usecols_diff_positional_int_columns_order(
309312

310313
expected = df_ref[["A", "C"]]
311314
adjust_expected(expected, read_ext, engine)
312-
if engine == "calamine":
313-
unit = "s"
314-
if read_ext == ".ods": # WTF?
315-
unit = "us"
316-
expected.index = expected.index.as_unit(unit)
317315

318316
result = pd.read_excel(
319317
"test1" + read_ext, sheet_name="Sheet1", index_col=0, usecols=usecols
@@ -457,24 +455,26 @@ def test_excel_table(self, request, engine, read_ext, df_ref):
457455
def test_reader_special_dtypes(self, request, engine, read_ext):
458456
xfail_datetimes_with_pyxlsb(engine, request)
459457

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

480480
# should read in correctly and infer types
@@ -634,8 +634,8 @@ def test_dtype_backend(self, read_ext, dtype_backend, engine):
634634
expected["j"] = ArrowExtensionArray(pa.array([None, None]))
635635
else:
636636
expected = df
637-
if not ((read_ext == ".ods") ^ (engine == "calamine")):
638-
expected["i"] = expected["i"].astype("M8[us]")
637+
unit = get_exp_unit(read_ext, engine)
638+
expected["i"] = expected["i"].astype(f"M8[{unit}]")
639639

640640
tm.assert_frame_equal(result, expected)
641641

@@ -1030,9 +1030,7 @@ def test_read_excel_multiindex(self, request, engine, read_ext):
10301030
if engine == "calamine" and read_ext == ".ods":
10311031
request.applymarker(pytest.mark.xfail(reason="Last test fails in calamine"))
10321032

1033-
unit = "us"
1034-
if (read_ext == ".ods") ^ (engine == "calamine"):
1035-
unit = "s"
1033+
unit = get_exp_unit(read_ext, engine)
10361034

10371035
mi = MultiIndex.from_product([["foo", "bar"], ["a", "b"]])
10381036
mi_file = "testmultiindex" + read_ext
@@ -1127,9 +1125,9 @@ def test_read_excel_multiindex_blank_after_name(
11271125

11281126
mi_file = "testmultiindex" + read_ext
11291127
mi = MultiIndex.from_product([["foo", "bar"], ["a", "b"]], names=["c1", "c2"])
1130-
unit = "us"
1131-
if (read_ext == ".ods") ^ (engine == "calamine"):
1132-
unit = "s"
1128+
1129+
unit = get_exp_unit(read_ext, engine)
1130+
11331131
expected = DataFrame(
11341132
[
11351133
[1, 2.5, pd.Timestamp("2015-01-01").as_unit(unit), True],
@@ -1246,13 +1244,7 @@ def test_read_excel_skiprows(self, request, engine, read_ext):
12461244
# GH 4903
12471245
xfail_datetimes_with_pyxlsb(engine, request)
12481246

1249-
unit = "us"
1250-
if read_ext == ".ods":
1251-
unit = "s"
1252-
if engine == "calamine":
1253-
unit = "us"
1254-
elif engine == "calamine":
1255-
unit = "s"
1247+
unit = get_exp_unit(read_ext, engine)
12561248

12571249
actual = pd.read_excel(
12581250
"testskiprows" + read_ext, sheet_name="skiprows_list", skiprows=[0, 2]
@@ -1305,13 +1297,7 @@ def test_read_excel_skiprows(self, request, engine, read_ext):
13051297
def test_read_excel_skiprows_callable_not_in(self, request, engine, read_ext):
13061298
# GH 4903
13071299
xfail_datetimes_with_pyxlsb(engine, request)
1308-
unit = "us"
1309-
if read_ext == ".ods":
1310-
unit = "s"
1311-
if engine == "calamine":
1312-
unit = "us" # WTF?
1313-
elif engine == "calamine":
1314-
unit = "s"
1300+
unit = get_exp_unit(read_ext, engine)
13151301

13161302
actual = pd.read_excel(
13171303
"testskiprows" + read_ext,
@@ -1583,11 +1569,6 @@ def test_excel_table_sheet_by_index(self, request, engine, read_ext, df_ref):
15831569

15841570
expected = df_ref
15851571
adjust_expected(expected, read_ext, engine)
1586-
if engine == "calamine":
1587-
unit = "s"
1588-
if read_ext == ".ods":
1589-
unit = "us"
1590-
expected.index = expected.index.as_unit(unit)
15911572

15921573
with pd.ExcelFile("test1" + read_ext) as excel:
15931574
df1 = pd.read_excel(excel, sheet_name=0, index_col=0)
@@ -1615,11 +1596,6 @@ def test_sheet_name(self, request, engine, read_ext, df_ref):
16151596

16161597
expected = df_ref
16171598
adjust_expected(expected, read_ext, engine)
1618-
if engine == "calamine":
1619-
unit = "s"
1620-
if read_ext == ".ods":
1621-
unit = "us"
1622-
expected.index = expected.index.as_unit(unit)
16231599

16241600
filename = "test1"
16251601
sheet_name = "Sheet1"
@@ -1711,9 +1687,7 @@ def test_read_datetime_multiindex(self, request, engine, read_ext):
17111687
f = "test_datetime_mi" + read_ext
17121688
with pd.ExcelFile(f) as excel:
17131689
actual = pd.read_excel(excel, header=[0, 1], index_col=0, engine=engine)
1714-
unit = "us"
1715-
if (read_ext == ".ods") ^ (engine == "calamine"):
1716-
unit = "s"
1690+
unit = get_exp_unit(read_ext, engine)
17171691
expected_column_index = MultiIndex.from_tuples(
17181692
[
17191693
(

0 commit comments

Comments
 (0)