@@ -48,6 +48,16 @@ def dtype(na_object, coerce):
48
48
return StringDType (coerce = coerce )
49
49
50
50
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
+
51
61
def test_dtype_creation ():
52
62
hashes = set ()
53
63
dt = StringDType ()
@@ -136,6 +146,34 @@ def test_scalars_string_conversion(data, dtype):
136
146
np .array (data , dtype = dtype )
137
147
138
148
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
+
139
177
@pytest .mark .parametrize (
140
178
("strings" ),
141
179
[
0 commit comments