当将I插入为F时,kdb类型错误

时间:2022-10-19 04:26:14

I have a certain file type that contains a column with floats which I read in using insert

我有一个特定的文件类型,它包含一个带有浮动的列,我使用insert来读取它

`table insert ("TISISIFIIIFFIbIFFFFFFIIIFIIFFFFIIIIIIIIIIIIFFFFFFIIFFIIIIFFIIIIIIIIIIIIIIIII"; enlist "\t" ) 0:`:my_file.txt

unfortunately, sometimes the values in column happen to be all integers and in the txt file the are saved as ints, not floats, so as 1 instead of 1.0 and it seems kdb is throwing a type error. Is there a way make kdb accept ints saved in that format as floats?

不幸的是,有时列中的值碰巧都是整数,在txt文件中,这些值被保存为int,而不是float,所以从1到1.0,kdb似乎抛出了一个类型错误。kdb是否有办法接受以那种格式保存为浮点数的ints ?

I do have a lot of columns with floats and theoretically, the problem can appear in any of them. Is there some way to tell kdb on insert to treat any int as float if the column type is float?

我确实有很多有浮动的列理论上,问题可以出现在它们中的任何一个。如果列类型为float,是否有办法告诉kdb在insert时将任何int类型作为float来处理?

1 个解决方案

#1


2  

The 'type error is actually happening from your insert. You are trying to insert some parsed data, but the types of each column are not conforming to the types of each column in 'table'. You are basically saying that your raw data can contain floats, therefore you are going to have to read them in as floats. What you do with that column after the parse is up to you though.

“类型错误实际上发生在插入中。您正在尝试插入一些经过解析的数据,但是每个列的类型与“表”中的每个列的类型不一致。您基本上是说原始数据可以包含浮点数,因此您将不得不将它们作为浮点数来读取。不过,解析后对该列做什么取决于您。

1) keep as floats, read in as floats, insert as floats, column should be a float in 'table' pre-read (I presume this is what you want going by your question):

1)保持为float, read in as float, insert as float, column should be a float in in in 'table' pre-read(我猜这就是你要问的):

update "f"$COLUMN from `table
`table insert (1#"F";1#"\t") 0:`myfile.txt   

2) update to an integer and then insert into 'table' - you are going to have update the schema of table first, read in as floats, and then run an update after every read:

2)更新到一个整数,然后插入到“table”中——您将首先更新表的模式,作为浮点数读取,然后在每次读取后运行更新:

update "i"$COLUMN from `table
`table insert update "i"COLUMN from (1#"F";1#"\t") 0:`myfile.txt

Another option you may want to consider, but please test first as it may replace too much, is to replace the trailing ".0" from your floats, and then just read in as integers:

另一个您可能想要考虑的选项,但是请先测试,因为它可能会替换太多,那就是替换尾随”。0"从浮点数中,然后以整数的形式读入:

q)\cd /var/tmp
q)`:myfile.txt 0:("x\tx1";"1.0\t2.0";"3.0\t1.0")
q)\sed -i -e 's/.0//g' myfile.txt
q)("II";1#"\t")0:`myfile.txt

#1


2  

The 'type error is actually happening from your insert. You are trying to insert some parsed data, but the types of each column are not conforming to the types of each column in 'table'. You are basically saying that your raw data can contain floats, therefore you are going to have to read them in as floats. What you do with that column after the parse is up to you though.

“类型错误实际上发生在插入中。您正在尝试插入一些经过解析的数据,但是每个列的类型与“表”中的每个列的类型不一致。您基本上是说原始数据可以包含浮点数,因此您将不得不将它们作为浮点数来读取。不过,解析后对该列做什么取决于您。

1) keep as floats, read in as floats, insert as floats, column should be a float in 'table' pre-read (I presume this is what you want going by your question):

1)保持为float, read in as float, insert as float, column should be a float in in in 'table' pre-read(我猜这就是你要问的):

update "f"$COLUMN from `table
`table insert (1#"F";1#"\t") 0:`myfile.txt   

2) update to an integer and then insert into 'table' - you are going to have update the schema of table first, read in as floats, and then run an update after every read:

2)更新到一个整数,然后插入到“table”中——您将首先更新表的模式,作为浮点数读取,然后在每次读取后运行更新:

update "i"$COLUMN from `table
`table insert update "i"COLUMN from (1#"F";1#"\t") 0:`myfile.txt

Another option you may want to consider, but please test first as it may replace too much, is to replace the trailing ".0" from your floats, and then just read in as integers:

另一个您可能想要考虑的选项,但是请先测试,因为它可能会替换太多,那就是替换尾随”。0"从浮点数中,然后以整数的形式读入:

q)\cd /var/tmp
q)`:myfile.txt 0:("x\tx1";"1.0\t2.0";"3.0\t1.0")
q)\sed -i -e 's/.0//g' myfile.txt
q)("II";1#"\t")0:`myfile.txt