replaceColumn!
Syntax
replaceColumn!(table, colName, newCol)
Arguments
table is an in-memory table or a DFS table.
colName a string indicating a column name.
newCol a vector with the same number elements as the number of rows of table.
Details
Replace a table column with the specified vector. The data type of the new column is the same as the data type of the specified vector. The original column name is not changed.
To update column’s value without changing its data type, we can use both replaceColumn!
and SQL statement update. Only replaceColumn!
, however, can change a column’s data type.
Examples
$ sym = `C`MS`MS`MS`IBM`IBM`C`C`C
$ price= 49.6 29.46 29.52 30.02 174.97 175.23 50.76 50.32 51.29
$ qty = 2200 1900 2100 3200 6800 5400 1300 2500 8800
$ timestamp = [09:34:07,09:36:42,09:36:51,09:36:59,09:32:47,09:35:26,09:34:16,09:34:26,09:38:12]
$ t = table(timestamp, sym, qty, price)
$ schema(t).colDefs;
name |
typeString |
typeInt |
comment |
---|---|---|---|
timestamp |
SECOND |
10 |
|
sym |
STRING |
18 |
|
qty |
INT |
4 |
|
price |
DOUBLE |
16 |
To change the data type of column “sym” to SYMBOL:
$ syms=symbol(exec sym from t)
$ replaceColumn!(t,`sym,syms);
$ schema(t).colDefs;
name |
typeString |
typeInt |
comment |
---|---|---|---|
timestamp |
SECOND |
10 |
|
sym |
SYMBOL |
17 |
|
qty |
INT |
4 |
|
price |
DOUBLE |
16 |
$ login("admin","123456")
$ if(existsDatabase("dfs://replaceColumn")){
$ dropDatabase("dfs://replaceColumn")
$ }
$ n=10
$ month=take(2012.06.13..2012.06.13, n);
$ x=rand(1.0, n);
$ t=table(month, x);
$ db=database("dfs://replaceColumn", VALUE, 2012.06.13..2012.06.23)
$ pt = db.createPartitionedTable(t, `pt, `month);
$ pt.append!(t);
$ schema(pt).colDefs
name |
typeString |
typeInt |
extra |
comment |
---|---|---|---|---|
month |
DATE |
6 |
||
x |
DOUBLE |
16 |
$ newCols=int(exec x from loadTable("dfs://replaceColumn",`pt))
$ replaceColumn!(loadTable("dfs://replaceColumn",`pt), `x, newCols)
$ schema(pt).colDefs
name |
typeString |
typeInt |
extra |
comment |
---|---|---|---|---|
month |
DATE |
6 |
||
x |
INT |
4 |