本文的目的是介绍python–使用executemany()插入行时出现“无效的参数类型”(numpy.int64)的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于c#–
本文的目的是介绍python – 使用executemany()插入行时出现“无效的参数类型”(numpy.int64)的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于c# – 为什么Convert.ToDateTime(Int64)失败?、c# – 这是“Int64”代理的原子吗?、Dask:不能安全地将非等效的 float64 转换为 int64,但只有 object 和 int64 列、DatabaseGeneratedOption.Computed with Int64 不工作的知识。
本文目录一览:- python – 使用executemany()插入行时出现“无效的参数类型”(numpy.int64)
- c# – 为什么Convert.ToDateTime(Int64)失败?
- c# – 这是“Int64”代理的原子吗?
- Dask:不能安全地将非等效的 float64 转换为 int64,但只有 object 和 int64 列
- DatabaseGeneratedOption.Computed with Int64 不工作
python – 使用executemany()插入行时出现“无效的参数类型”(numpy.int64)
insert_list = [(1,1,1),(2,2,2),(3,3,3),....] #up to 10000 tuples in this list conn = pyodbc.connect('DRIVER={FreeTDS};SERVER=xxxxx;DATABASE=xxxx;UID=xx;PWD=xx;TDS_Version=7.0') cursor = conn.cursor() sql = "insert into ScanEMAxEMAHistoryDay(SecurityNumber,EMA1,EMA2,Crosstype,DayCross,IsLocalMinMax) values (?,?,?)" cursor.executemany(sql,insert_list)
cursor.executemany(sql,insert_list)
pyodbc.ProgrammingError: (‘Invalid parameter type. param-index=4 param-type=numpy.int64’,‘HY105’)
减少到100元组:
cursor.executemany(sql,insert_list[:100])
cursor.executemany(sql,insert_list[:100])
pyodbc.ProgrammingError: (‘Invalid parameter type. param-index=4 param-type=numpy.int64’,‘HY105’)
cursor.executemany(sql,insert_list[:100])
减少到5元组:
cursor.executemany(sql,insert_list[:5]) conn.commit()
这可以插入数据库
我试着:
sql = 'SET GLOBAL max_allowed_packet=50*1024*1024' cursor.execute(sql)
在excutemany()之前,它有一个错误:
pyodbc.ProgrammingError: (‘42000′,“[42000] [FreeTDS][sql Server]’GLOBAL’ is not a recognized SET option. (195) (sqlExecDirectW)”)
我是怎么解决这个问题的
谢谢.
解决方法
a = numpy.array([10,11,12],dtype=numpy.int64) params = (1,a[1],1) crsr.execute(sql,params)
会扔
ProgrammingError: (‘Invalid parameter type. param-index=2 param-type=numpy.int64’,‘HY105’)
因为第三个参数值是numpy数组a中的numpy.int64元素.使用int()转换该值将避免此问题:
a = numpy.array([10,int(a[1]),params)
顺便说一句,原因
sql = 'SET GLOBAL max_allowed_packet=50*1024*1024' cursor.execute(sql)
没有用的是max_allowed_packet是MysqL设置,对Microsoft sql Server没有任何意义.
c# – 为什么Convert.ToDateTime(Int64)失败?
long oldDate=new DateTime(2015,1,1).Ticks; DateTime newDate=Convert.ToDateTime(oldDate);
.Ticks是long / Int64,Convert.ToDateTime(Int64)MSDN文档显示接受long / Int64的方法.
public static DateTime ToDateTime( long value )
编辑:
如下面的ebyrob所指出的那样应该是:
long oldDate=new DateTime(2015,1).Ticks; DateTime newDate=new DateTime(oldDate);
解决方法
Calling this method always throws InvalidCastException.
和
Return Value
Type: System.DateTime
This conversion is not supported. No value is returned.
我不确定为什么不支持这一点,特别是如果新的DateTime(oldDate)运行良好:
DateTime newDate = new DateTime(oldDate);
c# – 这是“Int64”代理的原子吗?
我知道,只要涉及的数据可以适合双字(32位),原子性就应该得到保证.无论操作系统是32位还是64位.
现在,考虑以下“长”代理人……
注意:我省略了几种方法,因为我不需要它们.在这种情况下,我只需要基本转换为/从真正的长.
public struct SafeLong : IConvertible { public SafeLong(long value) { unchecked { var arg = (ulong)value; this._data = new byte[] { (byte)arg,(byte)(arg >> 8),(byte)(arg >> 16),(byte)(arg >> 24),(byte)(arg >> 32),(byte)(arg >> 40),(byte)(arg >> 48),(byte)(arg >> 56),}; } } private byte[] _data; private long Value { get { unchecked { var lo = this._data[0] | this._data[1] << 8 | this._data[2] << 16 | this._data[3] << 24; var hi = this._data[4] | this._data[5] << 8 | this._data[6] << 16 | this._data[7] << 24; return (long)((uint)lo | (ulong)(uint)hi << 32); } } } public static implicit operator long(SafeLong value) { return value.Value; // implicit conversion } public static explicit operator SafeLong(long value) { return new SafeLong(value); // explicit conversion } #region IConvertible implementation public TypeCode GetTypeCode() { return Type.GetTypeCode(typeof(SafeLong)); } public object ToType(Type conversionType,IFormatProvider provider) { return Convert.ChangeType(this.Value,conversionType); } public long ToInt64(IFormatProvider provider) { return this.Value; } // ... OMISSIS (not implemented) ... #endregion }
好吧,它看起来像我期望的那样完美.
这是一个小测试:
class Program { static void Main(string[] args) { var sla = (SafeLong)12345678987654321L; var la = (long)sla; Console.WriteLine(la); var slb = (SafeLong)(-998877665544332211L); var lb = (long)slb; Console.WriteLine(lb); Console.WriteLine(Marshal.SizeOf(typeof(SafeLong))); Console.WriteLine(Marshal.SizeOf(sla)); Console.WriteLine(Marshal.SizeOf(slb)); long lc = new SafeLong(556677); var slc = slb; Console.WriteLine(slc); slc = (SafeLong)lc; Console.WriteLine(slc); Console.WriteLine(slb); Console.Write("Press any key..."); Console.ReadKey(); } }
SizeOf函数总是产生4个字节作为我的代理的大小.这个值是否保证了SafeLong-to-SafeLong副本的原子性,或者4个字节应该被解释为“真正的物理双字”?
无论长< - >的非原子性如何. SafeLong:它将被包含在安全的上下文中.
非常感谢提前.
解决方法
public struct SafeThing<T> where T : struct { private object BoxedThing; public SafeThing(T value) { BoxedThing = value; } public T Value { get { return BoxedThing == null ? default(T) : (T)BoxedThing; } } public static implicit operator T(SafeThing<T> value) { return value.Value; } public static implicit operator SafeThing<T>(T value) { return new SafeThing(value); } }
而且你已经完成了.你为什么要用阵列搞乱这一切?
另外,我注意到在您的实现中,您已经向后转换了显式/隐式转换.如果转换无损且不抛出,则只应隐式转换.您从SafeLong到long的隐式转换可以抛出,因此它不应该是隐式的.你从long到SafeLong的显式转换不能抛出并且是无损的,所以如果你想要的话它可能是隐含的.正如你所看到的,我已经通过使两个方向无损且无投掷来解决我的实现中的问题,因此它们都是隐含的.
请注意,此结构本质上是一个围绕盒装值的强类型包装器;如果在CLR的第一个版本中可以使用泛型类型,毫无疑问,盒装值类型将使用某种类型的类型实现,就像可空值类型类似地通过特殊泛型类型实现一样.
The SizeOf function yields always 4 bytes as the size of my surrogate. Does this value guarantees the atomicity of a copy SafeLong-to-SafeLong?
嗯,是的,不.
首先,“SizeOf”方法不会在内存中给出结构的大小;它在跨越托管/非托管边界持久化时给出结构的大小.这不一定与托管内存中结构的大小相同;它通常是相同的,但不保证是相同的.如果您想知道托管内存中结构的大小,则需要打开“不安全”模式并使用“sizeof”运算符.
实际上,大小为4的结构的副本总是原子的,只要它被复制到四字节边界上对齐的位置即可.语言规范并不保证任何四字节结构将以原子方式复制,但实际上在我们的实现中也是如此.
在您的特定情况下,这四个字节是对数组的引用;语言规范确保始终以原子方式复制引用.
Dask:不能安全地将非等效的 float64 转换为 int64,但只有 object 和 int64 列
如何解决Dask:不能安全地将非等效的 float64 转换为 int64,但只有 object 和 int64 列?
我尝试阅读和处理 60 列 (150MB) 的 CSV 文件:
df = dataframe.read_csv(
path,**dialect,dtype=dtype,encoding=encoding,storage_options=storage_options,)
但是当我尝试 len(df)
时,它会引发 TypeError: cannot safely cast non-equivalent float64 to int64
但是,在 dtype
中,我只为我的列定义指定了 Int64
和 object
类型。
这个浮点转换从何而来?
编辑:经过调查,问题与定义为 int 的列有关,其中包含(在 CSV 中很远)一些字符串值。错误显然不明确
编辑:堆栈跟踪
Traceback (most recent call last):
File "/home/benjamin/try/try-back/apps/data/tasks.py",line 168,in csv_to_parquet_task
df.to_parquet(
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/dask/dataframe/core.py",line 3974,in to_parquet
return to_parquet(self,path,*args,**kwargs)
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/dask/dataframe/io/parquet/core.py",line 508,in to_parquet
out = out.compute(**compute_kwargs)
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/dask/base.py",line 167,in compute
(result,) = compute(self,traverse=False,**kwargs)
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/dask/base.py",line 452,in compute
results = schedule(dsk,keys,**kwargs)
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/dask/threaded.py",line 76,in get
results = get_async(
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/dask/local.py",line 486,in get_async
raise_exception(exc,tb)
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/dask/local.py",line 316,in reraise
raise exc
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/dask/local.py",line 222,in execute_task
result = _execute_task(task,data)
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/dask/core.py",line 121,in _execute_task
return func(*(_execute_task(a,cache) for a in args))
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/dask/core.py",in <genexpr>
return func(*(_execute_task(a,cache) for a in args))
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/dask/dataframe/io/csv.py",line 150,in pandas_read_text
df = reader(bio,**kwargs)
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/pandas/io/parsers.py",line 688,in read_csv
return _read(filepath_or_buffer,kwds)
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/pandas/io/parsers.py",line 460,in _read
data = parser.read(nrows)
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/pandas/io/parsers.py",line 1198,in read
ret = self._engine.read(nrows)
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/pandas/io/parsers.py",line 2157,in read
data = self._reader.read(nrows)
File "pandas/_libs/parsers.pyx",line 847,in pandas._libs.parsers.TextReader.read
File "pandas/_libs/parsers.pyx",line 862,in pandas._libs.parsers.TextReader._read_low_memory
File "pandas/_libs/parsers.pyx",line 941,in pandas._libs.parsers.TextReader._read_rows
File "pandas/_libs/parsers.pyx",line 1073,in pandas._libs.parsers.TextReader._convert_column_data
File "pandas/_libs/parsers.pyx",line 1104,in pandas._libs.parsers.TextReader._convert_tokens
File "pandas/_libs/parsers.pyx",line 1182,in pandas._libs.parsers.TextReader._convert_with_dtype
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/pandas/core/arrays/integer.py",line 385,in _from_sequence_of_strings
return cls._from_sequence(scalars,dtype,copy)
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/pandas/core/arrays/integer.py",line 378,in _from_sequence
return integer_array(scalars,copy=copy)
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/pandas/core/arrays/integer.py",line 160,in integer_array
values,mask = coerce_to_array(values,line 275,in coerce_to_array
values = safe_cast(values,copy=False)
File "/home/benjamin/.cache/pypoetry/virtualenvs/try-dKznE2K2-py3.8/lib/python3.8/site-packages/pandas/core/arrays/integer.py",line 179,in safe_cast
raise TypeError(
TypeError: cannot safely cast non-equivalent float64 to int64
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
DatabaseGeneratedOption.Computed with Int64 不工作
如何解决DatabaseGeneratedOption.Computed with Int64 不工作?
我想让数据库自动生成 Ancora_id 属性。我使用了数据注释,但它不起作用。我将向您展示 Ancora 模型和 Db_context 中的代码。
字段 ancora_id 它不是我的表的主键,作为主键,我有另一个名为 Id 的 Guid 类型的字段。
谁能告诉我我哪里错了?
public partial class Ancore: EntityDb
{
[Key]
public Guid Id {get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public Int64 Ancora_id { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("Relational:Collation","latin1_General_CI_AS");
modelBuilder.Entity<Ancore>(entity =>
{
entity.ToTable("Ancore");
entity.Property(e => e.Id)
.ValueGeneratednever()
.HasColumnName("id");
entity.Property(e => e.Ancora_id).HasColumnName("ancora_id");
});
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
我们今天的关于python – 使用executemany()插入行时出现“无效的参数类型”(numpy.int64)的分享就到这里,谢谢您的阅读,如果想了解更多关于c# – 为什么Convert.ToDateTime(Int64)失败?、c# – 这是“Int64”代理的原子吗?、Dask:不能安全地将非等效的 float64 转换为 int64,但只有 object 和 int64 列、DatabaseGeneratedOption.Computed with Int64 不工作的相关信息,可以在本站进行搜索。
本文标签: