我有一个数据框,我正在查看与每列关联的数据类型。
当我跑步时:
In [23]: df.dtype.descr
Out [24]: [(u'date', '<i8'), (u'open', '<f8'), (u'high', '<f8'), (u'low', '<f8'), (u'close', '<f8'), (u'volume', '<f8'), (u'dividend', '<f8'), (u'adj_factor', '<f8'), (u'split_factor', '<f8'), (u'liq', '<f8'), (u'currency', '|O')]
我想将货币 dtype 设置为 S7。我在做:
In [25]: dtype_new[-1] = (u'currency', "|S7")
In [26]: print dtype_new
Out [27]: [(u'date', '<i8'), (u'open', '<f8'), (u'high', '<f8'), (u'low', '<f8'), (u'close', '<f8'), (u'volume', '<f8'), (u'dividend', '<f8'), (u'adj_factor', '<f8'), (u'split_factor', '<f8'), (u'liq', '<f8'), (u'currency', '|S7')]
它看起来是正确的格式。所以我尝试将其放回我的 df:
In [28]: df = df.astype(np.dtype(dtype_new))
我收到错误:
TypeError('data type not understood',)
我应该改变什么?谢谢。在我最近更新 anaconda 之前,这是有效的,我不知道这个问题。谢谢。
调整:
df.dtype 是
In [23]: records.dtype
Out[23]: dtype((numpy.record, [(u'date', '<i8'), (u'open', '<f8'), (u'high', '<f8'), (u'low', '<f8'), (u'close', '<f8'), (u'volume', '<f8'), (u'dividend', '<f8'), (u'adj_factor', '<f8'), (u'split_factor', '<f8'), (u'liq', '<f8'), (u'currency', 'O')]))
如何将“0”更改为小于 7 个字符的字符串?
如何将最后一个数据类型从“O”更改为其他类型?具体来说是少于 7 个字符的字符串。
最后 - 这是一个 unicode 问题吗?
使用统一码:
In [38]: np.dtype([(u'date', '<i8')])
...:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-38-8702f0c7681f> in <module>()
----> 1 np.dtype([(u'date', '<i8')])
TypeError: data type not understood
无统一码:
In [39]: np.dtype([('date', '<i8')])
Out[39]: dtype([('date', '<i8')])