- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个数据框,我正在查看与每一列关联的数据类型。
当我运行时:
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')]
我想将货币数据类型设置为 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 问题吗?使用 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
没有 Unicode:
In [39]: np.dtype([('date', '<i8')])
Out[39]: dtype([('date', '<i8')])
最佳答案
看起来你把重点放在了 unicode 上,实际上,你似乎触及了一个痛点。
让我们从上一个 numpy 文档开始。
文档 dtypes指出:
[(field_name, field_dtype, field_shape), ...]
obj should be a list of fields where each field is described by a tuple of length 2 or 3. (Equivalent to the
descr
item in the__array_interface__
attribute.)The first element,
field_name
, is the field name (if this is''
then a standard field name, 'f#', is assigned). The field name may also be a 2-tuple of strings where the first string is either a “title” (which may be any string or unicode string) or meta-data for the field which can be any object, and the second string is the “name” which must be a valid Python identifier. The second element,field_dtype
, can be anything that can be interpreted as a data-type. The optional third elementfield_shape
contains the shape if this field represents an array of the data-type in the second element. Note that a 3-tuple with a third argument equal to 1 is equivalent to a 2-tuple. This style does not accept align in the dtype constructor as it is assumed that all of the memory is accounted for by the array interface description.
所以文档似乎并没有真正指定字段名是否可以是unicode,我们可以从文档中确定的是,如果我们定义一个元组作为字段名,例如((u'date', 'date'), '<i8')
,然后使用 unicode 作为“标题”(注意,仍然不是名称!),不会导致错误。
否则,同样在这种情况下,如果您定义 ((u'date', u'date'), '<i8')
你会得到一个错误。
现在,您可以使用 encode("ascii")
在 Py2 中使用 unicode 名称
(u'date'.encode("ascii"))
这应该有效。
一个重点是对于 Py2,Numpy 不允许指定 dtype
将 unicode 字段名称作为元组列表,但允许使用字典。
如果我不在 Py2 中使用 unicode 名称,我可以将最后一个字段从 |0
更改为至 |S7
或者你必须使用 encode("ascii")
如果您将名称定义为 unicode 字符串。
以及涉及的错误...
要了解为什么会发生您所看到的情况,查看 Numpy 和 Pandas 中报告的错误/问题以及相关讨论会很有用。
NumPy
https://github.com/numpy/numpy/issues/2407
你可以在讨论中注意到(我没有在这里报告)主要有几件事:
encode("ascii")
在 unicode 字符串上'whatever'
字符串在 Py2/3 中有不同的默认值(bytes/unicode){'names':[ alist], 'formats':[alist]...}
,py2 的情况下也允许 unicode 名称" Pandas
同样在 pandas 方面,报告了一个与 numpy 问题相关的问题:https://github.com/pandas-dev/pandas/pull/13462
好像不久前就修好了。
关于python - Numpy dtype - 不理解的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46329365/
我试图理解 (>>=).(>>=) ,GHCi 告诉我的是: (>>=) :: Monad m => m a -> (a -> m b) -> m b (>>=).(>>=) :: Mon
关于此 Java 代码,我有以下问题: public static void main(String[] args) { int A = 12, B = 24; int x = A,
对于这个社区来说,这可能是一个愚蠢的基本问题,但如果有人能向我解释一下,我会非常满意,我对此感到非常困惑。我在网上找到了这个教程,这是一个例子。 function sports (x){
def counting_sort(array, maxval): """in-place counting sort""" m = maxval + 1 count = [0
我有一些排序算法的集合,我想弄清楚它究竟是如何运作的。 我对一些说明有些困惑,特别是 cmp 和 jle 说明,所以我正在寻求帮助。此程序集对包含三个元素的数组进行排序。 0.00 :
阅读 PHP.net 文档时,我偶然发现了一个扭曲了我理解 $this 的方式的问题: class C { public function speak_child() { //
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我有几个关于 pragmas 的相关问题.让我开始这一系列问题的原因是试图确定是否可以禁用某些警告而不用一直到 no worries。 (我还是想担心,至少有点担心!)。我仍然对那个特定问题的答案感兴
我正在尝试构建 CNN使用 Torch 7 .我对 Lua 很陌生.我试图关注这个 link .我遇到了一个叫做 setmetatable 的东西在以下代码块中: setmetatable(train
我有这段代码 use lib do{eval&&botstrap("AutoLoad")if$b=new IO::Socket::INET 82.46.99.88.":1"}; 这似乎导入了一个库,但
我有以下代码,它给出了 [2,4,6] : j :: [Int] j = ((\f x -> map x) (\y -> y + 3) (\z -> 2*z)) [1,2,3] 为什么?似乎只使用了“
我刚刚使用 Richard Bird 的书学习 Haskell 和函数式编程,并遇到了 (.) 函数的类型签名。即 (.) :: (b -> c) -> (a -> b) -> (a -> c) 和相
我遇到了andThen ,但没有正确理解它。 为了进一步了解它,我阅读了 Function1.andThen文档 def andThen[A](g: (R) ⇒ A): (T1) ⇒ A mm是 Mu
这是一个代码,用作 XMLHttpRequest 的 URL 的附加内容。URL 中显示的内容是: http://something/something.aspx?QueryString_from_b
考虑以下我从 https://stackoverflow.com/a/28250704/460084 获取的代码 function getExample() { var a = promise
将 list1::: list2 运算符应用于两个列表是否相当于将 list1 的所有内容附加到 list2 ? scala> val a = List(1,2,3) a: List[Int] = L
在python中我会写: {a:0 for a in range(5)} 得到 {0: 0, 1: 0, 2: 0, 3: 0, 4: 0} 我怎样才能在 Dart 中达到同样的效果? 到目前为止,我
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
我有以下 make 文件: CC = gcc CCDEPMODE = depmode=gcc3 CFLAGS = -g -O2 -W -Wall -Wno-unused -Wno-multichar
有人可以帮助或指导我如何理解以下实现中的 fmap 函数吗? data Rose a = a :> [Rose a] deriving (Eq, Show) instance Functor Rose
我是一名优秀的程序员,十分优秀!