@@ -125,6 +125,41 @@ def __setitem__(self, item, val):
125125 .format (self .__class__ .__name__ ))
126126
127127
128+ def _expand_string_array_for_values (arr , values ):
129+ """
130+ For string-dtype return a version of ``arr`` that is wide enough for ``values``.
131+ If ``arr`` is not string-dtype or does not need expansion then return ``arr``.
132+
133+ Parameters
134+ ----------
135+ arr : np.ndarray
136+ Input array
137+ values : scalar or array-like
138+ Values for width comparison for string arrays
139+
140+ Returns
141+ -------
142+ arr_expanded : np.ndarray
143+
144+ """
145+ # If this is not a string type then just return original arr
146+ if arr .dtype .kind not in ('U' , 'S' ):
147+ return arr
148+
149+ # Find the length of the longest string in the new values.
150+ values_str_len = np .char .str_len (values ).max ()
151+
152+ # Determine character repeat count of self.dtype. Returns a positive
153+ # int or None (something like 'U0' is not possible in numpy). If new values
154+ # are longer than current then make a new (wider) version of self.
155+ arr_str_len = dtype_bytes_or_chars (arr .dtype )
156+ if arr_str_len and values_str_len > arr_str_len :
157+ arr_dtype = arr .dtype .byteorder + arr .dtype .kind + str (values_str_len )
158+ return arr .astype (arr_dtype )
159+ else :
160+ return arr
161+
162+
128163class ColumnInfo (BaseColumnInfo ):
129164 """
130165 Container for meta information like name, description, format.
@@ -1030,10 +1065,9 @@ def insert(self, obj, values, axis=0):
10301065 data = np .insert (self , obj , None , axis = axis )
10311066 data [obj ] = values
10321067 else :
1033- # Explicitly convert to dtype of this column. Needed because numpy 1.7
1034- # enforces safe casting by default, so . This isn't the case for 1.6 or 1.8+.
1035- values = np .asarray (values , dtype = self .dtype )
1036- data = np .insert (self , obj , values , axis = axis )
1068+ self_for_insert = _expand_string_array_for_values (self , values )
1069+ data = np .insert (self_for_insert , obj , values , axis = axis )
1070+
10371071 out = data .view (self .__class__ )
10381072 out .__array_finalize__ (self )
10391073 return out
@@ -1343,16 +1377,14 @@ def insert(self, obj, values, mask=None, axis=0):
13431377 new_data = np .insert (self_ma .data , obj , None , axis = axis )
13441378 new_data [obj ] = values
13451379 else :
1346- # Explicitly convert to dtype of this column. Needed because numpy 1.7
1347- # enforces safe casting by default, so . This isn't the case for 1.6 or 1.8+.
1348- values = np .asarray (values , dtype = self .dtype )
1380+ self_ma = _expand_string_array_for_values (self_ma , values )
13491381 new_data = np .insert (self_ma .data , obj , values , axis = axis )
13501382
13511383 if mask is None :
13521384 if self .dtype .kind == 'O' :
13531385 mask = False
13541386 else :
1355- mask = np .zeros (values .shape , dtype = bool )
1387+ mask = np .zeros (np . broadcast ( values ) .shape , dtype = bool )
13561388 new_mask = np .insert (self_ma .mask , obj , mask , axis = axis )
13571389 new_ma = np .ma .array (new_data , mask = new_mask , copy = False )
13581390
0 commit comments