@@ -948,24 +948,27 @@ def test_widths_and_usecols():
948
948
tm .assert_frame_equal (result , expected )
949
949
950
950
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 ):
952
953
# GH#50289
953
954
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" )
959
957
960
958
if string_storage == "python" :
961
959
arr = StringArray (np .array (["a" , "b" ], dtype = np .object_ ))
962
960
arr_na = StringArray (np .array ([pd .NA , "a" ], dtype = np .object_ ))
963
961
else :
964
- import pyarrow as pa
965
-
966
962
arr = ArrowStringArray (pa .array (["a" , "b" ]))
967
963
arr_na = ArrowStringArray (pa .array ([None , "a" ]))
968
964
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
+
969
972
expected = DataFrame (
970
973
{
971
974
"a" : pd .Series ([1 , 3 ], dtype = "Int64" ),
@@ -979,4 +982,15 @@ def test_use_nullable_dtypes(string_storage):
979
982
"i" : pd .Series ([pd .NA , pd .NA ], dtype = "Int64" ),
980
983
}
981
984
)
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
+
982
996
tm .assert_frame_equal (result , expected )
0 commit comments