I have a numpy array with several strings e.g. '4:30:30' or '3:29:11'
I write these values in a wxpython grid.
The other way arround doesn not work, why?
arr[0] = self.grid.GetCellValue(0,0)
error message:
ValueError: invalid literal for float(): 4:2:21
Some experiments with the array (dtype = None) did not help...
Anyone an idea?
Related
I am getting TypeError after converting pandas dataframe to numpy array (after using pd.get_dummies or by creating dummy variables from the dataframe using df.apply function) if the columns are of mixed types int, str and float.
I am not getting these errors if only using mixed types int, and str.
code:
df = pd.DataFrame({'a':[1,2]*2, 'b':['m','f']*2, 'c':[0.2, .1, .3, .5]})
dfd = pd.get_dummies(df, drop_first=True, dtype=int)
dfd.values
Error: TypeError: '<' not supported between instances of 'str' and 'int'
I am getting error with dfd.to_numpy() too.
Even if I convert the dataframe dfd to int or float values using df.astype,
dfd.to_numpy() is still producing error. I am getting error even if only selecting columns which were not changed from df.
Goal:
I am encoding categorical features of the dataframe to one hot encoding, and then want to use SelectKBest with score_func=mutual_info_classif to select some features. The error produced by the code after fitting SelectKBest is same as the error produced by dfd.to_numpy() and hence I am assuming that the error is being produced when SelectKBest is trying to convert dataframe to numpy.
Besides, just using mutual_info_classif to get scores for corresponding features is working.
How should I debug it? Thanks.
pandas converting to numpy error for mixed types
I kept getting errors with a numpy ndarray with booleans not being accepted as a mask by a pandas structure when it occured to me that I may have the 'wrong' booleans. Edit: it was not a raw numpy array but a pandas.Index.
While I was able to find a solution, the only one that worked was quite ugly:
mymask = mymask.astype(np.bool_) #ver.1 does not work, elements remain <class 'bool'>
mymask = mymask==True #ver.2, does work, elements become <class 'numpy.bool_'>
mypdstructure[mymask]
What's the proper way to typecast the values?
Ok, I found the problem. My original post was not fully correct: my mask was a pandas.Index.
It seems that the pands.Index.astype is behaving unexpectedly (for me), as I get different behavior for the following:
mask = pindex.map(myfun).astype(np.bool_) # doesn't cast
mask = pindex.map(myfun).astype(np.bool_,copy=False) # doesn't cast
mask = pindex.map(myfun).values.astype(np.bool_) # does cast
Maybe it is actually a pandas bug? This result is surprising to me because I was under the impression that pandas is usually just calling the functions of the numpy arrays that it is based on. This is clearly not the case here.
I got the below error message. I have found some questions on Stack Overflow, and I tried their solutions but it didn't worked.
import numpy as np
R=0.9999 #Reflectivity
a=np.arange(0,100000,1,dtype=np.complex)
b=R**(a)
c=np.exp(np.complex(0,a))
Error:
c=np.exp(np.complex(0,a))
TypeError: only length-1 arrays can be converted to Python scalars
The error is in np.complex(0,a). It doesn't expect 'a' to be an array. Compare with:
c = np.exp(np.complex(0,a[0])).
Since a is already an array with complex numbers, can't you directly calculate it? (Although this will result in inf+0.j given the size of the exponent)
c = np.exp(a)
my question is related to this but I can't get that solution to work and didn't want to add my own scenario to the old question.
I have a 2D float numpy array, am running python 3.5.1 with numpy 1.10.4, and am trying to write out the array with
numpy.savetext(filename, arrayname, delimiter = ',')
which works beautifully with a 1D array.
I've tried the solution from the referenced post
with open(filename, 'ab') as f:
numpy.savetext(f, arrayname, delimiter = ',')
to no avail. Actually, I've tried this without the delimiter as well as with 'w', 'wb, 'a' and with formatting arguments, and always get the same error message:
TypeError: Mismatch between array dtype ('float64') and format specifier.
I need to write this 2D array to a file which will be read later into a panda dataframe (have been using read.csv). I understand this may be an issue with numpy.savetxt, so I'm looking for an alternative.
Please try a minimal example and post the result, since the following works for me:
import numpy as np
array1=np.array([[1,2],[3,4]])
np.savetxt('file1.txt', array1 , delimiter = ',')
file content:
1.000000000000000000e+00,2.000000000000000000e+00
3.000000000000000000e+00,4.000000000000000000e+00
I had the same error message - until I finally realized that the type of my output actually was a list, not a numpy array!
I am receiving a really unhelpful error message 'TypeError: narray.fields require' on doing the following;
I have a pandas data frame which I have converted to a numpy array using
df.as_matrix()
this is the numpy array "npArrayIN" shape: (3, 10)
I then need to create a feature class - here is the call to the arcpy function which has the list of 10 fields I want to create but which crashes returning the error. All numbers are floating point.
arcpy.da.NumPyArrayToFeatureClass(npArrayIN, outputShape, ("TID","X","Y","Z","H","D","WGS84Lat","WGS84Long","OFFSETA", "OFFSETB"), spRef)
Any suggestions gratefully received.
Thanks
Have you tried it with the "X","Y","Z" as the 1st three columns instead of leading it with "TID"?
Also, you may want to try it with only the xyz columns.