@@ -48,6 +48,16 @@ def dtype(na_object, coerce):
4848 return StringDType (coerce = coerce )
4949
5050
51+ # second copy for cast tests to do a cartesian product over dtypes
52+ @pytest .fixture ()
53+ def dtype2 (na_object , coerce ):
54+ # explicit is check for pd_NA because != with pd_NA returns pd_NA
55+ if na_object is pd_NA or na_object != "unset" :
56+ return StringDType (na_object = na_object , coerce = coerce )
57+ else :
58+ return StringDType (coerce = coerce )
59+
60+
5161def test_dtype_creation ():
5262 hashes = set ()
5363 dt = StringDType ()
@@ -136,6 +146,34 @@ def test_scalars_string_conversion(data, dtype):
136146 np .array (data , dtype = dtype )
137147
138148
149+ @pytest .mark .parametrize (
150+ ("strings" ),
151+ [
152+ ["this" , "is" , "an" , "array" ],
153+ ["€" , "" , "😊" ],
154+ ["A¢☃€ 😊" , " A☃€¢😊" , "☃€😊 A¢" , "😊☃A¢ €" ],
155+ ],
156+ )
157+ def test_self_casts (dtype , dtype2 , strings ):
158+ if hasattr (dtype , "na_object" ):
159+ strings = strings + [dtype .na_object ]
160+ arr = np .array (strings , dtype = dtype )
161+ newarr = arr .astype (dtype2 )
162+
163+ if hasattr (dtype , "na_object" ) and not hasattr (dtype2 , "na_object" ):
164+ assert newarr [- 1 ] == str (dtype .na_object )
165+ with pytest .raises (TypeError ):
166+ arr .astype (dtype2 , casting = "safe" )
167+ arr .astype (dtype2 , casting = "unsafe" )
168+ elif hasattr (dtype , "na_object" ) and hasattr (dtype2 , "na_object" ):
169+ assert newarr [- 1 ] is dtype2 .na_object
170+ arr .astype (dtype2 , casting = "safe" )
171+ else :
172+ arr .astype (dtype2 , casting = "safe" )
173+
174+ np .testing .assert_array_equal (arr [:- 1 ], newarr [:- 1 ])
175+
176+
139177@pytest .mark .parametrize (
140178 ("strings" ),
141179 [
0 commit comments