Skip to content

Non-string columns should get casted to string in DataFrame.to_records #822

@wesm

Description

@wesm

data like this

In [36]: df
Out[36]: 
        0       1        2       3       4       5       6       7       8       9
0  2.1670  1.7100  0.61340  3.0150  2.5060  0.6787  1.6970  4.0460  1.2010  0.6134
1  1.2920  0.6526  0.33930  0.9789  1.9320  0.3915  0.6526  1.9710  1.0310  0.5221
2  0.4176  0.3002  0.01305  0.3132  0.5351  0.1566  0.2088  0.5612  0.1436  0.2349
3  3.2760  1.0700  0.52210  1.2140  1.8010  0.6917  1.7620  2.3360  0.7961  0.6134
4  2.2840  2.4800  0.41760  1.4100  1.8010  0.5221  0.8092  3.8370  1.1490  0.5743
5  0.4699  0.2349  0.26100  0.3393  0.4699  0.1044  0.2088  0.6526  0.3915  0.1175
6  1.6310  0.8222  0.30020  1.2920  1.1090  0.1697  0.4960  1.1350  0.4568  0.5743
7  3.8760  2.0100  0.53510  2.2840  3.5110  0.8875  1.2010  2.3100  1.2790  0.8222
8  0.7831  0.9397  0.18270  0.4960  0.6265  0.4046  0.3524  0.7178  0.3132  0.1436
9  0.8353  0.4829  0.28710  0.6265  0.7570  0.2219  0.5351  0.6787  0.3002  0.1044

and

In [35]: df.to_records()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/wesm/Dropbox/book/svn/<ipython-input-35-6d3142e97d2d> in <module>()
----> 1 df.to_records()

/home/wesm/code/pandas/pandas/core/frame.pyc in to_records(self, index)
    716             names = list(self.columns)
    717 
--> 718         return np.rec.fromarrays(arrays, names=names)
    719 
    720     @classmethod

/home/wesm/epd/lib/python2.7/site-packages/numpy/core/records.pyc in fromarrays(arrayList, dtype, shape, formats, names, titles, aligned, byteorder)
    540         _names = descr.names
    541     else:
--> 542         parsed = format_parser(formats, names, titles, aligned, byteorder)
    543         _names = parsed._names
    544         descr = parsed._descr

/home/wesm/epd/lib/python2.7/site-packages/numpy/core/records.pyc in __init__(self, formats, names, titles, aligned, byteorder)
    142     def __init__(self, formats, names, titles, aligned=False, byteorder=None):
    143         self._parseFormats(formats, aligned)
--> 144         self._setfieldnames(names, titles)
    145         self._createdescr(byteorder)
    146         self.dtype = self._descr

/home/wesm/epd/lib/python2.7/site-packages/numpy/core/records.pyc in _setfieldnames(self, names, titles)
    177                 raise NameError, "illegal input names %s" % `names`
    178 
--> 179             self._names = [n.strip() for n in names[:self._nfields]]
    180         else:
    181             self._names = []

AttributeError: 'numpy.int64' object has no attribute 'strip'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions