Skip to content

Commit 8e8d72d

Browse files
mroeschkephofl
authored andcommitted
TST: read_fwf with dtype_backend (pandas-dev#50911)
Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 4ea8e57 commit 8e8d72d

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ to select the nullable dtypes implementation.
5454

5555
* :func:`read_csv` (with ``engine="pyarrow"`` or ``engine="python"``)
5656
* :func:`read_clipboard` (with ``engine="python"``)
57+
* :func:`read_fwf`
5758
* :func:`read_excel`
5859
* :func:`read_html`
5960
* :func:`read_xml`

pandas/tests/io/parser/test_read_fwf.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -948,24 +948,27 @@ def test_widths_and_usecols():
948948
tm.assert_frame_equal(result, expected)
949949

950950

951-
def test_use_nullable_dtypes(string_storage):
951+
@pytest.mark.parametrize("dtype_backend", ["pandas", "pyarrow"])
952+
def test_use_nullable_dtypes(string_storage, dtype_backend):
952953
# GH#50289
953954

954-
data = """a b c d e f g h i
955-
1 2.5 True a
956-
3 4.5 False b True 6 7.5 a"""
957-
with pd.option_context("mode.string_storage", string_storage):
958-
result = read_fwf(StringIO(data), use_nullable_dtypes=True)
955+
if string_storage == "pyarrow" or dtype_backend == "pyarrow":
956+
pa = pytest.importorskip("pyarrow")
959957

960958
if string_storage == "python":
961959
arr = StringArray(np.array(["a", "b"], dtype=np.object_))
962960
arr_na = StringArray(np.array([pd.NA, "a"], dtype=np.object_))
963961
else:
964-
import pyarrow as pa
965-
966962
arr = ArrowStringArray(pa.array(["a", "b"]))
967963
arr_na = ArrowStringArray(pa.array([None, "a"]))
968964

965+
data = """a b c d e f g h i
966+
1 2.5 True a
967+
3 4.5 False b True 6 7.5 a"""
968+
with pd.option_context("mode.string_storage", string_storage):
969+
with pd.option_context("mode.dtype_backend", dtype_backend):
970+
result = read_fwf(StringIO(data), use_nullable_dtypes=True)
971+
969972
expected = DataFrame(
970973
{
971974
"a": pd.Series([1, 3], dtype="Int64"),
@@ -979,4 +982,15 @@ def test_use_nullable_dtypes(string_storage):
979982
"i": pd.Series([pd.NA, pd.NA], dtype="Int64"),
980983
}
981984
)
985+
if dtype_backend == "pyarrow":
986+
from pandas.arrays import ArrowExtensionArray
987+
988+
expected = DataFrame(
989+
{
990+
col: ArrowExtensionArray(pa.array(expected[col], from_pandas=True))
991+
for col in expected.columns
992+
}
993+
)
994+
expected["i"] = ArrowExtensionArray(pa.array([None, None]))
995+
982996
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)