Let's make sure we understand what you are starting with:

In [7]: weights
Out[7]: 
[array([[-2.66665269,  0.        ],
        [-0.36358187,  0.        ],
        [ 1.55058871,  0.        ],
        [ 3.91364328,  0.        ]]), array([[ 0.],
        [ 0.]])]
In [8]: len(weights)
Out[8]: 2
In [9]: weights[0]
Out[9]: 
array([[-2.66665269,  0.        ],
       [-0.36358187,  0.        ],
       [ 1.55058871,  0.        ],
       [ 3.91364328,  0.        ]])
In [10]: weights[0].dtype
Out[10]: dtype('float64')
In [11]: weights[0].shape
Out[11]: (4, 2)

In [13]: weights[1]
Out[13]: 
array([[ 0.],
       [ 0.]])
In [14]: weights[1].dtype
Out[14]: dtype('float64')
In [15]: weights[1].shape
Out[15]: (2, 1)

This is a 2 item list, containing two arrays. Both are 2d float.

First you wrap the whole list in an object array:

In [16]: duh =np.array(weights,dtype='object')
In [17]: duh
Out[17]: 
array([ array([[-2.66665269,  0.        ],
       [-0.36358187,  0.        ],
       [ 1.55058871,  0.        ],
       [ 3.91364328,  0.        ]]),
       array([[ 0.],
       [ 0.]])], dtype=object)

This is a 2 element array, shape (2,). But it doesn't change the nature of the elements. And there's a potential gotcha - if the element arrays had the same shape, it would have created a 3d array of objects.

This is not the right syntax for change the dtype of an array. dtype is not a writable property/attribute.

weights[1].dtype='object'

We can use astype instead:

In [19]: weights[1].astype(object)
Out[19]: 
array([[0.0],
       [0.0]], dtype=object)
In [20]: weights[1]=weights[1].astype(object)
In [21]: weights
Out[21]: 
[array([[-2.66665269,  0.        ],
        [-0.36358187,  0.        ],
        [ 1.55058871,  0.        ],
        [ 3.91364328,  0.        ]]), array([[0.0],
        [0.0]], dtype=object)]

It makes a new array, which we'd have write back into the original list.

Now I can change an element of that 2nd array

In [22]: weights[1][0,0]=None
In [23]: weights
Out[23]: 
[array([[-2.66665269,  0.        ],
        [-0.36358187,  0.        ],
        [ 1.55058871,  0.        ],
        [ 3.91364328,  0.        ]]), array([[None],
        [0.0]], dtype=object)]

When playing games like this you have to pay attention to where you have arrays and where they are lists. And pay attention to the shape and dtype of the arrays. Don't blindly index and hope for the best. Display/print these attributes, or the whole array if it isn't too large.

Answer from hpaulj on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 75412937 › typeerror-for-function-defined
python - typeerror for function defined - Stack Overflow
Congrats on the answer, it mostly looks good. However, the answer does not make it clear why the error is raised. The np.array function arguments description is wrong - please check the doc. The second argument is a "dtype", but the op supplied an array, hence the type mismatch and the error message..
🌐
GitHub
github.com › numpy › numpy › issues › 19470
Cannot make numpy array with dtype object if elements are numpy array with ndim > 1 · Issue #19470 · numpy/numpy
July 13, 2021 - import numpy as np b = np.array([np.array([[1], [1], [1]]), np.array([[1, 2], [1, 2], [1, 2]]), np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]), np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]])], dtype=object)
Author   mfkasim1
Top answer
1 of 1
4

Let's make sure we understand what you are starting with:

In [7]: weights
Out[7]: 
[array([[-2.66665269,  0.        ],
        [-0.36358187,  0.        ],
        [ 1.55058871,  0.        ],
        [ 3.91364328,  0.        ]]), array([[ 0.],
        [ 0.]])]
In [8]: len(weights)
Out[8]: 2
In [9]: weights[0]
Out[9]: 
array([[-2.66665269,  0.        ],
       [-0.36358187,  0.        ],
       [ 1.55058871,  0.        ],
       [ 3.91364328,  0.        ]])
In [10]: weights[0].dtype
Out[10]: dtype('float64')
In [11]: weights[0].shape
Out[11]: (4, 2)

In [13]: weights[1]
Out[13]: 
array([[ 0.],
       [ 0.]])
In [14]: weights[1].dtype
Out[14]: dtype('float64')
In [15]: weights[1].shape
Out[15]: (2, 1)

This is a 2 item list, containing two arrays. Both are 2d float.

First you wrap the whole list in an object array:

In [16]: duh =np.array(weights,dtype='object')
In [17]: duh
Out[17]: 
array([ array([[-2.66665269,  0.        ],
       [-0.36358187,  0.        ],
       [ 1.55058871,  0.        ],
       [ 3.91364328,  0.        ]]),
       array([[ 0.],
       [ 0.]])], dtype=object)

This is a 2 element array, shape (2,). But it doesn't change the nature of the elements. And there's a potential gotcha - if the element arrays had the same shape, it would have created a 3d array of objects.

This is not the right syntax for change the dtype of an array. dtype is not a writable property/attribute.

weights[1].dtype='object'

We can use astype instead:

In [19]: weights[1].astype(object)
Out[19]: 
array([[0.0],
       [0.0]], dtype=object)
In [20]: weights[1]=weights[1].astype(object)
In [21]: weights
Out[21]: 
[array([[-2.66665269,  0.        ],
        [-0.36358187,  0.        ],
        [ 1.55058871,  0.        ],
        [ 3.91364328,  0.        ]]), array([[0.0],
        [0.0]], dtype=object)]

It makes a new array, which we'd have write back into the original list.

Now I can change an element of that 2nd array

In [22]: weights[1][0,0]=None
In [23]: weights
Out[23]: 
[array([[-2.66665269,  0.        ],
        [-0.36358187,  0.        ],
        [ 1.55058871,  0.        ],
        [ 3.91364328,  0.        ]]), array([[None],
        [0.0]], dtype=object)]

When playing games like this you have to pay attention to where you have arrays and where they are lists. And pay attention to the shape and dtype of the arrays. Don't blindly index and hope for the best. Display/print these attributes, or the whole array if it isn't too large.

🌐
CopyProgramming
copyprogramming.com › howto › cannot-construct-a-dtype-from-an-array
Python: Constructing a dtype from an array is not possible
April 15, 2023 - To simplify the process, you can convert your array to a tuple-based list (utilizing zip ). Then, utilize np.fromiter to construct the structured array by specifying dt as dtype .
🌐
HatchJS
hatchjs.com › home › how to fix the ‘cannot construct a dtype from an array’ error in python
How to Fix the 'Cannot Construct a dtype from an Array' Error in Python
January 5, 2024 - This error can be fixed by either ... “cannot construct a dtype from an array” occurs when you try to create a dtype from an array that does not have the correct shape....
🌐
Ray
docs.ray.io › en › releases-1.13.0 › _modules › ray › data › extensions › tensor_extension.html
ray.data.extensions.tensor_extension — Ray 1.13.0
See issue: # https://github.com/CODAIT/text-extensions-for-pandas/issues/166 base = None @property def type(self): """ The scalar type for the array, e.g. ``int`` It's expected ``ExtensionArray[item]`` returns an instance of ``ExtensionDtype.type`` for scalar ``item``, assuming that value is valid (not NA). NA values do not need to be instances of `type`. """ return TensorArrayElement @property def name(self) -> str: """ A string identifying the data type. Will be used for display in, e.g. ``Series.dtype`` """ return "TensorDtype" [docs] @classmethod def construct_from_string(cls, string: str): """ Construct this type from a string.
🌐
Intelpython
intelpython.github.io › dpnp › _modules › dpnp › dpnp_iface_manipulation.html
dpnp.dpnp_iface_manipulation — Data Parallel Extension for NumPy 0.20.0dev4+1.g7fae3a6ea29 documentation
Examples -------- Basic examples ... 'same_kind') False >>> np.can_cast('<i8', '>u4', 'unsafe') True """ if dpnp.is_supported_array_type(to): raise TypeError("Cannot construct a dtype from an array") dtype_from = ( from_.dtype if dpnp.is_supported_array_type(from_) else ...
🌐
NumPy
numpy.org › devdocs › reference › arrays.dtypes.html
Data type objects (dtype) — NumPy v2.5.dev0 Manual
First, NumPy treats data type specifications (everything that can be passed to the dtype constructor) as equivalent to the data type object itself. This equivalence can only be handled through ==, not through is. ... Try it in your browser! A dtype object is equal to all data type specifications that are equivalent to it. ... >>> a = np.array([1, 2], dtype=np.float64) >>> a.dtype == np.dtype(np.float64) True >>> a.dtype == np.float64 True >>> a.dtype == float True >>> a.dtype == "float64" True >>> a.dtype == "d" True
Find elsewhere
🌐
GitHub
github.com › cupy › cupy › issues › 6426
cupy.asarray/cp.array fails to create from numpy ndarray with structured dtype · Issue #6426 · cupy/cupy
February 3, 2022 - Description Creating a cupy array from a numpy array with structured dtype fails on 10.1.0. To Reproduce import numpy as np import cupy as cp dtype = np.dtype({'names':['x','y','z'], 'formats': [np.float_]*3}) a = np.empty(100, dtype=dty...
Author   TK-21st
🌐
OneCompiler
onecompiler.com › python › 3xktm4pcr
3xktm4pcr - Python - OneCompiler
Frequency of each character/Integer: Traceback (most recent call last): File "HelloWorld.py", line 5, in <module> print(np.asarray(unique_elements,counts_elements)) File "/usr/local/lib/python3.6/dist-packages/numpy/core/_asarray.py", line 83, in asarray return array(a, dtype, copy=False, order=order) TypeError: Cannot construct a dtype from an array
🌐
Ethz
metaphor.ethz.ch › fsdb › sam › PythonTutorial › frequent_errors.html
Some frequent errors — SciPyTutorial 0.0.4 documentation
Parameters ---------- shape : int or sequence of ints Shape of the new array, e.g., ``(2, 3)``. dtype : data-type, optional The desired data-type for the array, e.g., `numpy.int8`. Default is `numpy.float64`.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › change-numpy-array-data-type
Change the Data Type of the Given NumPy Array - GeeksforGeeks
# change the dtype to 'float64' arr = arr.astype('float64') print(arr) # print the data type print(arr.dtype)
Published   July 11, 2025
🌐
HopHR
hophr.com › tutorial-page › encountering-dtype-errors-numpy-arrays-step-by-step-guide
Hire Vetted AI & Software Talent from Latin America
Browse 500+ pre-vetted data scientists, ML engineers, and AI professionals from Latin America. Competitive rates, English proficiency, and overlapping time zones.
🌐
Medium
mr-amit.medium.com › changing-data-type-dtype-in-numpy-arrays-a-step-by-step-guide-ea7e323e1959
Changing Data Type (dtype) in NumPy Arrays: A Step-by-Step Guide | by It's Amit | Medium
March 6, 2025 - Always check for invalid entries in your array before conversion to avoid errors. Specify dtype when creating or loading arrays to optimize memory usage.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: astype() to change dtype of an array | note.nkmk.me
February 4, 2024 - In this case, they are treated as the equivalent dtype. Examples in Python 3, 64-bit environment are as follows. uint, which is not a standard Python type, is included for convenience. When specifying as an argument, strings 'int' or 'float' can be used for int or float. The non-Python type uint must be specified using the string 'uint'. a = np.array([1, 2, 3], dtype=int) print(a.dtype) # int64 a = np.array([1, 2, 3], dtype='int') print(a.dtype) # int64
🌐
GitHub
github.com › pytorch › pytorch › issues › 85606
Creating NumPy array with `dtype=object` of PyTorch tensors fails · Issue #85606 · pytorch/pytorch
September 25, 2022 - However, when attempting to create a NumPy array with dtype=object, PyTorch tries to convert the tensors to NumPy arrays. This should not be done, as we're not interested in storing the tensors as arrays.
Author   thomasbbrunner
🌐
W3Schools
w3schools.com › python › numpy › numpy_data_types.asp
NumPy Data Types
import numpy as np arr = np.array(['a', '2', '3'], dtype='i') Try it Yourself »