Data Types
Data Types Supported by Python Parser
Similar to Python, Python Parser supports various data types, including integers, floating-point numbers, strings, booleans, null values, variables, and constants. The difference lies in that Python Parser's data types are compatible with DolphinDB's. For instance, None in Python Parser corresponds to VOID in DolphinDB; the integer precision in Python Parser is consistent with that in DolphinDB, rather than unlimited as in Python.
The following table compares the data types in DolphinDB, Python Parser, and Python (Empty cells in the "Python" column indicate data types not supported by Python):
Data Type |
DolphinDB |
Python Parser |
Python |
---|---|---|---|
VOID | NULL | None | None |
BOOL | 1b, 0b, true, false | 1b, 0b, True, False, bool(1) | True, False |
CHAR | 'a', 97c | 97c, char('a') | |
SHORT | 122h | 122h | |
INT | 21 | 21 | 21 |
LONG | 22l | 22l | |
DATE | 2013.06.13 | 2013.06.13 | 2013-06-13 |
MONTH | 2012.06M | 2012.06M | 2012-06 |
TIME | 13:30:10.008 | 13:30:10.008 | 13:30:10.008 |
MINUTE | 13:30m | 13:30m | 13:30 |
SECOND | 13:30:10 | 13:30:10 | 13:30:10 |
DATETIME | 2012.06.13 13:30:10 or 2012.06.13T13:30:10 | 2012.06.13 13:30:10 or 2012.06.13T13:30:10 | 2012-06-13 13:30:10 |
TIMESTAMP | 2012.06.13 13:30:10.008 or 2012.06.13T13:30:10.008 | 2012.06.13 13:30:10.008 or 2012.06.13T13:30:10.008 | 2012-06-13 13:30:10.008000 |
NANOTIME | 13:30:10.008007006 | 13:30:10.008007006 | 13:30:10.008007006 |
NANOTIMESTAMP | 2012.06.13 13:30:10.008007006 or 2012.06.13T13:30:10.008007006 | 2012.06.13 13:30:10.008007006 or 2012.06.13T13:30:10.008007006 | 2012.06.13 13:30:10.008007006 |
FLOAT | 2.1f (single precision) | 2.1f (single precision) | 2.1 (double precision) |
DOUBLE | 2.1, 2.1F (double precision) | 2.1, 2.1F (double precision) | |
SYMBOL | symbol([`A,`B,`C]) | symbol([`A,`B,`C].toddb()) | |
STRING | "Hello" or 'Hello' or `Hello | "Hello" or 'Hello' or `Hello or """Hello""" or '''Hello''' | "Hello" or 'Hello' or """Hello""" or '''Hello''' |
UUID | uuid("9d457e79-1bed-d6c2-3612-b0d31c1881f6") | uuid("9d457e79-1bed-d6c2-3612-b0d31c1881f6") | 9d457e79-1bed-d6c2-3612-b0d31c1881f6 |
FUNCTIONDEF | def f1(a,b) {return a+b;} | def f1(a,b): return a+b | |
HANDLE | file handle, socket handle, and db handle | file handle, socket handle, and db handle | |
CODE | <1+2> | <1+2> | |
DATASOURCE | data source | data source | |
RESOURCE | model (kmeans) | model (kmeans) | |
ANY | (1,2,3) | (1,2,3) | (1,2,3) |
COMPRESSED | compress(seq(1,10), "delta") | compress(seq(1,10), "delta") | |
ANY DICTIONARY | {"a":1,"b":2} | {"a":1,"b":2} | {"a":1,"b":2} |
DATEHOUR | datehour(2012.06.13 13:30:10) or datehour(2012.06.13T13:30:10) | Generated by the datehour function
in DolphinDB: datehour(2012.06.13 13:30:10) or
datehour(2012.06.13T13:30:10) |
2012-06-13 13:30:10 |
IPADDR | ipaddr("192.168.1.13") | ipaddr("192.168.1.13") | ipaddress.IPv4Address("192.168.1.13") |
INT128 | int128("e1671797c52e15f763380b45e841ec32") | int128("e1671797c52e15f763380b45e841ec32") | |
BLOB | blob(str) | blob(str) | |
COMPLEX | complex(2, 5) | complex(2, 5) | complex(2, 5) |
POINT | point(117.60972, 24.118418) | point(117.60972, 24.118418) | |
DURATION | 1s, 3M, 5y, 200ms | 1s, 3M, 5y, 200ms | |
DECIMAL32(S) | 3.1415926$DECIMAL32(3), decimal32(3.1415926, 3) | decimal32(3.1415926, 3) |
decimal.Decimal(3.1415926) DECIMAL in Python does not specify fixed-bit representations like DECIMAL32/64/128. Its maximum number of bits is determined by the Python version and the underlying hardware. |
DECIMAL64(S) | 3.1415926$DECIMAL64(3), 3.141P, decimal64(3.1415926, 3) | decimal64(3.1415926, 3) | |
DECIMAL128(S) | 3.1415926$DECIMAL128(3), decimal128(3.1415926, 3) | decimal128(3.1415926, 3) |
Data Type Query
The type function can be used to query the data type of an object, as shown below:
type(None)
Output: dolphindb.SCALAR.VOID
Reserved Word
Constant Reserved Word
Python Parser supports specific reserved words in Python, such as "True", "False", "None", etc., and is compatible with constant reserved words in DolphinDB, as shown in the following table:
Python |
DolphinDB |
---|---|
True | ddb.true |
False | ddb.false |
None | ddb.VOID |
To call DolphinDB's built-in functions, the dolphindb module can be used to import reserved words exclusive to DolphinDB, as shown in the following script:
import dolphindb as ddb
ddb.NULL
ddb.VOID
ddb.pi
ddb.e
ddb.HINT_KEEPORDER
ddb.RANGE
...
Use the is
operator to determine whether two variables reference
the same object.
None is ddb.VOID
Output: true
None is ddb.NULL
Output: false
Programming Reserved Word
Python Parser supports most programming reserved words in Python and DolphinDB,
except global
, nonlocal
, del
,
yield
, match
, async
,
await
, and with
.
def zero(s):
a = int(s)
assert a > 0,"a is out of range"
return a
zero("-2")
Output: Testing case adhocTesting_"a is out of range" failed
n = 0
while n < 10:
n = n + 1
if n % 2 == 0:
continue
print(n)
Output:
1
3
5
7
9
The raise
statement in Python Parser only supports raising an
exception with a message, as shown below:
def test_exception():
try:
raise "asdf"
except Exception as ex:
assert ex[0]=="USER"
print("test_exception test pass")
test_exception()