def _get_serialization_context(self):
        # Create _context lazily.
        if not hasattr(self, '_context'):
            self._context = pyarrow.SerializationContext()
            register_default_serialization_handlers(self._context)
            self._context.register_type(Decimal,
                                        'decimal.Decimal',
                                        pickle=True)

        return self._context
Example #2
0
def test_serialization_deprecated():
    with pytest.warns(FutureWarning):
        ser = pa.serialize(1)

    with pytest.warns(FutureWarning):
        pa.deserialize(ser.to_buffer())

    f = pa.BufferOutputStream()
    with pytest.warns(FutureWarning):
        pa.serialize_to(12, f)

    buf = f.getvalue()
    f = pa.BufferReader(buf)
    with pytest.warns(FutureWarning):
        pa.read_serialized(f).deserialize()

    with pytest.warns(FutureWarning):
        pa.default_serialization_context()

    context = pa.lib.SerializationContext()
    with pytest.warns(FutureWarning):
        pa.register_default_serialization_handlers(context)
                             mask=mask,
                             fill_value=fill_value,
                             hard_mask=hardmask)


def _serialize_numpy_masked_constant(obj):
    # Workaround for "Changing the dtype of a 0d array is only supported if the itemsize is unchanged" error
    return None


def _deserialize_numpy_masked_constant(obj):
    return np.ma.masked


serialization_context = pa.SerializationContext()
pa.register_default_serialization_handlers(serialization_context)

serialization_context.register_type(
    np.ma.MaskedArray,
    "numpy.ma.core.MaskedArray",
    custom_serializer=_serialize_numpy_masked_array,
    custom_deserializer=_deserialize_numpy_masked_array,
)

serialization_context.register_type(
    np.ma.core.MaskedConstant,
    "numpy.ma.core.MaskedConstant",
    custom_serializer=_serialize_numpy_masked_constant,
    custom_deserializer=_deserialize_numpy_masked_constant,
)