python中利用空表新建分区数据库

如何根据空表用python API建立空的分区数据库,如下面的dolphindb脚本:

login("admin","123456")

dbPath="dfs://yearRange"

if (existsDatabase(dbPath))

dropDatabase(dbPath)

yearRange=date(2005.01M + 12*0..25)

db=database(dbPath,RANGE,yearRange)

cols = `ts_code`suspend_date`suspend_type

types = `STRING`DATETIME`INT

tbl = db.createPartitionedTable(table(1:0,cols,types),`tbl,`suspend_date)

如何在python api中不用s.run进行实现

请先 登录 后评论

1 个回答

JAY
可以是用如下的python脚本来实现,主要就是加粗部分的代码在python中定义了格式:
import dolphindb as ddb
import dolphindb.settings as keys
import numpy as np

s= ddb.session()
s.connect(server, port, "admin", "123456")
dbPath="dfs://yearRange"
yearRange = np.array(np.arange('2005-01','2030-12',step= 12,dtype='datetime64[M]'),dtype='datetime64[D]')
if s.existsDatabase(dbPath):
    s.dropDatabase(dbPath)
db = s.database(dbName='mydb',partitionType=keys.RANGE,partitions=yearRange,dbPath=dbPath)

// defination of data type 
data_type = np.dtype([('ts_code',np.str,40),('suspend_date', np.datetime64),('suspend_type',np.int32)])
data_np = np.array([],dtype=data_type)
df = pd.DataFrame(data_np)

// upload local dataframe to database
t = s.table(data = df)

// creat empty partitioned table in database
pt1 = db.createPartitionedTable(table=t, tableName='pt1', partitionColumns=['suspend_date'])
请先 登录 后评论