in src/python/turicreate/data_structures/sarray.py [0:0]
def __init__(self, data=[], dtype=None, ignore_cast_failure=False, _proxy=None):
"""
__init__(data=list(), dtype=None, ignore_cast_failure=False)
Construct a new SArray. The source of data includes: list,
range, generators, map, filter, numpy.ndarray, pandas.Series, and urls.
"""
if dtype is not None and type(dtype) != type:
raise TypeError("dtype must be a type, e.g. use int rather than 'int'")
if _proxy:
self.__proxy__ = _proxy
elif isinstance(data, SArray):
if dtype is None:
self.__proxy__ = data.__proxy__
else:
self.__proxy__ = data.astype(dtype).__proxy__
else:
self.__proxy__ = UnitySArrayProxy()
## data transfromation from generator and other iterable to list
if self._is_iterable_required_to_listify(data):
data = list(data)
# we need to perform type inference
if dtype is None:
if HAS_PANDAS and isinstance(data, pandas.Series):
# if it is a pandas series get the dtype of the series
dtype = pytype_from_dtype(data.dtype)
if dtype == object:
# we need to get a bit more fine grained than that
dtype = infer_type_of_sequence(data.values)
elif HAS_NUMPY and isinstance(data, numpy.ndarray):
# first try the fast inproc method
try:
from .. import numpy_loader
if numpy_loader.numpy_activation_successful():
from ..numpy import _fast_numpy_to_sarray
ret = _fast_numpy_to_sarray(data)
# conversion is good!
# swap the proxy.
self.__proxy__, ret.__proxy__ = (
ret.__proxy__,
self.__proxy__,
)
return
else:
dtype = infer_type_of_sequence(data)
except:
pass
# if it is a numpy array, get the dtype of the array
dtype = pytype_from_dtype(data.dtype)
if dtype == object:
# we need to get a bit more fine grained than that
dtype = infer_type_of_sequence(data)
if len(data.shape) == 2:
# we need to make it an array or a list
if dtype == float or dtype == int:
dtype = array.array
else:
dtype = list
elif len(data.shape) > 2:
raise TypeError(
"Cannot convert Numpy arrays of greater than 2 dimensions"
)
elif isinstance(data, str) or (
sys.version_info.major < 3 and isinstance(data, unicode)
):
# if it is a file, we default to string
dtype = str
elif isinstance(data, array.array):
dtype = pytype_from_array_typecode(data.typecode)
elif isinstance(data, collections.Sequence):
# Covers any ordered python container and arrays.
# Convert it to a list first.
dtype = infer_type_of_sequence(data)
else:
dtype = None
if HAS_PANDAS and isinstance(data, pandas.Series):
with cython_context():
self.__proxy__.load_from_iterable(
data.values, dtype, ignore_cast_failure
)
elif isinstance(data, str) or (
sys.version_info.major <= 2 and isinstance(data, unicode)
):
internal_url = _make_internal_url(data)
with cython_context():
self.__proxy__.load_autodetect(internal_url, dtype)
elif (
(HAS_NUMPY and isinstance(data, numpy.ndarray))
or isinstance(data, array.array)
or isinstance(data, collections.Sequence)
):
with cython_context():
self.__proxy__.load_from_iterable(data, dtype, ignore_cast_failure)
else:
raise TypeError(
"Unexpected data source. "
"Possible data source types are: list, "
"numpy.ndarray, pandas.Series, and string(url)"
)