Thanks to visit codestin.com
Credit goes to fory.apache.org

Skip to main content
Version: dev

NumPy & Scientific Computing

Fory natively supports numpy arrays with optimized serialization.

NumPy Array Serialization

Large arrays use zero-copy when possible:

import pyfory
import numpy as np

f = pyfory.Fory()

# Numpy arrays are supported natively
arrays = {
'matrix': np.random.rand(1000, 1000),
'vector': np.arange(10000),
'bool_mask': np.random.choice([True, False], size=5000)
}

data = f.serialize(arrays)
result = f.deserialize(data)

# Zero-copy for compatible array types
assert np.array_equal(arrays['matrix'], result['matrix'])

Pandas DataFrames

Fory can serialize Pandas DataFrames efficiently:

import pyfory
import pandas as pd
import numpy as np

f = pyfory.Fory(xlang=False, ref=False, strict=False)

df = pd.DataFrame({
'a': np.arange(1000, dtype=np.float64),
'b': np.arange(1000, dtype=np.int64),
'c': ['text'] * 1000
})

data = f.serialize(df)
result = f.deserialize(data)

assert df.equals(result)

Zero-Copy with Out-of-Band Buffers

For maximum performance with large arrays, use out-of-band serialization:

import pyfory
import numpy as np

f = pyfory.Fory(xlang=False, ref=False, strict=False)

# Large array
array = np.random.rand(10000, 1000)

# Out-of-band for zero-copy
buffer_objects = []
data = f.serialize(array, buffer_callback=buffer_objects.append)
buffers = [obj.getbuffer() for obj in buffer_objects]

result = f.deserialize(data, buffers=buffers)
assert np.array_equal(array, result)

Supported Array Types

  • np.ndarray (all dtypes)
  • np.matrix
  • Structured arrays
  • Record arrays