Error occurred when importing DECIMAL data with Python Api

41 Views Asked by At

DolphinDB server version : 2.00.9.1 2023.02.20 ; linux64

python==3.7

DDB package version : 1.30.21.1

Problem Description:

I want to use ddb.tableAppender() to write a Python dataframe to a DolphinDB table. A column in the table is of type DECIMAL. But after uploading, the data is changed.

enter image description here

1

There are 1 best solutions below

0
YaN On BEST ANSWER

According to the Python API for DolphinDB (5.4.2), when uploading tables containing objects of Python decimal.Decimal(), you must ensure that all values in the columns of DECIMAL type have the same decimal digits. Otherwise, it will return a value with the same digits as the first row of value. Your first row of data has four decimal digits, so error will occur when you upload data with 3 decimal digits.

To solve the problem, you can try the following script:

price_list = [Decimal(str(round(random.uniform(1, 1000),4))).quantize(decimal.Decimal("0.0000")) for _ in range(10)] + [Decimal(str(round(random.uniform(1, 100),4))).quantize(decimal.Decimal("0.0000")) for _ in range(10)]