Skip to content

Commit c75f5af

Browse files
authored
CLN: Use fixture dtype_backend in nullable tests (#51129)
* CLN: Use fixture dtype_backend in nullable tests * Remove skip
1 parent d9b56b2 commit c75f5af

12 files changed

+25
-27
lines changed

pandas/conftest.py

+16
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,22 @@ def string_storage(request):
12931293
return request.param
12941294

12951295

1296+
@pytest.fixture(
1297+
params=[
1298+
"pandas",
1299+
pytest.param("pyarrow", marks=td.skip_if_no("pyarrow")),
1300+
]
1301+
)
1302+
def dtype_backend(request):
1303+
"""
1304+
Parametrized fixture for pd.options.mode.string_storage.
1305+
1306+
* 'python'
1307+
* 'pyarrow'
1308+
"""
1309+
return request.param
1310+
1311+
12961312
# Alias so we can test with cartesian product of string_storage
12971313
string_storage2 = string_storage
12981314

pandas/tests/io/excel/test_readers.py

-4
Original file line numberDiff line numberDiff line change
@@ -536,10 +536,6 @@ def test_reader_dtype_str(self, read_ext, dtype, expected):
536536
actual = pd.read_excel(basename + read_ext, dtype=dtype)
537537
tm.assert_frame_equal(actual, expected)
538538

539-
@pytest.mark.parametrize(
540-
"dtype_backend",
541-
["pandas", pytest.param("pyarrow", marks=td.skip_if_no("pyarrow"))],
542-
)
543539
@pytest.mark.parametrize("option", [True, False])
544540
def test_use_nullable_dtypes(self, read_ext, dtype_backend, option):
545541
# GH#36712

pandas/tests/io/json/test_pandas.py

-2
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,6 @@ def test_json_uint64(self):
18691869
result = df.to_json(orient="split")
18701870
assert result == expected
18711871

1872-
@pytest.mark.parametrize("dtype_backend", ["pandas", "pyarrow"])
18731872
@pytest.mark.parametrize(
18741873
"orient", ["split", "records", "values", "index", "columns"]
18751874
)
@@ -1936,7 +1935,6 @@ def test_read_json_nullable(self, string_storage, dtype_backend, orient, option)
19361935

19371936
tm.assert_frame_equal(result, expected)
19381937

1939-
@pytest.mark.parametrize("dtype_backend", ["pandas", "pyarrow"])
19401938
@pytest.mark.parametrize("orient", ["split", "records", "index"])
19411939
def test_read_json_nullable_series(self, string_storage, dtype_backend, orient):
19421940
# GH#50750

pandas/tests/io/parser/test_read_fwf.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -948,17 +948,13 @@ def test_widths_and_usecols():
948948
tm.assert_frame_equal(result, expected)
949949

950950

951-
@pytest.mark.parametrize("dtype_backend", ["pandas", "pyarrow"])
952951
def test_use_nullable_dtypes(string_storage, dtype_backend):
953952
# GH#50289
954-
955-
if string_storage == "pyarrow" or dtype_backend == "pyarrow":
956-
pa = pytest.importorskip("pyarrow")
957-
958953
if string_storage == "python":
959954
arr = StringArray(np.array(["a", "b"], dtype=np.object_))
960955
arr_na = StringArray(np.array([pd.NA, "a"], dtype=np.object_))
961956
else:
957+
pa = pytest.importorskip("pyarrow")
962958
arr = ArrowStringArray(pa.array(["a", "b"]))
963959
arr_na = ArrowStringArray(pa.array([None, "a"]))
964960

@@ -983,6 +979,7 @@ def test_use_nullable_dtypes(string_storage, dtype_backend):
983979
}
984980
)
985981
if dtype_backend == "pyarrow":
982+
pa = pytest.importorskip("pyarrow")
986983
from pandas.arrays import ArrowExtensionArray
987984

988985
expected = DataFrame(

pandas/tests/io/test_clipboard.py

-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,6 @@ def test_raw_roundtrip(self, data):
418418
# Clipboard can sometimes keep previous param causing flaky CI failures
419419
subprocess.run(["xsel", "--delete", "--clipboard"], check=True)
420420

421-
@pytest.mark.parametrize("dtype_backend", ["pandas", "pyarrow"])
422421
@pytest.mark.parametrize("engine", ["c", "python"])
423422
def test_read_clipboard_nullable_dtypes(
424423
self, request, mock_clipboard, string_storage, dtype_backend, engine

pandas/tests/io/test_feather.py

-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ def test_http_path(self, feather_file):
199199
res = read_feather(url)
200200
tm.assert_frame_equal(expected, res)
201201

202-
@pytest.mark.parametrize("dtype_backend", ["pandas", "pyarrow"])
203202
@pytest.mark.parametrize("option", [True, False])
204203
def test_read_json_nullable(self, string_storage, dtype_backend, option):
205204
# GH#50765

pandas/tests/io/test_html.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ def test_to_html_compat(self):
138138
res = self.read_html(out, attrs={"class": "dataframe"}, index_col=0)[0]
139139
tm.assert_frame_equal(res, df)
140140

141-
@pytest.mark.parametrize("dtype_backend", ["pandas", "pyarrow"])
142-
@pytest.mark.parametrize("storage", ["python", "pyarrow"])
143-
def test_use_nullable_dtypes(self, storage, dtype_backend):
141+
def test_use_nullable_dtypes(self, string_storage, dtype_backend):
144142
# GH#50286
145143
df = DataFrame(
146144
{
@@ -155,7 +153,7 @@ def test_use_nullable_dtypes(self, storage, dtype_backend):
155153
}
156154
)
157155

158-
if storage == "python":
156+
if string_storage == "python":
159157
string_array = StringArray(np.array(["a", "b", "c"], dtype=np.object_))
160158
string_array_na = StringArray(np.array(["a", "b", NA], dtype=np.object_))
161159

@@ -165,7 +163,7 @@ def test_use_nullable_dtypes(self, storage, dtype_backend):
165163
string_array_na = ArrowStringArray(pa.array(["a", "b", None]))
166164

167165
out = df.to_html(index=False)
168-
with pd.option_context("mode.string_storage", storage):
166+
with pd.option_context("mode.string_storage", string_storage):
169167
with pd.option_context("mode.dtype_backend", dtype_backend):
170168
result = self.read_html(out, use_nullable_dtypes=True)[0]
171169

pandas/tests/io/test_parquet.py

+2
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ def test_write_column_index_nonstring(self, pa):
591591
msg = r"parquet must have string column names"
592592
self.check_error_on_write(df, engine, ValueError, msg)
593593

594+
@pytest.mark.skipif(pa_version_under6p0, reason="minimum pyarrow not installed")
594595
def test_use_nullable_dtypes(self, engine, request):
595596
import pyarrow.parquet as pq
596597

@@ -640,6 +641,7 @@ def test_use_nullable_dtypes(self, engine, request):
640641
expected = expected.drop("c", axis=1)
641642
tm.assert_frame_equal(result2, expected)
642643

644+
@pytest.mark.skipif(pa_version_under6p0, reason="minimum pyarrow not installed")
643645
def test_use_nullable_dtypes_option(self, engine, request):
644646
# GH#50748
645647
import pyarrow.parquet as pq

pandas/tests/io/test_spss.py

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def test_spss_usecols(datapath):
8282
pd.read_spss(fname, usecols="VAR00002")
8383

8484

85-
@pytest.mark.parametrize("dtype_backend", ["pandas", "pyarrow"])
8685
def test_spss_umlauts_use_nullable_dtypes(datapath, dtype_backend):
8786
# test file from the Haven project (https://haven.tidyverse.org/)
8887
fname = datapath("io", "data", "spss", "umlauts.sav")

pandas/tests/io/test_sql.py

-2
Original file line numberDiff line numberDiff line change
@@ -2360,7 +2360,6 @@ def test_get_engine_auto_error_message(self):
23602360

23612361
@pytest.mark.parametrize("option", [True, False])
23622362
@pytest.mark.parametrize("func", ["read_sql", "read_sql_query"])
2363-
@pytest.mark.parametrize("dtype_backend", ["pandas", "pyarrow"])
23642363
def test_read_sql_nullable_dtypes(
23652364
self, string_storage, func, option, dtype_backend
23662365
):
@@ -2395,7 +2394,6 @@ def test_read_sql_nullable_dtypes(
23952394

23962395
@pytest.mark.parametrize("option", [True, False])
23972396
@pytest.mark.parametrize("func", ["read_sql", "read_sql_table"])
2398-
@pytest.mark.parametrize("dtype_backend", ["pandas", "pyarrow"])
23992397
def test_read_sql_nullable_dtypes_table(
24002398
self, string_storage, func, option, dtype_backend
24012399
):

pandas/tests/io/xml/test_xml.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1773,11 +1773,8 @@ def test_s3_parser_consistency():
17731773
tm.assert_frame_equal(df_lxml, df_etree)
17741774

17751775

1776-
@pytest.mark.parametrize("dtype_backend", ["pandas", "pyarrow"])
17771776
def test_read_xml_nullable_dtypes(parser, string_storage, dtype_backend):
17781777
# GH#50500
1779-
if string_storage == "pyarrow" or dtype_backend == "pyarrow":
1780-
pa = pytest.importorskip("pyarrow")
17811778
data = """<?xml version='1.0' encoding='utf-8'?>
17821779
<data xmlns="http://example.com">
17831780
<row>
@@ -1809,6 +1806,7 @@ def test_read_xml_nullable_dtypes(parser, string_storage, dtype_backend):
18091806
string_array_na = StringArray(np.array(["x", NA], dtype=np.object_))
18101807

18111808
else:
1809+
pa = pytest.importorskip("pyarrow")
18121810
string_array = ArrowStringArray(pa.array(["x", "y"]))
18131811
string_array_na = ArrowStringArray(pa.array(["x", None]))
18141812

@@ -1831,6 +1829,7 @@ def test_read_xml_nullable_dtypes(parser, string_storage, dtype_backend):
18311829
)
18321830

18331831
if dtype_backend == "pyarrow":
1832+
pa = pytest.importorskip("pyarrow")
18341833
from pandas.arrays import ArrowExtensionArray
18351834

18361835
expected = DataFrame(

pandas/tests/tools/test_to_numeric.py

-3
Original file line numberDiff line numberDiff line change
@@ -912,13 +912,10 @@ def test_to_numeric_use_nullable_dtypes_already_nullable(dtype):
912912
@pytest.mark.parametrize(
913913
"use_nullable_dtypes, dtype", [(True, "Float64"), (False, "float64")]
914914
)
915-
@pytest.mark.parametrize("dtype_backend", ["pandas", "pyarrow"])
916915
def test_to_numeric_use_nullable_dtypes_error(
917916
use_nullable_dtypes, dtype, dtype_backend
918917
):
919918
# GH#50505
920-
if dtype_backend == "pyarrow":
921-
pytest.importorskip("pyarrow")
922919
ser = Series(["a", "b", ""])
923920
expected = ser.copy()
924921
with pytest.raises(ValueError, match="Unable to parse string"):

0 commit comments

Comments
 (0)