Skip to content

Commit 2babd1b

Browse files
committed
BUG: handle length-0 objects in Series.to_string, GH #488
1 parent 2bd4c00 commit 2babd1b

File tree

4 files changed

+13
-27
lines changed

4 files changed

+13
-27
lines changed

pandas/core/series.py

+3
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,9 @@ def to_string(self, buf=None, na_rep='NaN', float_format=None, nanRep=None,
471471

472472
def _get_repr(self, name=False, print_header=False, length=True,
473473
na_rep='NaN', float_format=None):
474+
if len(self) == 0:
475+
return ''
476+
474477
vals = self.values
475478
index = self.index
476479

pandas/src/tseries.pyx

-27
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,6 @@ cdef inline int int_min(int a, int b): return a if a <= b else b
2222

2323
ctypedef unsigned char UChar
2424

25-
cdef int is_contiguous(ndarray arr):
26-
return np.PyArray_CHKFLAGS(arr, np.NPY_C_CONTIGUOUS)
27-
28-
cdef int _contiguous_check(ndarray arr):
29-
if not is_contiguous(arr):
30-
raise ValueError('Tried to use data field on non-contiguous array!')
31-
32-
cdef int16_t *get_int16_ptr(ndarray arr):
33-
_contiguous_check(arr)
34-
35-
return <int16_t *> arr.data
36-
37-
cdef int32_t *get_int32_ptr(ndarray arr):
38-
_contiguous_check(arr)
39-
40-
return <int32_t *> arr.data
41-
42-
cdef int64_t *get_int64_ptr(ndarray arr):
43-
_contiguous_check(arr)
44-
45-
return <int64_t *> arr.data
46-
47-
cdef double_t *get_double_ptr(ndarray arr):
48-
_contiguous_check(arr)
49-
50-
return <double_t *> arr.data
51-
5225
cimport util
5326

5427
cdef extern from "math.h":

pandas/src/util.pxd

+3
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ cdef inline set_value_at(ndarray arr, object loc, object value):
4343
raise IndexError('index out of bounds')
4444

4545
assign_value_1d(arr, i, value)
46+
47+
cdef inline int is_contiguous(ndarray arr):
48+
return cnp.PyArray_CHKFLAGS(arr, cnp.NPY_C_CONTIGUOUS)

pandas/tests/test_series.py

+7
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,13 @@ def test_to_string(self):
539539
expected = [format(x) for x in self.ts]
540540
self.assertEqual(result, expected)
541541

542+
# empty string
543+
result = self.ts[:0].to_string()
544+
self.assertEqual(result, '')
545+
546+
result = self.ts[:0].to_string(length=0)
547+
self.assertEqual(result, '')
548+
542549
def test_iter(self):
543550
for i, val in enumerate(self.series):
544551
self.assertEqual(val, self.series[i])

0 commit comments

Comments
 (0)