Error: only length-1 arrays can be converted to Python scalars - python

def answer_six():
census_df = pd.read_csv('census.csv')
copy = census_df.copy()
states = copy['STNAME'].unique()
counties = copy['CTYNAME']
play = copy.copy()
play = play.set_index(['STNAME','CTYNAME'])
copy = copy.set_index(['STNAME'])
def population_with_top3 (state):
counties = copy.loc[state]['CTYNAME'].values
population_array = list(map(lambda county:int(play.loc[state,county]['CENSUS2010POP'].values),counties))
population_array.sort(reverse = True)
population = population_array[1] + population_array[2] + population_array[3]
return {'STNAME': state, 'POP': population}
states_with_pop = list(map(population_with_top3,states))
return states_with_pop
answer_six()
when running the code I get:
TypeError: only length-1 arrays can be converted to Python scalars
Does anybody have any experience with this kind of issue?
Thanks!

This error happens when you try to use an array in place of single value.
I think the error is in this part of your code.
int(play.loc[state,county]['CENSUS2010POP'].values)
int takes only a single value for typecasting and .values returns an array. But if the array is of size one int will ignore that its an array and take the 1st element. In you case i think your play.loc[,][''].values is returning more than one value. This happens if there is more than one row with same state name, same county name and same CENSUS2010POP.

Related

How Can I Convert a String to a Ufunc?

I am trying to write a simple function that converts an input written in LaTeX to a ufunc that can be read by numpy. So far, I'm testing the function by converting \sin(x) to np.sin(x) (ignoring the conversion from {} to (). I'm not that familiar with ufuncs so I keep running into an error when trying to convert the \sin(x) string to the np.sin(x) ufunc for any given x.
import numpy as np
latexEq = input()
def latexMap(latexEq):
if "\sin" in latexEq:
sinLatex = latexEq.replace("\sin", "np.sin")
else:
pass
return sinLatex
npOutput = np.ufunc(latexMap(latexEq))
print(f"f(x)={numpyOutput}")
Similar to how an integer may be converted into a string str(2), I tried enclosed both sinLatex, latexEq, "np.sin", and npOutput but the error I always receive is
TypeError: cannot create 'numpy.ufunc' instances
Any explanation would be appreciated.
So if you just have npOutput = "np.sin" (without the (x)) you can do
# function_name = getattr(module, function_name_string)
npOutput = getattr(np, npOutput.lstrip("np."))
which gives you the function np.sin. The parentheses have to be gone because otherwise it would be a function call, not a function.

Indexing a list of arrays in *args of a defined python function

I have a variable length list of 2-D arrays that I'd like to optionally pass to a python function. This is a scaled back version of my first attempt.
list_of_2Darrays = [array_a,array_b,array_c]
def viol(required,*args):
dist_a = args[0]
dist_b = args[1]
dist_c = args[2]
sum = dist_a + dist_b + dist_c
output = viol(required,list_of_2Darrays)
It seemed to work for "dist_a", but when it got to the "1" position, it yielded: "IndexError: tuple index out of range". From there, I read this question here which explained that *args is read as a tuple and I can't index through a tuple.
I tried converting the tuple to a list with list(args) but then I received "IndexError: list index out of range. Next I tried implementing the approach in the question I shared above 1 but it gave me an "AttributeError: 'NoneType' object is not subscriptable." I tried to make it subscriptable using an approach I found on this other site which uses the sort function.
list_of_2Darrays = [array_a,array_b,array_c]
def foo(*args):
print(args)
def viol(required,*args):
temp = foo(args)
dists = temp.sort()
dist_a = dists[0]
dist_b = dists[1]
dist_c = dists[2]
sum = dist_a + dist_b + dist_c
output = viol(required,list_of_2Darrays)
This yielded the following error, "AttributeError: 'NoneType' object has no attribute 'sort'".
I'm sure I'm missing something simple, or just not coding this in an efficient way. If you can provide any advice, I'd greatly appreciate it.
edit: Sometimes the list of arrays is 3 elements, sometimes 5. The "required" argument essentially tells it the length of the list it's going to receive and then there's a series of if/elif/else statements that direct to the different types of calculations.

TypeError: float() argument must be a string or a number, not 'method'

I am trying to convert the latitude and longitude to zipcodes for around 10k data points. I am using geocoder for the task.
lat = subsamp['Latitude'].as_matrix
long = subsamp['Longitude'].as_matrix
g = geocoder.google([lat, long], method='reverse')
zip = g.postal
But, on executing the geocoder I get the error:
TypeError: float() argument must be a string or a number, not 'method'
I tried running it using a Pandas series then Numpy array but does not work.
Its a Missing parentheses issue for .as_matrix,
pandas.DataFrame.as_matrix, is a method
used to convert the frame to its Numpy-array representation.
As it is a function, you missed the (), you have not added () function parenthesis, for .as_matrix.
lat = subsamp['Latitude'].as_matrix
long = subsamp['Longitude'].as_matrix
It should be as follows :
lat = subsamp['Latitude'].as_matrix()
long = subsamp['Longitude'].as_matrix()
zip is a number or string but you have assigned a function to this value.
zip = g.postal -> zip = g.postal()

How to return multiple strings from a script to the rule sequence in booggie 2?

This is an issue specific to the use of python scripts in booggie 2.
I want to return multiple strings to the sequence and store them there in variables.
The script should look like this:
def getConfiguration(config_id):
""" Signature: getConfiguration(int): string, string"""
return "string_1", "string_2"
In the sequence I wanna have this:
(param_1, param_2) = getConfiguration(1)
Please note: The booggie-project does not exist anymore but led to the development of Soley Studio which covers the same functionality.
Scripts in booggie 2 are restricted to a single return value.
But you can return an array which then contains your strings.
Sadly Python arrays are different from GrGen arrays so we need to convert them first.
So your example would look like this:
def getConfiguration(config_id):
""" Signature: getConfiguration(int): array<string>"""
#TypeHelper in booggie 2 contains conversion methods from Python to GrGen types
return TypeHelper.ToSeqArray(["string_1", "string_2"])
return a tuple
return ("string_1", "string_2")
See this example
In [124]: def f():
.....: return (1,2)
.....:
In [125]: a, b = f()
In [126]: a
Out[126]: 1
In [127]: b
Out[127]: 2
Still, it's not possible to return multiple values but a python list is now converted into a C#-array that works in the sequence.
The python script itself should look like this
def getConfiguration(config_id):
""" Signature: getConfiguration(int): array<string>"""
return ["feature_1", "feature_2"]
In the sequence, you can then use this list as if it was an array:
config_list:array<string> # initialize array of string
(config_list) = getConfigurationList(1) # assign script output to that array
{first_item = config_list[0]} # get the first string("feature_1")
{second_item = config_list[1]} # get the second string("feature_2")
For the example above I recommend using the following code to access the entries in the array (in the sequence):
config_list:array<string> # initialize array of string
(config_list) = getConfigurationList(1) # assign script output to that array
{first_item = config_list[0]} # get the first string("feature_1")
{second_item = config_list[1]} # get the second string("feature_2")

Python 2.6 numpy interaction array objects error

I have a multi-dimensional array of objects. I want to interate over the objects using the nditer iterator.
Here is a code example:
import numpy as np
class Test:
def __init__(self,a):
self.a = a
def get_a(self):
return self.a
b = np.empty((2,3),dtype = object)
t_00 = Test(0)
t_01 = Test(1)
t_11 = Test (11)
b[0,0] = t_00
b[0,1] = t_01
b[1,1] = t_11
for item in np.nditer(b,flags = ["refs_ok"]):
if item:
print item.get_a()
I would expect the "item" to contain the object reference that I can use to access data.
However I am getting the following error:AttributeError: 'numpy.ndarray' object has no attribute 'get_a'
My question is how can I go thru the array to access the object in the array?
The array.flat method of iteration will work, and can confirm that this works as you'd expect
for item in b.flat:
if item:
print item.get_a()
Iterating over an array with nditer gives you views of the original array's cells as 0-dimensional arrays. For non-object arrays, this is almost equivalent to producing scalars, since 0-dimensional arrays usually behave like scalars, but that doesn't work for object arrays.
If you were determined to go through nditer for this, you could extract the elements from the 0-dimensional views with the item() method:
for element in np.nditer(b,flags = ["refs_ok"]):
element = element.item()
if element:
print(element.get_a())

Categories

Resources