Skip to content

Commit c8eff39

Browse files
committed
COMPAT/PERF carefully allow multiple calls of to_array()
1 parent 2cb49fa commit c8eff39

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

pandas/_libs/hashtable_class_helper.pxi.in

+10-16
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ cdef class {{name}}Vector:
110110
cdef:
111111
ndarray ao
112112
cnp.npy_intp shape[1]
113-
if self.external_view_exists:
114-
raise ValueError("Vector.to_array() can only be called once")
115-
116-
self.data.data = <{{arg}}*> realloc(self.data.data, self.data.n * sizeof({{arg}}))
117-
self.data.m = self.data.n
113+
if self.data.m != self.data.n:
114+
if self.external_view_exists:
115+
raise ValueError("Vector.to_array() can only be called once")
116+
self.data.data = <{{arg}}*> realloc(self.data.data, self.data.n * sizeof({{arg}}))
117+
self.data.m = self.data.n
118118
self.external_view_exists = True
119119
shape[0] = self.data.n
120120
ao = cnp.PyArray_SimpleNewFromData(1, shape, {{idtype}}, <void*>self.data.data)
@@ -181,24 +181,17 @@ cdef class StringVector:
181181
ndarray ao
182182
size_t n
183183
object val
184-
if self.external_view_exists:
185-
raise ValueError("Vector.to_array() can only be called once")
186-
187184
ao = np.empty(self.data.n, dtype=np.object)
188185
for i in range(self.data.n):
189186
val = self.data.data[i]
190187
ao[i] = val
191-
self.data.m = self.data.n
192188
self.external_view_exists = True
193189
return ao
194190

195191
cdef inline void append(self, char * x):
196192

197193
if needs_resize(self.data):
198-
if self.external_view_exists:
199-
raise ValueError("external reference but Vector.resize() needed")
200194
self.resize()
201-
202195
append_data_string(self.data, x)
203196

204197

@@ -233,10 +226,11 @@ cdef class ObjectVector:
233226
self.n += 1
234227

235228
def to_array(self):
236-
if self.external_view_exists:
237-
raise ValueError("Vector.to_array() can only be called once")
238-
self.ao.resize(self.n, refcheck=False)
239-
self.m = self.n
229+
if self.m != self.n:
230+
if self.external_view_exists:
231+
raise ValueError("Vector.to_array() can only be called once")
232+
self.ao.resize(self.n, refcheck=False)
233+
self.m = self.n
240234
self.external_view_exists = True
241235
return self.ao
242236

0 commit comments

Comments
 (0)