MCPcopy Create free account
hub / github.com/huggingface/datasets / to_numpy

Method to_numpy

src/datasets/features/features.py:789–831  ·  view source on GitHub ↗
(self, zero_copy_only=True)

Source from the content-addressed store, hash-verified

787 return self.storage[i]
788
789 def to_numpy(self, zero_copy_only=True):
790 storage: pa.ListArray = self.storage
791 null_mask = storage.is_null().to_numpy(zero_copy_only=False)
792
793 if self.type.shape[0] is not None:
794 size = 1
795 null_indices = np.arange(len(storage))[null_mask] - np.arange(np.sum(null_mask))
796
797 for i in range(self.type.ndims):
798 size *= self.type.shape[i]
799 storage = storage.flatten()
800 numpy_arr = storage.to_numpy(zero_copy_only=zero_copy_only)
801 numpy_arr = numpy_arr.reshape(len(self) - len(null_indices), *self.type.shape)
802
803 if len(null_indices):
804 numpy_arr = np.insert(numpy_arr.astype(np.float64), null_indices, np.nan, axis=0)
805
806 else:
807 shape = self.type.shape
808 ndims = self.type.ndims
809 arrays = []
810 first_dim_offsets = np.array([off.as_py() for off in storage.offsets])
811 for i, is_null in enumerate(null_mask):
812 if is_null:
813 arrays.append(np.nan)
814 else:
815 storage_el = storage[i : i + 1]
816 first_dim = first_dim_offsets[i + 1] - first_dim_offsets[i]
817 # flatten storage
818 for _ in range(ndims):
819 storage_el = storage_el.flatten()
820
821 numpy_arr = storage_el.to_numpy(zero_copy_only=zero_copy_only)
822 arrays.append(numpy_arr.reshape(first_dim, *shape[1:]))
823
824 if len(np.unique(np.diff(first_dim_offsets))) > 1:
825 # ragged
826 numpy_arr = np.empty(len(arrays), dtype=object)
827 numpy_arr[:] = arrays
828 else:
829 numpy_arr = np.array(arrays)
830
831 return numpy_arr
832
833 def to_pylist(self, maps_as_pydicts: Optional[Literal["lossy", "strict"]] = None):
834 zero_copy_only = _is_zero_copy_only(self.storage.type, unnest=True)

Callers 13

__array__Method · 0.95
to_pylistMethod · 0.95
selectMethod · 0.80
__from_arrow__Method · 0.80
cast_storageMethod · 0.80
_arrow_array_to_numpyMethod · 0.80
test_to_numpyMethod · 0.80
test_to_pandasMethod · 0.80
test_table_to_pandasFunction · 0.80
test_load_vectorsFunction · 0.80

Calls 2

flattenMethod · 0.45
uniqueMethod · 0.45

Tested by 6

test_to_numpyMethod · 0.64
test_to_pandasMethod · 0.64
test_table_to_pandasFunction · 0.64
test_load_vectorsFunction · 0.64
test_load_vectorsFunction · 0.64