Data Types and Data Forms#

Data Types#

Python Swordfish provides the same data types as DolphinDB. When constructing data objects, you can specify the data type by passing a string to the type parameter. For example:

from swordfish import types as st
sf.vector([1, 2, 3], type="FLOAT")

Each data object has a type attribute, which can be used to retrieve its data type. For example:

sf.vector([1, 2, 3], type="FLOAT").type
# output: FLOAT

A complete list of supported data types can be found in the appendix.

Data Forms#

Python Swordfish supports the following data forms: Scalar, Vector, Pair, Matrix, Set, Dictionary, and Table. These forms are defined in the types submodule and need to be imported before use:

import swordfish as sf
from swordfish import types as st

The following example demonstrates how to construct these data forms. For detailed information on each form, refer to the corresponding topic page.

Creating Scalars#

sf.scalar(1)                               # 1
sf.scalar(1, type=st.INT)                  # 1
sf.scalar(None, type=st.STRING)            # NULL
sf.scalar(1.23, type=st.DECIMAL32(3))      # 1.230

Creating Vectors#

sf.vector([1, 2, 3])                          # [1,2,3]
sf.vector([1, 2, None])                       # [1,2,]
sf.vector([1, 2, 3], type=st.FLOAT)           # [1,2,3]
sf.vector([1, 2, 3], type=st.DECIMAL32(3))    # [1.000, 2.000, 3.000]

# Create an array vector
x = [[1, 2], [3, 4, 5], []]
sf.array_vector(x, type=st.INT)               # [[1, 2], [3, 4, 5], []]

# Create a tuple
sf.any_vector([1, 2, 3])                      # (1,2,3)

Creating Pairs#

p = sf.pair(10,20)   #  10:20
print(p.form)  #  PAIR

Creating Matrices#

import numpy as np
# Create a NumPy array
data = np.array([[1, 2, 3], [4, 5, 6]])
# [[1 2 3] [4 5 6]]


# Create a matrix via Numpy array
m1 = sf.matrix(data, type=st.FLOAT)
"""output
#0 #1 #2
-- -- --
1  2  3
4  5  6
"""

Creating Sets#

sf.set(set(), type=st.INT)              # set()
sf.set({1,2,3})                         # set(3,2,1)

Creating Dictionaries#

sf.dictionary({'a': 1, 'b': 2})
""" output:
{
a->1
b->2
}
"""

Creating Tables#

import pandas as pd

df = pd.DataFrame({'a': [1, 2, 3], 'b': ["a", "b", "c"]})
sf.table(df, types={'a': st.INT, 'b': st.STRING})

""" output:
a b
- -
1 a
2 b
3 c
"""

Appendix#

The following table lists all data types supported by Python Swordfish. Examples use scalars and vectors to illustrate how to specify data types. The same approach applies to other data forms.

Data Type

Example

VOID

sf.scalar(None)
sf.data.Nothing
sf.data.NULL

BOOL

sf.scalar(True)
sf.vector([True, False], type="BOOL")

CHAR

sf.scalar("a", type="CHAR")

SHORT

sf.scalar(10, type="SHORT")

INT

sf.scalar(69, type="INT")

LONG

sf.scalar(10000, type="LONG")

DATE

sf.scalar("2025.08.13", type="DATE")

from datetime import date
sf.vector([date(2025, 8, 20), date(2025, 8, 25), date(2025, 8, 27)])

import numpy as np
sf.scalar(np.datetime64('2025-07-29'), type="DATE")

import pandas as pd
sf.scalar(pd.Timestamp('2025-07-29 13:45:00').date(), type="DATE")

MONTH

sf.scalar("2025.06", type="MONTH")

from datetime import date
sf.vector([date(2025, 8, 20), date(2025, 8, 25), date(2025, 8, 27)], type="MONTH")

import numpy as np
sf.scalar(np.datetime64('2025-07', 'M'), type="MONTH")

import pandas as pd
sf.scalar(pd.Timestamp('2025-07-29 13:45:00'), type="MONTH")

TIME

sf.scalar("13:30:10.008", type="TIME")

from datetime import time
sf.vector([time(14, 30, 0), time(15, 30, 0), time(16, 30, 0)], type="TIME")

import numpy as np
sf.scalar(np.datetime64('2025-07-29T13:45:00'), type="TIME")

import pandas as pd
sf.scalar(pd.Timestamp('15:45:00.000').time(), type="TIME")

MINUTE

sf.scalar("13:30m", type="MINUTE")

from datetime import time
sf.vector([time(14, 30, 0), time(15, 30, 0), time(16, 30, 0)], type="MINUTE")

import numpy as np
sf.scalar(np.datetime64('2025-07-29T13:45:00'), type="MINUTE")

import pandas as pd
sf.scalar(pd.Timestamp('15:45:00.000').time(), type="MINUTE")

SECOND

sf.scalar("13:30:10", type="SECOND")

from datetime import time
sf.vector([time(14, 30, 0), time(15, 30, 0), time(16, 30, 0)], type="SECOND")

import numpy as np
sf.scalar(np.datetime64('2025-07-29T13:45:00'), type="SECOND")

import pandas as pd
sf.scalar(pd.Timestamp('15:45:00.000').time(), type="SECOND")

DATETIME

sf.scalar("2025.06.13 13:30:10", type="DATETIME")

from datetime import datetime
sf.scalar(datetime(2025, 6, 13, 13, 30, 10), type="DATETIME")

import numpy as np
sf.scalar(np.datetime64("2025-06-13T13:30:10"), type="DATETIME")

import pandas as pd
sf.scalar(pd.Timestamp("2025.06.13 13:30:10").time(), type="DATETIME")

TIMESTAMP

sf.scalar("2025.06.13 13:30:10.008", type="TIMESTAMP")

from datetime import datetime
sf.scalar(datetime(2025, 6, 13, 13, 30, 10, 8000), type="TIMESTAMP")

import numpy as np
sf.scalar(np.datetime64('2025-07-29T13:45:00.008'), type="TIMESTAMP")

import pandas as pd
sf.scalar(pd.Timestamp('2025-07-29 13:45:00.008'), type="TIMESTAMP")

NANOTIME

sf.scalar("13:30:10.008007006", type="NANOTIME")

import pandas as pd
sf.scalar(pd.Timestamp("14:30:00.123456789"), type="NANOTIME")

NANOTIMESTAMP

sf.scalar("2025.06.13 13:30:10.008007006", type="NANOTIMESTAMP")

import pandas as pd
sf.scalar(pd.Timestamp("2025-06-13 13:30:10.008007006"), type="NANOTIMESTAMP")

DATEHOUR

sf.scalar("2012.06.13T13", type="DATEHOUR")

from datetime import datetime
sf.scalar(datetime(2025, 6, 13, 13), type="DATEHOUR")

import numpy as np
sf.scalar(np.datetime64("2025-06-13T13", "h"), type="DATEHOUR")

import pandas as pd
sf.scalar(pd.Timestamp("2025-06-13 13:00:00"), type="DATEHOUR")

FLOAT

sf.scalar(3.14, type="FLOAT")

import numpy as np
sf.scalar(np.float32(3.14), type="FLOAT")

import pandas as pd
sf.vector(pd.Series(np.random.rand(5).astype(np.float32)), type="FLOAT")

DOUBLE

sf.scalar(3.14, type="DOUBLE")

import numpy as np
sf.scalar(np.float64(3.14), type="DOUBLE")

import pandas as pd
sf.vector(pd.Series(np.random.rand(5).astype(np.float64)), type="DOUBLE")

SYMBOL

sf.scalar("hello", type="SYMBOL")

import numpy as np
sf.vector(np.array(["a", "b", "c"]), type="SYMBOL")

import pandas as pd
sf.vector(pd.Series(["a", "b", "c"]), type="SYMBOL")

STRING

sf.scalar("hello", type="STRING")

import numpy as np
sf.vector(np.array(["a", "b", "c"]), type="STRING")

import pandas as pd
sf.vector(pd.Series(["a", "b", "c"]), type="STRING")

UUID

sf.scalar("5d212a78-cc48-e3b1-4235-b4d91473ee87", type="UUID")

FUNCTIONDEF

(not yet demonstrated)

HANDLE

(not yet demonstrated)

CODE

(not yet demonstrated)

DATASOURCE

(not yet demonstrated)

RESOURCE

(not yet demonstrated)

ANY

sf.vector([1, 2, 3], type="ANY")
sf.any_vector([1, "a", 15])
sf.any_vector(np.array([10, 42, 59]))

DICTIONARY

sf.dictionary({'a': 1, 'b': 2})

IPADDR

sf.scalar("192.168.1.13", type="IPADDR")

INT128

sf.scalar("e1671797c52e15f763380b45e841ec32", type="INT128")

BLOB

sf.data.Blob(b"hello")

COMPLEX

(not yet demonstrated)

POINT

(not yet demonstrated)

DURATION

sf.scalar("200ms", type="DURATION")
sf.data.Duration(3, "w")

OBJECT

(not yet demonstrated)

DECIMAL32

sf.scalar(3.14, type=["DECIMAL32", 2])
sf.data.Decimal32(3.14, 2)
sf.vector([1.336, 2.559, 5.223], type="DECIMAL32(3)")

DECIMAL64

sf.scalar(123.123456789, type=["DECIMAL64", 3])
sf.data.Decimal64(123.123456789, 3)
sf.vector([123.1234, 2.5594, 5.2236], type="DECIMAL64(3)")

DECIMAL128

sf.scalar(123.123456789012345678, type=["DECIMAL128", 3])
sf.data.Decimal128(123.123456789012345678, 3)
sf.vector([123.1234, 2.5594, 5.2236], type="DECIMAL128(3)")