9
9
DataFrame ,
10
10
Index ,
11
11
Series ,
12
+ option_context ,
12
13
to_numeric ,
13
14
)
14
15
import pandas ._testing as tm
@@ -813,39 +814,86 @@ def test_to_numeric_use_nullable_dtypes(val, dtype):
813
814
814
815
815
816
@pytest .mark .parametrize (
816
- "val, dtype" , [(1 , "Int64" ), (1.5 , "Float64" ), (True , "boolean" )]
817
+ "val, dtype" ,
818
+ [
819
+ (1 , "Int64" ),
820
+ (1.5 , "Float64" ),
821
+ (True , "boolean" ),
822
+ (1 , "int64[pyarrow]" ),
823
+ (1.5 , "float64[pyarrow]" ),
824
+ (True , "bool[pyarrow]" ),
825
+ ],
817
826
)
818
827
def test_to_numeric_use_nullable_dtypes_na (val , dtype ):
819
828
# GH#50505
829
+ if "pyarrow" in dtype :
830
+ pytest .importorskip ("pyarrow" )
831
+ dtype_backend = "pyarrow"
832
+ else :
833
+ dtype_backend = "pandas"
820
834
ser = Series ([val , None ], dtype = object )
821
- result = to_numeric (ser , use_nullable_dtypes = True )
835
+ with option_context ("mode.dtype_backend" , dtype_backend ):
836
+ result = to_numeric (ser , use_nullable_dtypes = True )
822
837
expected = Series ([val , pd .NA ], dtype = dtype )
823
838
tm .assert_series_equal (result , expected )
824
839
825
840
826
841
@pytest .mark .parametrize (
827
842
"val, dtype, downcast" ,
828
- [(1 , "Int8" , "integer" ), (1.5 , "Float32" , "float" ), (1 , "Int8" , "signed" )],
843
+ [
844
+ (1 , "Int8" , "integer" ),
845
+ (1.5 , "Float32" , "float" ),
846
+ (1 , "Int8" , "signed" ),
847
+ (1 , "int8[pyarrow]" , "integer" ),
848
+ (1.5 , "float[pyarrow]" , "float" ),
849
+ (1 , "int8[pyarrow]" , "signed" ),
850
+ ],
829
851
)
830
852
def test_to_numeric_use_nullable_dtypes_downcasting (val , dtype , downcast ):
831
853
# GH#50505
854
+ if "pyarrow" in dtype :
855
+ pytest .importorskip ("pyarrow" )
856
+ dtype_backend = "pyarrow"
857
+ else :
858
+ dtype_backend = "pandas"
832
859
ser = Series ([val , None ], dtype = object )
833
- result = to_numeric (ser , use_nullable_dtypes = True , downcast = downcast )
860
+ with option_context ("mode.dtype_backend" , dtype_backend ):
861
+ result = to_numeric (ser , use_nullable_dtypes = True , downcast = downcast )
834
862
expected = Series ([val , pd .NA ], dtype = dtype )
835
863
tm .assert_series_equal (result , expected )
836
864
837
865
838
- def test_to_numeric_use_nullable_dtypes_downcasting_uint ():
866
+ @pytest .mark .parametrize (
867
+ "smaller, dtype_backend" , [["UInt8" , "pandas" ], ["uint8[pyarrow]" , "pyarrow" ]]
868
+ )
869
+ def test_to_numeric_use_nullable_dtypes_downcasting_uint (smaller , dtype_backend ):
839
870
# GH#50505
871
+ if dtype_backend == "pyarrow" :
872
+ pytest .importorskip ("pyarrow" )
840
873
ser = Series ([1 , pd .NA ], dtype = "UInt64" )
841
- result = to_numeric (ser , use_nullable_dtypes = True , downcast = "unsigned" )
842
- expected = Series ([1 , pd .NA ], dtype = "UInt8" )
874
+ with option_context ("mode.dtype_backend" , dtype_backend ):
875
+ result = to_numeric (ser , use_nullable_dtypes = True , downcast = "unsigned" )
876
+ expected = Series ([1 , pd .NA ], dtype = smaller )
843
877
tm .assert_series_equal (result , expected )
844
878
845
879
846
- @pytest .mark .parametrize ("dtype" , ["Int64" , "UInt64" , "Float64" , "boolean" ])
880
+ @pytest .mark .parametrize (
881
+ "dtype" ,
882
+ [
883
+ "Int64" ,
884
+ "UInt64" ,
885
+ "Float64" ,
886
+ "boolean" ,
887
+ "int64[pyarrow]" ,
888
+ "uint64[pyarrow]" ,
889
+ "float64[pyarrow]" ,
890
+ "bool[pyarrow]" ,
891
+ ],
892
+ )
847
893
def test_to_numeric_use_nullable_dtypes_already_nullable (dtype ):
848
894
# GH#50505
895
+ if "pyarrow" in dtype :
896
+ pytest .importorskip ("pyarrow" )
849
897
ser = Series ([1 , pd .NA ], dtype = dtype )
850
898
result = to_numeric (ser , use_nullable_dtypes = True )
851
899
expected = Series ([1 , pd .NA ], dtype = dtype )
@@ -855,16 +903,30 @@ def test_to_numeric_use_nullable_dtypes_already_nullable(dtype):
855
903
@pytest .mark .parametrize (
856
904
"use_nullable_dtypes, dtype" , [(True , "Float64" ), (False , "float64" )]
857
905
)
858
- def test_to_numeric_use_nullable_dtypes_error (use_nullable_dtypes , dtype ):
906
+ @pytest .mark .parametrize ("dtype_backend" , ["pandas" , "pyarrow" ])
907
+ def test_to_numeric_use_nullable_dtypes_error (
908
+ use_nullable_dtypes , dtype , dtype_backend
909
+ ):
859
910
# GH#50505
911
+ if dtype_backend == "pyarrow" :
912
+ pytest .importorskip ("pyarrow" )
860
913
ser = Series (["a" , "b" , "" ])
861
914
expected = ser .copy ()
862
915
with pytest .raises (ValueError , match = "Unable to parse string" ):
863
- to_numeric (ser , use_nullable_dtypes = use_nullable_dtypes )
916
+ with option_context ("mode.dtype_backend" , dtype_backend ):
917
+ to_numeric (ser , use_nullable_dtypes = use_nullable_dtypes )
864
918
865
- result = to_numeric (ser , use_nullable_dtypes = use_nullable_dtypes , errors = "ignore" )
919
+ with option_context ("mode.dtype_backend" , dtype_backend ):
920
+ result = to_numeric (
921
+ ser , use_nullable_dtypes = use_nullable_dtypes , errors = "ignore"
922
+ )
866
923
tm .assert_series_equal (result , expected )
867
924
868
- result = to_numeric (ser , use_nullable_dtypes = use_nullable_dtypes , errors = "coerce" )
925
+ with option_context ("mode.dtype_backend" , dtype_backend ):
926
+ result = to_numeric (
927
+ ser , use_nullable_dtypes = use_nullable_dtypes , errors = "coerce"
928
+ )
929
+ if use_nullable_dtypes and dtype_backend == "pyarrow" :
930
+ dtype = "double[pyarrow]"
869
931
expected = Series ([np .nan , np .nan , np .nan ], dtype = dtype )
870
932
tm .assert_series_equal (result , expected )
0 commit comments