Typeerror: Unsupported Operand Type(S) for /: 'Str' and 'Str'

TypeError: unsupported operand type(s) for /: 'str' and 'str'

By turning them into integers instead:

percent = (int(pyc) / int(tpy)) * 100;

In python 3, the input() function returns a string. Always. This is a change from Python 2; the raw_input() function was renamed to input().

TypeError: unsupported operand type(s) for /: 'str' and 'str' thrown in pct_change

Here's a test of read_csv() using your file contents (columns are separated by two spaces, as in the question text):

import pandas as pd
base = pd.read_csv('base_start.txt')
print(f"columns\n{base.columns}")
print(base)

Results:

columns
Index(['1979-01-01 226.0'], dtype='object')
1979-01-01 226.0
0 1979-01-02 226.8
1 1979-01-03 218.6
2 1979-01-04 223.2
3 1979-01-05 225.5
4 1979-01-08 223.1
5 1979-01-09 224.0

It looks like it isn't detecting any separators but rather reading strings like '1979-01-09 224.0' as values in a single column, and it's also inferring that the first row is a column heading '1979-01-01 226.0'. So the error "TypeError: unsupported operand type(s) for /: 'str' and 'str'" raised by pct_change() is apparently referring to successive string values in this lone column.

You can try calling read_csv() and sim_leverage() like this:

import pandas as pd
def sim_leverage(proxy, leverage=1, expense_ratio = 0.0, initial_value=1.0):
pct_chg = proxy.pct_change(1)
pct_chg = (pct_chg - expense_ratio / 252) * leverage
sim = (1 + pct_chg).cumprod() * initial_value
sim[0] = initial_value
return sim

base = pd.read_csv('base_start.txt', sep=' ', header=None, engine='python')
print(f"base:\n{base}")
sim = sim_leverage(base[1])
print(f"sim:\n{sim}")

Results:

base:
0 1
0 1979-01-01 226.0
1 1979-01-02 226.8
2 1979-01-03 218.6
3 1979-01-04 223.2
4 1979-01-05 225.5
5 1979-01-08 223.1
6 1979-01-09 224.0
sim:
0 1.000000
1 1.003540
2 0.967257
3 0.987611
4 0.997788
5 0.987168
6 0.991150
Name: 1, dtype: float64

Note that if we don't use the engine-'python' argument, this code raises the following warning:

 ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
base = pd.read_csv('base_start.txt', sep=' ', header=None)

UPDATED:

Based on OP comments, here's an update to what I am seeing.

Contents of .csv input file:

Date,Close
1979-01-01,226.0
1979-01-02,226.8
1979-01-03,218.6
1979-01-04,223.2
1979-01-05,225.5
1979-01-08,223.1
1979-01-09,224.0

Python code:

import pandas as pd
def sim_leverage(proxy, leverage=1, expense_ratio = 0.0, initial_value=1.0):
pct_chg = proxy.pct_change(1)
pct_chg = (pct_chg - expense_ratio / 252) * leverage
sim = (1 + pct_chg).cumprod() * initial_value
sim[0] = initial_value
return sim

base = pd.read_csv('base_start.txt')
print(f"base:\n{base}")
sim = sim_leverage(base['Close'])
print(f"sim:\n{sim}")

Alternative code for calling sim_leverage() (gives the same output):

sim = sim_leverage(base.iloc[:,1])

Output:

base:
Date Close
0 1979-01-01 226.0
1 1979-01-02 226.8
2 1979-01-03 218.6
3 1979-01-04 223.2
4 1979-01-05 225.5
5 1979-01-08 223.1
6 1979-01-09 224.0
sim:
0 1.000000
1 1.003540
2 0.967257
3 0.987611
4 0.997788
5 0.987168
6 0.991150
Name: Close, dtype: float64

Cannot define path: TypeError: unsupported operand type(s) for /: 'str' and 'str'

Just wrap the entire path in the f-string:

with open(f'insects/{dragonfly}.pdf', 'wb') 

TypeError: unsupported operand type(s) for /: 'str' and 'int' error in python

You will need to cast the inputs to int() where you explicitly expect them to be integers. All input captured from the terminal comes across as string. Attempting to add strings just concatenates them. Division is not supported on strings hence this error that / is an invalid operand for type str.

Facing this error :- TypeError: unsupported operand type(s) for -: 'str' and 'float'?

I`m not sure what you think this is doing, but it's not what you expect:

B = dict((literal_eval, i) for i in dict_A.items()) #convert k, v to int

That doesn't convert anything to int. The inner part of that:

(literal_eval, i)

will product a tuple that contains the literal_eval function object, and the item pairs from your dictionary. I'm guessing you didn't print(B) to see what you were building. Since all of the items have the same key, what you end up with is this:

>>> B = dict((literal_eval, i) for i in t.items()) #convert k, v to int
>>> B
{<function literal_eval at 0x7f531ddcb5e0>: ('4', '[3,5]')}

What you actually want is to CALL literal_eval:

>>> B = dict((literal_eval(k), literal_eval(v)) for k,v in t.items())
>>> B
{1: [0, 0], 2: [5, 0], 3: [6, 0], 4: [3, 5]}
>>>

TypeError: unsupported operand type(s) for -: 'str' and 'str' in python

You need to convert str to a number type to do - operation on them:

slope = (float(y2)-float(y1))/(float(x2)-float(x1))

(or you may use int() instead of float() depending on your demand)

TypeError: unsupported operand type(s) for +=: 'dict' and 'str'

As the exception message points out, the line student_grades += key isn't doing anything that makes sense. student_grades is an (initially empty) dictionary, and key is a string with the name of a student in it. You can't add those things together. I'm not even sure what you intended it to do, so I can't exactly tell you what to do differently to achieve your objective.

What I do see is that you're modifying the dictionary with the input scores in place in the if/elif blocks. Perhaps you don't want to be doing that? If you changed all the assignment statements to use student_grades instead of student_scores (while leaving the references to students_scores in the if and elif conditions), you'd probably get what you wanted, without any extra steps:

for key in student_scores:
if student_scores[key] >= 91:
student_grades[key] = "Outstanding" # change to student_grades here
elif student_scores[key] >= 81:
student_grades[key] = "Exceeds Expectations" # and here
elif student_scores[key] >= 71:
student_grades[key] = "Acceptable" # and here
elif student_scores[key] <= 70:
student_grades[key] = "Fail" # here too

As a bonus, you won't have clobbered the student_scores dict after this loop, so you can still refer back to it if necessary.



Related Topics



Leave a reply



Submit