Skip to content

Commit 0e2db33

Browse files
committed
review feedback
1 parent a2fbf8c commit 0e2db33

3 files changed

Lines changed: 29 additions & 101 deletions

File tree

python/pyarrow/array.pxi

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5057,26 +5057,22 @@ cdef class VariableShapeTensorArray(ExtensionArray):
50575057
"""
50585058

50595059
@staticmethod
5060-
def from_numpy_ndarray(obj, dim_names=None, permutation=None, uniform_shape=None,
5061-
value_type=None, ndim=None):
5060+
def from_numpy_ndarray(obj, dim_names=None, permutation=None, uniform_shape=None):
50625061
"""
50635062
Convert a sequence of numpy.ndarrays to a variable shape tensor extension array.
50645063
The length of the input sequence becomes the length of the output array.
50655064
50665065
Parameters
50675066
----------
50685067
obj : Sequence[numpy.ndarray]
5069-
Sequence of ndarrays with matching dtype, ndim, and memory permutation.
5068+
Non-empty sequence of ndarrays with matching dtype, ndim, and
5069+
memory permutation.
50705070
dim_names : tuple or list of strings, default None
50715071
Explicit names to tensor dimensions.
50725072
permutation : tuple or list of integers, default None
50735073
Physical permutation for all input arrays. If None, inferred from strides.
50745074
uniform_shape : tuple or list of integers or None, default None
50755075
Optional known uniform dimensions in physical order. If None, inferred.
5076-
value_type : pyarrow.DataType or numpy dtype, default None
5077-
Optional explicit tensor value type. Required with empty input.
5078-
ndim : int, default None
5079-
Optional explicit tensor rank. Required with empty input.
50805076
50815077
Returns
50825078
-------
@@ -5095,56 +5091,23 @@ cdef class VariableShapeTensorArray(ExtensionArray):
50955091
cdef:
50965092
list arrays
50975093
list shape_rows
5098-
int array_ndim
5094+
int ndim
50995095
int i
51005096
object base_dtype
51015097
DataType arrow_type
51025098
list normalized_permutation
51035099
list permutation_metadata
5104-
DataType shape_type
51055100
Array values
51065101
Array shapes
51075102
StructArray struct_arr
51085103
VariableShapeTensorType ext_type
51095104

5110-
if isinstance(obj, np.ndarray):
5111-
raise TypeError("obj must be a sequence of numpy arrays")
51125105
if not isinstance(obj, Sequence) or isinstance(obj, (str, bytes)):
51135106
raise TypeError("obj must be a sequence of numpy arrays")
51145107
arrays = list(obj)
51155108

5116-
if value_type is not None and not isinstance(value_type, DataType):
5117-
try:
5118-
value_type = from_numpy_dtype(np.dtype(value_type))
5119-
except (TypeError, ValueError) as exc:
5120-
raise TypeError(
5121-
"value_type must be a pyarrow.DataType or numpy dtype"
5122-
) from exc
5123-
51245109
if len(arrays) == 0:
5125-
if value_type is None or ndim is None:
5126-
raise ValueError(
5127-
"For empty input, both value_type and ndim must be provided")
5128-
if ndim < 0:
5129-
raise ValueError("ndim must be non-negative")
5130-
5131-
_validate_dim_names(dim_names, ndim)
5132-
permutation = _validate_permutation(permutation, ndim)
5133-
_validate_uniform_shape(uniform_shape, ndim)
5134-
5135-
shape_type = list_(int32(), list_size=ndim)
5136-
values = array([], list_(value_type))
5137-
shapes = array([], shape_type)
5138-
struct_arr = StructArray.from_arrays(
5139-
[values, shapes], names=["data", "shape"])
5140-
ext_type = variable_shape_tensor(
5141-
value_type,
5142-
ndim,
5143-
dim_names=dim_names,
5144-
permutation=permutation,
5145-
uniform_shape=uniform_shape
5146-
)
5147-
return ExtensionArray.from_storage(ext_type, struct_arr)
5110+
raise ValueError("Expected a non-empty sequence of ndarrays")
51485111

51495112
for i, arr in enumerate(arrays):
51505113
if not isinstance(arr, np.ndarray):
@@ -5153,18 +5116,9 @@ cdef class VariableShapeTensorArray(ExtensionArray):
51535116
raise ValueError("Cannot convert scalar to variable shape tensor array")
51545117

51555118
base_dtype = arrays[0].dtype
5156-
array_ndim = arrays[0].ndim
5119+
ndim = arrays[0].ndim
51575120
arrow_type = from_numpy_dtype(base_dtype)
51585121

5159-
if value_type is not None and value_type != arrow_type:
5160-
raise TypeError(
5161-
f"numpy array dtype {base_dtype} does not match value_type {value_type}")
5162-
5163-
if ndim is not None and ndim != array_ndim:
5164-
raise ValueError(
5165-
f"ndim must match numpy arrays ndim ({array_ndim}). Got {ndim}.")
5166-
ndim = array_ndim
5167-
51685122
for i, arr in enumerate(arrays[1:], start=1):
51695123
if arr.dtype != base_dtype:
51705124
raise TypeError(

python/pyarrow/includes/libarrow.pxd

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -908,14 +908,6 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
908908
const shared_ptr[CBuffer] null_bitmap,
909909
)
910910

911-
@staticmethod
912-
CResult[shared_ptr[CArray]] FromArraysAndType" FromArrays"(
913-
shared_ptr[CDataType],
914-
const shared_ptr[CArray]& offsets,
915-
const shared_ptr[CArray]& keys,
916-
const shared_ptr[CArray]& items,
917-
CMemoryPool* pool)
918-
919911
shared_ptr[CArray] keys()
920912
shared_ptr[CArray] items()
921913
CMapType* map_type()
@@ -1192,11 +1184,6 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
11921184
void set_chunksize(int64_t chunksize)
11931185

11941186
cdef cppclass CTensor" arrow::Tensor":
1195-
CTensor(const shared_ptr[CDataType]& type,
1196-
const shared_ptr[CBuffer]& data,
1197-
const vector[int64_t]& shape,
1198-
const vector[int64_t]& strides,
1199-
const vector[c_string]& dim_names)
12001187
shared_ptr[CDataType] type()
12011188
shared_ptr[CBuffer] data()
12021189

@@ -3027,24 +3014,6 @@ cdef extern from "arrow/extension_type.h" namespace "arrow":
30273014

30283015
shared_ptr[CArray] storage()
30293016

3030-
cdef extern from "arrow/extension/variable_shape_tensor.h" namespace "arrow::extension" nogil:
3031-
cdef cppclass CVariableShapeTensorType \
3032-
" arrow::extension::VariableShapeTensorType"(CExtensionType) nogil:
3033-
3034-
CResult[shared_ptr[CTensor]] MakeTensor(const shared_ptr[CExtensionScalar]& scalar) const
3035-
3036-
@staticmethod
3037-
CResult[shared_ptr[CDataType]] Make(const shared_ptr[CDataType]& value_type,
3038-
const int32_t ndim,
3039-
const vector[int64_t] permutation,
3040-
const vector[c_string] dim_names,
3041-
const vector[optional[int64_t]] uniform_shape)
3042-
3043-
const shared_ptr[CDataType] value_type()
3044-
const int32_t ndim()
3045-
const vector[int64_t] permutation()
3046-
const vector[c_string] dim_names()
3047-
const vector[optional[int64_t]] uniform_shape()
30483017

30493018
cdef extern from "arrow/extension/json.h" namespace "arrow::extension" nogil:
30503019
cdef cppclass CJsonType" arrow::extension::JsonExtensionType"(CExtensionType):
@@ -3065,7 +3034,7 @@ cdef extern from "arrow/extension/uuid.h" namespace "arrow::extension" nogil:
30653034

30663035
cdef extern from "arrow/extension/fixed_shape_tensor.h" namespace "arrow::extension" nogil:
30673036
cdef cppclass CFixedShapeTensorType \
3068-
" arrow::extension::FixedShapeTensorType"(CExtensionType) nogil:
3037+
" arrow::extension::FixedShapeTensorType"(CExtensionType):
30693038

30703039
CResult[shared_ptr[CTensor]] MakeTensor(const shared_ptr[CExtensionScalar]& scalar) const
30713040

@@ -3085,6 +3054,26 @@ cdef extern from "arrow/extension/fixed_shape_tensor.h" namespace "arrow::extens
30853054
const CResult[shared_ptr[CTensor]] ToTensor() const
30863055

30873056

3057+
cdef extern from "arrow/extension/variable_shape_tensor.h" namespace "arrow::extension" nogil:
3058+
cdef cppclass CVariableShapeTensorType \
3059+
" arrow::extension::VariableShapeTensorType"(CExtensionType):
3060+
3061+
CResult[shared_ptr[CTensor]] MakeTensor(const shared_ptr[CExtensionScalar]& scalar) const
3062+
3063+
@staticmethod
3064+
CResult[shared_ptr[CDataType]] Make(const shared_ptr[CDataType]& value_type,
3065+
const int32_t ndim,
3066+
const vector[int64_t] permutation,
3067+
const vector[c_string] dim_names,
3068+
const vector[optional[int64_t]] uniform_shape)
3069+
3070+
const shared_ptr[CDataType] value_type()
3071+
const int32_t ndim()
3072+
const vector[int64_t] permutation()
3073+
const vector[c_string] dim_names()
3074+
const vector[optional[int64_t]] uniform_shape()
3075+
3076+
30883077
cdef extern from "arrow/extension/opaque.h" namespace "arrow::extension" nogil:
30893078
cdef cppclass COpaqueType \
30903079
" arrow::extension::OpaqueType"(CExtensionType):

python/pyarrow/tests/test_extension_type.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,23 +1978,8 @@ def test_variable_shape_tensor_roundtrip_2d(dtype):
19781978

19791979

19801980
@pytest.mark.numpy
1981-
def test_variable_shape_tensor_from_numpy_empty_input_schema():
1982-
arr = pa.VariableShapeTensorArray.from_numpy_ndarray(
1983-
[],
1984-
value_type=pa.int32(),
1985-
ndim=2,
1986-
dim_names=["H", "W"],
1987-
permutation=[1, 0],
1988-
uniform_shape=[None, None],
1989-
)
1990-
assert len(arr) == 0
1991-
assert arr.type.value_type == pa.int32()
1992-
assert arr.type.ndim == 2
1993-
assert arr.type.dim_names == ["H", "W"]
1994-
assert arr.type.permutation == [1, 0]
1995-
assert arr.type.uniform_shape == [None, None]
1996-
1997-
with pytest.raises(ValueError, match="both value_type and ndim must be provided"):
1981+
def test_variable_shape_tensor_from_numpy_empty_input_rejected():
1982+
with pytest.raises(ValueError, match="non-empty sequence of ndarrays"):
19981983
pa.VariableShapeTensorArray.from_numpy_ndarray([])
19991984

20001985

0 commit comments

Comments
 (0)