Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions python/pyarrow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 14 additions & 12 deletions python/pyarrow/scalar.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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):

Expand Down Expand Up @@ -402,6 +401,7 @@ cdef class StructValue(ArrayValue):
zip(child_names, wrapped_arrays)
}


cdef class DictionaryValue(ArrayValue):

def as_py(self):
Expand Down Expand Up @@ -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
54 changes: 53 additions & 1 deletion python/pyarrow/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 1 addition & 12 deletions python/pyarrow/tests/test_scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down