@@ -118,7 +118,9 @@ def test_from_dtype_from_float(self, data):
118118
119119 # from float
120120 expected = pd .Series (data )
121- result = pd .Series (np .array (data , dtype = "float" ), dtype = str (dtype ))
121+ result = pd .Series (
122+ data .to_numpy (na_value = np .nan , dtype = "float" ), dtype = str (dtype )
123+ )
122124 tm .assert_series_equal (result , expected )
123125
124126 # from int / list
@@ -634,10 +636,47 @@ def test_construct_cast_invalid(self, dtype):
634636 with pytest .raises (TypeError , match = msg ):
635637 pd .Series (arr ).astype (dtype )
636638
637- def test_coerce_to_ndarray_float_NA_rasies (self ):
638- a = pd .array ([0 , 1 , 2 ], dtype = "Int64" )
639- with pytest .raises (TypeError , match = "NAType" ):
640- a ._coerce_to_ndarray (dtype = "float" , na_value = pd .NA )
639+ @pytest .mark .parametrize ("in_series" , [True , False ])
640+ def test_to_numpy_na_nan (self , in_series ):
641+ a = pd .array ([0 , 1 , None ], dtype = "Int64" )
642+ if in_series :
643+ a = pd .Series (a )
644+
645+ result = a .to_numpy (dtype = "float64" , na_value = np .nan )
646+ expected = np .array ([0.0 , 1.0 , np .nan ], dtype = "float64" )
647+ tm .assert_numpy_array_equal (result , expected )
648+
649+ result = a .to_numpy (dtype = "int64" , na_value = - 1 )
650+ expected = np .array ([0 , 1 , - 1 ], dtype = "int64" )
651+ tm .assert_numpy_array_equal (result , expected )
652+
653+ result = a .to_numpy (dtype = "bool" , na_value = False )
654+ expected = np .array ([False , True , False ], dtype = "bool" )
655+ tm .assert_numpy_array_equal (result , expected )
656+
657+ @pytest .mark .parametrize ("in_series" , [True , False ])
658+ @pytest .mark .parametrize ("dtype" , ["int32" , "int64" , "bool" ])
659+ def test_to_numpy_dtype (self , dtype , in_series ):
660+ a = pd .array ([0 , 1 ], dtype = "Int64" )
661+ if in_series :
662+ a = pd .Series (a )
663+
664+ result = a .to_numpy (dtype = dtype )
665+ expected = np .array ([0 , 1 ], dtype = dtype )
666+ tm .assert_numpy_array_equal (result , expected )
667+
668+ @pytest .mark .parametrize ("dtype" , ["float64" , "int64" , "bool" ])
669+ def test_to_numpy_na_raises (self , dtype ):
670+ a = pd .array ([0 , 1 , None ], dtype = "Int64" )
671+ with pytest .raises (ValueError , match = dtype ):
672+ a .to_numpy (dtype = dtype )
673+
674+ def test_astype_str (self ):
675+ a = pd .array ([1 , 2 , None ], dtype = "Int64" )
676+ expected = np .array (["1" , "2" , "NA" ], dtype = object )
677+
678+ tm .assert_numpy_array_equal (a .astype (str ), expected )
679+ tm .assert_numpy_array_equal (a .astype ("str" ), expected )
641680
642681
643682def test_frame_repr (data_missing ):
@@ -887,7 +926,7 @@ def test_reduce_to_float(op):
887926def test_astype_nansafe ():
888927 # see gh-22343
889928 arr = integer_array ([np .nan , 1 , 2 ], dtype = "Int8" )
890- msg = "cannot convert to integer NumPy array with missing values"
929+ msg = "cannot convert to 'uint32'-dtype NumPy array with missing values. "
891930
892931 with pytest .raises (ValueError , match = msg ):
893932 arr .astype ("uint32" )
0 commit comments