diff --git a/python/pyarrow/__init__.py b/python/pyarrow/__init__.py index 89dfd035ab3..20254c2a84d 100644 --- a/python/pyarrow/__init__.py +++ b/python/pyarrow/__init__.py @@ -80,8 +80,10 @@ def parse_version(root): UInt8Value, UInt16Value, UInt32Value, UInt64Value, HalfFloatValue, FloatValue, DoubleValue, ListValue, BinaryValue, StringValue, FixedSizeBinaryValue, - DecimalValue, - Date32Value, Date64Value, TimestampValue) + DecimalValue, UnionValue, StructValue, DictionaryValue, + Date32Value, Date64Value, + Time32Value, Time64Value, + TimestampValue) # Buffers, allocation from pyarrow.lib import (Buffer, ResizableBuffer, foreign_buffer, py_buffer, diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi index f7fd24db0b7..9d14e1edda5 100644 --- a/python/pyarrow/array.pxi +++ b/python/pyarrow/array.pxi @@ -348,8 +348,9 @@ def _restore_array(data): cdef class Array: def __init__(self): - raise TypeError("Do not call Array's constructor directly, use one " - "of the `pyarrow.Array.from_*` functions instead.") + raise TypeError("Do not call {}'s constructor directly, use one of " + "the `pyarrow.Array.from_*` functions instead." + .format(self.__class__.__name__)) cdef void init(self, const shared_ptr[CArray]& sp_array): self.sp_array = sp_array diff --git a/python/pyarrow/scalar.pxi b/python/pyarrow/scalar.pxi index adb5fa6c2af..964eadbfeb3 100644 --- a/python/pyarrow/scalar.pxi +++ b/python/pyarrow/scalar.pxi @@ -42,6 +42,11 @@ NA = NAType() cdef class ArrayValue(Scalar): + def __init__(self): + raise TypeError("Do not call {}'s constructor directly, use array " + "subscription instead." + .format(self.__class__.__name__)) + cdef void init(self, DataType type, const shared_ptr[CArray]& sp_array, int64_t index): self.type = type @@ -51,14 +56,7 @@ cdef class ArrayValue(Scalar): cdef void _set_array(self, const shared_ptr[CArray]& sp_array): self.sp_array = sp_array - def _check_null(self): - if self.sp_array.get() == NULL: - raise ReferenceError( - 'ArrayValue instance not propertly initialized ' - '(references NULL pointer)') - def __repr__(self): - self._check_null() if hasattr(self, 'as_py'): return repr(self.as_py()) else: @@ -74,7 +72,8 @@ cdef class ArrayValue(Scalar): "Cannot compare Arrow values that don't support as_py()") def __hash__(self): - return hash(self.as_py()) + return hash(self.as_py()) + cdef class BooleanValue(ArrayValue): @@ -402,6 +401,7 @@ cdef class StructValue(ArrayValue): zip(child_names, wrapped_arrays) } + cdef class DictionaryValue(ArrayValue): def as_py(self): @@ -457,12 +457,14 @@ cdef dict _scalar_classes = { cdef object box_scalar(DataType type, const shared_ptr[CArray]& sp_array, int64_t index): - cdef ArrayValue val + cdef ArrayValue value + if type.type.id() == _Type_NA: return NA elif sp_array.get().IsNull(index): return NA else: - val = _scalar_classes[type.type.id()]() - val.init(type, sp_array, index) - return val + klass = _scalar_classes[type.type.id()] + value = klass.__new__(klass) + value.init(type, sp_array, index) + return value diff --git a/python/pyarrow/tests/test_misc.py b/python/pyarrow/tests/test_misc.py index 3b17f4ca77f..26717e19a07 100644 --- a/python/pyarrow/tests/test_misc.py +++ b/python/pyarrow/tests/test_misc.py @@ -55,7 +55,59 @@ def test_cpu_count(): pa.lib.TimestampType, pa.lib.Decimal128Type, pa.lib.DictionaryType, - pa.lib.FixedSizeBinaryType + pa.lib.FixedSizeBinaryType, + pa.NullArray, + pa.NumericArray, + pa.IntegerArray, + pa.FloatingPointArray, + pa.BooleanArray, + pa.Int8Array, + pa.Int16Array, + pa.Int32Array, + pa.Int64Array, + pa.UInt8Array, + pa.UInt16Array, + pa.UInt32Array, + pa.UInt64Array, + pa.ListArray, + pa.UnionArray, + pa.BinaryArray, + pa.StringArray, + pa.FixedSizeBinaryArray, + pa.DictionaryArray, + pa.Date32Array, + pa.Date64Array, + pa.TimestampArray, + pa.Time32Array, + pa.Time64Array, + pa.Decimal128Array, + pa.StructArray, + pa.ArrayValue, + pa.BooleanValue, + pa.Int8Value, + pa.Int16Value, + pa.Int32Value, + pa.Int64Value, + pa.UInt8Value, + pa.UInt16Value, + pa.UInt32Value, + pa.UInt64Value, + pa.HalfFloatValue, + pa.FloatValue, + pa.DoubleValue, + pa.DecimalValue, + pa.Date32Value, + pa.Date64Value, + pa.Time32Value, + pa.Time64Value, + pa.TimestampValue, + pa.StringValue, + pa.BinaryValue, + pa.FixedSizeBinaryValue, + pa.ListValue, + pa.UnionValue, + pa.StructValue, + pa.DictionaryValue ]) def test_extension_type_constructor_errors(klass): # ARROW-2638: prevent calling extension class constructors directly diff --git a/python/pyarrow/tests/test_scalars.py b/python/pyarrow/tests/test_scalars.py index 0a5c72a427b..0b910723595 100644 --- a/python/pyarrow/tests/test_scalars.py +++ b/python/pyarrow/tests/test_scalars.py @@ -28,20 +28,9 @@ class TestScalars(unittest.TestCase): def test_null_singleton(self): - with self.assertRaises(Exception): + with pytest.raises(Exception): pa.NAType() - def test_ctor_null_check(self): - # ARROW-1155 - with pytest.raises(ReferenceError): - repr(pa.Int16Value()) - - with pytest.raises(ReferenceError): - str(pa.Int16Value()) - - with pytest.raises(ReferenceError): - repr(pa.StringValue()) - def test_bool(self): arr = pa.array([True, None, False, None])