Nasty little problem... I have been fooling around with this toy example:

>>> arr = np.array([['one', [1, 2, 3]],['two', [4, 5, 6]]], dtype=np.object)
>>> arr
array([['one', [1, 2, 3]],
       ['two', [4, 5, 6]]], dtype=object)

My first guess was:

>>> np.array(arr[:, 1])
array([[1, 2, 3], [4, 5, 6]], dtype=object)

But that keeps the object dtype, so perhaps then:

>>> np.array(arr[:, 1], dtype=float)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.

You can normally work around this doing the following:

>>> np.array(arr[:, 1], dtype=[('', float)]*3).view(float).reshape(-1, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected a readable buffer object

Not here though, which was kind of puzzling. Apparently it is the fact that the objects in your array are lists that throws this off, as replacing the lists with tuples works:

>>> np.array([tuple(j) for j in arr[:, 1]],
...          dtype=[('', float)]*3).view(float).reshape(-1, 3)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])

Since there doesn't seem to be any entirely satisfactory solution, the easiest is probably to go with:

>>> np.array(list(arr[:, 1]), dtype=float)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])

Although that will not be very efficient, probably better to go with something like:

>>> np.fromiter((tuple(j) for j in arr[:, 1]), dtype=[('', float)]*3,
...             count=len(arr)).view(float).reshape(-1, 3)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])
Answer from Jaime on Stack Overflow
Top answer
1 of 8
28

Nasty little problem... I have been fooling around with this toy example:

>>> arr = np.array([['one', [1, 2, 3]],['two', [4, 5, 6]]], dtype=np.object)
>>> arr
array([['one', [1, 2, 3]],
       ['two', [4, 5, 6]]], dtype=object)

My first guess was:

>>> np.array(arr[:, 1])
array([[1, 2, 3], [4, 5, 6]], dtype=object)

But that keeps the object dtype, so perhaps then:

>>> np.array(arr[:, 1], dtype=float)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.

You can normally work around this doing the following:

>>> np.array(arr[:, 1], dtype=[('', float)]*3).view(float).reshape(-1, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected a readable buffer object

Not here though, which was kind of puzzling. Apparently it is the fact that the objects in your array are lists that throws this off, as replacing the lists with tuples works:

>>> np.array([tuple(j) for j in arr[:, 1]],
...          dtype=[('', float)]*3).view(float).reshape(-1, 3)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])

Since there doesn't seem to be any entirely satisfactory solution, the easiest is probably to go with:

>>> np.array(list(arr[:, 1]), dtype=float)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])

Although that will not be very efficient, probably better to go with something like:

>>> np.fromiter((tuple(j) for j in arr[:, 1]), dtype=[('', float)]*3,
...             count=len(arr)).view(float).reshape(-1, 3)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])
2 of 8
13

Based on Jaime's toy example I think you can do this very simply using np.vstack():

arr = np.array([['one', [1, 2, 3]],['two', [4, 5, 6]]], dtype=np.object)
float_arr = np.vstack(arr[:, 1]).astype(np.float)

This will work regardless of whether the 'numeric' elements in your object array are 1D numpy arrays, lists or tuples.

๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ convert-a-numpy-2d-array-with-object-dtype-to-a-regular-2d-array-of-floats.aspx
Python - Convert a NumPy 2D array with object dtype to a regular 2D array of floats
April 4, 2023 - Secondly, we can directly use vstack() method on the extracted column which will stack all the lists vertically and we can define the data type as a float without any error. ... # Import numpy import numpy as np # Creating an array arr = np.array([['one', [1, 2, 3]],['two', [4, 5, 6]]], dtype='O') ...
Discussions

python - How to convert the 'dtype = object' NumPy array into 'dtype = float' array? - Stack Overflow
This problem is caused by using ... get a NumPy array of object, it is quite confusing for me here. First, I don't see numbers in the rref matrix is wrapped by ' ' or " "๏ผŒhow come they are 'object'. Second, is there any way to convert it into a 'dtype = float' ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Converting a data structure of dtype=object to numpy array of dtype=float64 - Stack Overflow
I am trying to convert 'feature1' array from the following data structure into a numpy array so I can input it to sklearn. However, I am running in circles as it always tells me that dtype=object is unsuitable, and I am not able to convert it to the desired float64 format. More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Convert numpy object type to float type - Stack Overflow
Those are all float64 dtype, and the resulting array will have that dtype as well. Select the integer volume column separately. You are already treating the string/object date column separately. ... Sign up to request clarification or add additional context in comments. ... As the first line under import numpy as np. ... it's work. but i think it's not storing my number in normal format in the memory. because my end goal is to convert ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
January 22, 2020
python - How to convert from a dtype('o') to a dtype(float) in numpy? - Stack Overflow
I'm looking for a way to read this csv into python 2.7 and turn it into a (3,22000) array. For some reason I haven't been able to do it, no matter which way i try, I either get a groupn of strings... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Top answer
1 of 3
2

Lets take as the starting point a list of lists or equivalently an object array of lists:

A = [[], [], [], [1,2,1], [], [2, 2, 0, 0], []]
A = array([[], [], [], [1,2,1], [], [2, 2, 0, 0], []], dtype=object)

If the sublists were all the same length, np.array([...]) would give you a 2d array, one row for each sublist, and columns matching their common length. But since they are unequal in length, it can only make it a 1d array, where each element is a pointer to one of these sublists - i.e. dtype=object.

I can imagine 2 ways of constructing a 2d array:

  • pad each sublist to a common length
  • insert each sublist into an empty array of the appropriate size.

Basically it requires common Python iteration; it's not a common enough task to have a wiz-bang numpy function.

For example:

In [346]: n=len(A)
In [348]: m=max([len(x) for x in A])
In [349]: AA=np.zeros((n,m),int)
In [350]: for i,x in enumerate(A):
   .....:     AA[i,:len(x)] = x
In [351]: AA
Out[351]: 
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [1, 2, 1, 0],
       [0, 0, 0, 0],
       [2, 2, 0, 0],
       [0, 0, 0, 0]])

To get a sparse matrix:

In [352]: from scipy import sparse
In [353]: MA=sparse.coo_matrix(AA)
In [354]: MA
Out[354]: 
<7x4 sparse matrix of type '<class 'numpy.int32'>'
    with 5 stored elements in COOrdinate format>

Nothing magical, just straight forward sparse matrix construction. I suppose you could bypass the dense matrix

There is a list-of-lists sparse format that looks a bit like your data.

In [356]: Ml=MA.tolil()

In [357]: Ml.rows
Out[357]: array([[], [], [], [0, 1, 2], [], [0, 1], []], dtype=object)

In [358]: Ml.data
Out[358]: array([[], [], [], [1, 2, 1], [], [2, 2], []], dtype=object)

Conceivably you could construct an empty sparse.lil_matrix((n,m)) matrix, and set it's .data attribute directly. But you'd also have to calculate the rows attribute.

You could also look at the data, row. col attributes of the coo format matrix, and decide it would be easy to construct the equivalent from your A list of lists.

One way or other you have to decide how the non-zero rows get padded to the full length.

2 of 3
0

This:

array = numpy.array ( [ x['feature1'] for x in ver ] )

Or you need to be more clear in your example...

๐ŸŒ
Codegive
codegive.com โ€บ blog โ€บ numpy_dtype_object_to_float.php
Numpy dtype object to float
Always prefer explicit conversion using .astype(float) or pd.to_numeric(). While sometimes NumPy might implicitly convert data when creating an array, relying on this can lead to unexpected object dtypes if data is not perfectly clean.
Top answer
1 of 2
2

131.199997,1.31199997e+02 are equivalent displays of the same number. They are both 'normal floats'.

In:

array([[131.199997, 137.149994, 131.199997, 136.320007, 117.08859299999999,
        615015],
       [135.199997, 136.570007, 134.330002, 135.639999, 116.504501, 352835],
       [131.419998, 133.5, 130.759995, 131.779999, 113.18906399999999,
        209805]], dtype=object)

each element is a Python float, and is formatted individually, regardless of the value. Note how some strings are long, other much short.

In:

a = a.astype('float64')                # converting to `float64`
a
array([[  1.31199997e+02,   1.37149994e+02,   1.31199997e+02,
          1.36320007e+02,   1.17088593e+02,   6.15015000e+05],
       [  1.35199997e+02,   1.36570007e+02,   1.34330002e+02,
          1.35639999e+02,   1.16504501e+02,   3.52835000e+05],
       [  1.31419998e+02,   1.33500000e+02,   1.30759995e+02,
          1.31779999e+02,   1.13189064e+02,   2.09805000e+05]])

the array is displayed as a whole, using a format that will work equally well for the smaller values (1e2, 100) and the larger ones (1e5, 100000). With this format it use neat columns, show the 2d array structure.

While you can control how numpy displays such an array, it does not change the underlying numeric values. And for fast numpy calculations, you want this numeric dtype, not the object one.

Try df.sample(3).values[:,1:-1]. That should just be the float values that are all around 100. It's the last integer column with values like 209805 that's triggering the scientific notation.

Better yet, select the columns from the dataframe, "Open,High,Low,Close,Adj Close" before applying .values. Those are all float64 dtype, and the resulting array will have that dtype as well. Select the integer volume column separately. You are already treating the string/object date column separately.

Try:

a = df[df.columns[1:-1]].sample().values
2 of 2
1

Try adding:

np.set_printoptions(suppress=True)

As the first line under import numpy as np.

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 32491842 โ€บ how-to-convert-from-a-dtypeo-to-a-dtypefloat-in-numpy
python - How to convert from a dtype('o') to a dtype(float) in numpy? - Stack Overflow
CopyIn [126]: import csv import numpy as np with open("Data.csv") as sd: ri = [] dv = [] for row in csv.reader(sd): if row != ["ccx","ccy","ccz","cellVolumes","Cell Type"]: nrow = [] for val in row[0:3]: val = float(val) nrow.append(val) ri.append(nrow) nrow = [] for val in row[3:4]: val = float(val) nrow.append(val) dv.append(nrow) ri = np.array(ri) ri ... CopyOut[126]: array([[-0.179967, -0.38936, -0.46127], [-0.0633236, -0.407683, -0.542979], [-0.125841, -0.494202, -0.412042], ..., [-0.0116821, 0.764493, 0.573541], [0.630377, 0.469657, 0.442017], [0.248253, 0.615365, 0.354134]], dtype=object
Find elsewhere
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2165723 โ€บ how-to-convert-data-type-object-to-float-decimal
How to convert data type: Object to Float/Decimal | Sololearn: Learn to code for FREE!
In general, this should work data['A'] = data['A'].astype(float) https://code.sololearn.com/cBcy5u433HQ0/?ref=app ... Hello guys :) Thanks for your inputs. I've search and tried alot but this one works :) only on the line where I opening the csv file. df = pd.read_csv('filename.csv', thousands= ',' ,decimal=b'.') The "thousands" and "decimals" ;) Thank you guys for the response! ... Yup. I've got, "TypeError: cannot convert the series to <class 'int'>
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 73202014 โ€บ convert-dtype-from-object-to-float
python - convert dtype from object to float - Stack Overflow
I tried doing this and I managed ... but it doesnt work. ... You need to fill-in na values with some default float value like 0 or something depending on the business domain....
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.types.html
Data types โ€” NumPy v2.5 Manual
NumPy numerical types are instances of numpy.dtype (data-type) objects, each having unique characteristics. Once you have imported NumPy using import numpy as np you can create arrays with a specified dtype using the scalar types in the numpy top-level API, e.g. numpy.bool, numpy.float32, etc.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ arrays.dtypes.html
Data type objects (dtype) โ€” NumPy v2.5 Manual
Whenever a data-type is required in a NumPy function or method, either a dtype object or something that can be converted to one can be supplied. Such conversions are done by the dtype constructor: What can be converted to a data-type object is described below: ... Used as-is. ... The default data type: float64.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ using-numpy-to-convert-array-elements-to-float-type
Using NumPy to Convert Array Elements to Float Type - GeeksforGeeks
July 15, 2025 - Explanation: Here, a string array a is converted to a float array res using astype(float), creating a new array without modifying the original. NumPy allows you to define the type of elements directly during the creation of the array using the dtype parameter.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how do i convert a object to a float.
r/learnpython on Reddit: How do I convert a object to a float.
April 7, 2022 -

Hi guys, im struggeling with my dataframe.

Im trying to get a percentage of 2 columns, but when I divide the 2 I get this error: unsupported operand type(s) for /: 'float' and 'str'

The dtypes of the columns are an 'object'

Im aware that I have empty data in my columns, but is that an issue?

Would appreciate your help!

๐ŸŒ
Arab Psychology
scales.arabpsychology.com โ€บ home โ€บ how do i convert an object to a float in pandas?
How Do I Convert An Object To A Float In Pandas?
November 29, 2025 - import pandas as pd #create DataFrame ... points object assists int64 dtype: object ยท The following code shows how to use the astype() function to convert the points column in the DataFrame from an object to a float...
Top answer
1 of 1
2

When I copy-n-paste through the x0 definition:

In [41]: x0                                                                                          
Out[41]: array([0, 0])

and do a test calculation at the starting point:

In [42]: model(x0, 0)                                                                                
/usr/local/bin/ipython3:29: VisibleDeprecationWarning: 
Creating an ndarray from ragged nested sequences (which is a 
list-or-tuple of lists-or-tuples-or ndarrays with different 
lengths or shapes) is deprecated. If you meant to do this, you 
must specify 'dtype=object' when creating the ndarray
Out[42]: 
array([0,
       array([100.        ,  99.49028159,  97.9663226 ,  95.44365884,
        91.94800728,  87.51500386,  82.18984026,  76.02680317,
        69.08872084,  61.44632264,  53.17751801,  44.36660217,
        35.10339685,  25.48233457,  15.60149599,   5.56161002,
        -4.53497306, -14.58532495, -24.48698867, -34.139023  ,
       -43.44303157, -52.30416587, -60.63209224, -68.34191273,
       -75.35503059, -81.59995152, -87.01301249, -91.53903077,
       -95.13186646, -97.75489286, -99.38136988, -99.99471662,
       -99.58868039, -98.16740047, -95.74536592, -92.34726785,
       -88.00774772, -82.7710442 , -76.69054217, -69.8282285 ,
       -62.25406016, -54.04525101, -45.28548466, -36.0640614 ,
       -26.47498782, -16.61601846,  -6.58765929,   3.5078569 ,
        13.56761271,  23.48905528,  33.17104177,  42.51487044,
        51.42528687,  59.81145498,  67.58788309,  74.67529543,
        81.00144031,  86.50182667,  91.12038155,  94.81002171,
        97.53313359,  99.26195678,  99.97886703,  99.67655589,
        98.35810522,  96.03695581,  92.73677031,  88.49119201,
        83.34350191,  77.34617746,  70.56035759,  63.05521944,
        54.90727317,  46.19958194,  37.02091515,  27.46484351,
        17.62878515,   7.61301246,  -2.48037008, -12.54846682,
       -22.48863986, -32.19955543, -41.58221687, -50.54097388,
       -58.98449759, -66.8267116 , -73.98766951, -80.39436986,
       -85.9815004 , -90.69210386, -94.47815861, -97.30106822,
       -99.1320549 , -99.95245291, -99.7538988 , -98.53841671,
       -96.31839771, -93.11647348, -88.96528564, -83.90715291])],
      dtype=object)
In [43]: _.shape                                                                                     
Out[43]: (2,)

model returns a 2 element object array containing a scalar 0, and a (100,) array. That's not what odeint can work with!

๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.DataFrame.convert_dtypes.html
pandas.DataFrame.convert_dtypes โ€” pandas 3.0.6 documentation
Convert columns from numpy dtypes to the best dtypes that support pd.NA. ... Whether object dtypes should be converted to the best possible types. ... Whether object dtypes should be converted to StringDtype(). ... Whether, if possible, conversion can be done to integer extension types. ... Whether object dtypes should be converted to BooleanDtypes(). ... Whether, if possible, conversion can be done to floating ...