- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
自从(我认为)升级到 python 3.5(从 2.7)以来,我在一些非常基本的代码中遇到了奇怪的错误
当尝试打开一个文件(充满位字符串)并且它们像这样操作字符串时:
#bit string data
data = open(read_path+'genomes'+str(time)).read().replace(',','\n').replace('\n','')
x = data.split()
CA = np.array(x).astype('string')
Genomes = np.reshape(CA, (size,size))
genomelength = len(Genomes[0][0])
for entry in range(0, size**2): total_mut1[entry] = np.array(sum_digits(CA[entry])).astype('int')
mut_array1 = np.reshape(total_mut1, (size,size))
升级前有效...
我现在收到此错误:
CA = np.array(x).astype('string')
TypeError: data type "string" not understood
这只是一个愚蠢的修复吗(我希望如此)。提前致谢。
最佳答案
以下是有关 dtype 选项的信息: http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html
具体:
Several python types are equivalent to a corresponding array scalar when used to generate a dtype object:
int int_
bool bool_
float float_
complex cfloat
str string
unicode unicode_
buffer void
(all others) object_
正如上面的评论所建议的,.astype(str) 应该可以工作。
编辑
实际上这个信息取自Python2.7,我也尝试过这个,unicode不起作用,但是astype(str)默认为unicode(正如在python3中所期望的那样)。不过很有趣,因为这些字符串代码似乎有效:
'b' boolean
'i' (signed) integer
'u' unsigned integer
'f' floating-point
'c' complex-floating point
'O' (Python) objects
'S', 'a' (byte-)string
'U' Unicode
'V' raw data (void)
有关 python3 与 python2 中字符串的信息(可能是最重要的变化)如下:
关于python - 他们在 python 3 中更改了 astype ('string' ) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34758641/
我有一个 DataFrame,其中一列由浮点型对象组成。我想转换为 int。正如我所尝试的,.astype("int") 和 astype(int) 方法都有效。我只是想知道是否有任何区别? 最佳答案
我有一个简单的数据框 df: col1 | col2 7 | 8 12 | 14 当我通过写 df.dtypes 检查 df 的数据类型时,int64 是两列的数据类型。 现在我要通过键
我目前正在使用 Anaconda 的 Spyder,我正在尝试将包含 float 类型的数组转换为 int 类型: x = np.array([1, 2, 2.5]) x.astype(int) pr
使用此简单的代码行,我会不断遇到SettingWithCopyWarning错误,而不是整个代码。 #make email a string df['Email Address'] = df['Ema
给定一系列类型 object我想知道使用 .astype() 可以成功将多少元素转换为给定类型功能。 例如,尝试将以下系列转换为 float32 : s = pd.Series([1.1, 'foo'
我正在使用 timedeltas,似乎这段代码 copy_for_U.Time.astype('timedelta64[m]',copy=False); 不会更改数据框 - 如果我从文档中正确理解,它
看来 Groovy 中转换对象的约定是使用 as 运算符并覆盖 asType()。例如: class Id { def value @Override public Objec
考虑以下示例: df = pd.DataFrame([[1, "a"], [2, "b"]], columns=["int", "str"]) df.astype({"int":np.int8, "s
astype 在使用 dict of columns 时引发 ValueError。 我正在尝试将大 DF 中的稀疏列类型转换(从 float 到 int)。我的问题是 NaN 值。即使 errors
我正在尝试在数据框的列中查找中值。我得到的中值是 float ,但我需要它的整数格式。 c_med = round(df['count'].median().astype(int)) c_med =
我的DataFrame中有一列float64,需要将其转换为Int。我尝试了这两个代码,但数据类型保持不变(float64) final_df['Total'] = final_df.Total.as
所以我试图将我的 float numpy 数组转换为整数。但是当我这样做时: array.astype(np.uint64) 它改变了这个数组: [ 550. 514. 451. 494. 4
使用astype实现dataframe字段类型转换 ? 1
我尝试使用 round 然后 astype 将浮点 DataFrame 的某些列中的值转换为整数。但是,这些值仍然包含小数位。我的代码有什么问题? nums = np.arange(1, 11) ar
用Delphi编写 uses System.Classes; ... var A: TAlignment; Value: TValue; begin Value := 0; A :=
我正在尝试使用 RTTI 将事件处理程序添加到控件,该控件可能已经设置了事件处理程序。代码看起来像这样: var prop: TRttiProperty; val: TValue; begin
我有一个数字列,它可以包含另一个不同形式的字符 [0-9] .说:x = pandas.Series(["1","1.2", "*", "1", "**."]) . 然后我想使用 x.astype(d
当我尝试将 astype(float) 方法应用于 pandas 中的分组数据框时,出现以下错误。 ValueError: could not convert string to float: 你
我正在使用 geopy 包搜索地址的坐标,列返回匹配的地址和坐标 我只想获取坐标 这是一个向您展示其工作原理的测试: # Test to see if response is obtained for
我有一个二维 numpy 数组,我想将特定的 dtype 应用于每一列。 a = np.arange(25).reshape((5,5)) In [40]: a Out[40]: array([[ 0
我是一名优秀的程序员,十分优秀!