casting

Is it possible to get an error when accidentally assigned wrong type in python?

Is it possible to get an error when accidentally assigned wrong type in python? Question: For example I have next code: name = str(‘John Doe’) Let’s imagine after I assign to name = 1, but code still valid. Is it possible to get an error in this case in Python or within some special tool? …

Total answers: 2

Python ensure function parameter is always a string

Python ensure function parameter is always a string Question: I’m trying to make a program with a function that only accepts strings. How can I make a python function parameter always be a string and throw an error if it’s not? I’m looking for something like: def foo(i: int): return i foo(5) foo(‘oops’) Except this …

Total answers: 1

Pytorch getting RuntimeError: Found dtype Double but expected Float

Pytorch getting RuntimeError: Found dtype Double but expected Float Question: I am trying to implement a neural net in PyTorch but it doesn’t seem to work. The problem seems to be in the training loop. I’ve spend several hours into this but can’t get it right. Please help, thanks. I haven’t added the data preprocessing …

Total answers: 2

How to pass a numbers list to function using command line sys.argv Python3

How to pass a numbers list to function using command line sys.argv Python3 Question: I want to use sys.argv[] to get a list of 5 numbers from the command line to a function (def calc_avg), so they can be averaged. But I keep getting – TypeError: int() argument must be a string, a bytes-like object …

Total answers: 1

How to change multiple columns' types in pyspark?

How to change multiple columns' types in pyspark? Question: I am just studying pyspark. I want to change the column types like this: df1=df.select(df.Date.cast(‘double’),df.Time.cast(‘double’), df.NetValue.cast(‘double’),df.Units.cast(‘double’)) You can see that df is a data frame and I select 4 columns and change all of them to double. Because of using select, all other columns are ignored. …

Total answers: 4

h2o frame from pandas casting

h2o frame from pandas casting Question: I am using h2o to perform predictive modeling from python. I have loaded some data from a csv using pandas, specifying some column types: dtype_dict = {‘SIT_SSICCOMP’:’object’, ‘SIT_CAPACC’:’object’, ‘PTT_SSIRMPOL’:’object’, ‘PTT_SPTCLVEI’:’object’, ‘cap_pad’:’object’, ‘SIT_SADNS_RESP_PERC’:’object’, ‘SIT_GEOCODE’:’object’, ‘SIT_TIPOFIRMA’:’object’, ‘SIT_TPFRODESI’:’object’, ‘SIT_CITTAACC’:’object’, ‘SIT_INDIRACC’:’object’, ‘SIT_NUMCIVACC’:’object’ } date_cols = [“SIT_SSIDTSIN”,”SIT_SSIDTDEN”,”PTT_SPTDTEFF”,”PTT_SPTDTSCA”,”SIT_DTANTIFRODE”,”PTT_DTELABOR”] columns_to_drop = [‘SIT_TPFRODESI’,’SIT_CITTAACC’, ‘SIT_INDIRACC’, ‘SIT_NUMCIVACC’, ‘SIT_CAPACC’, ‘SIT_LONGITACC’, …

Total answers: 2

Python struct.error: integer out of range for 'q' format code

Python struct.error: integer out of range for 'q' format code Question: I’m having a problem, I want to cast a binary number into a double precission number. After some searchs, I found some way, but I’m still having a problem, I can cast “little” numbers but not the big ones (double precission), here’s my example …

Total answers: 2

Can a conversion to string raise an error?

Can a conversion to string raise an error? Question: I was wondering if converting to string i.e. str(sth) can raise an exception like for example float(sth) does? I am asking this to know if it is necessary to wrap my code in: try: x = str(value) except ValueError: x = None to be sure that …

Total answers: 4

Pandas reading csv as string type

Pandas reading csv as string type Question: I have a data frame with alpha-numeric keys which I want to save as a csv and read back later. For various reasons I need to explicitly read this key column as a string format, I have keys which are strictly numeric or even worse, things like: 1234E5 …

Total answers: 5

Convert Unicode data to int in python

Convert Unicode data to int in python Question: I am getting values passed from url as : user_data = {} if (request.args.get(‘title’)) : user_data[‘title’] =request.args.get(‘title’) if(request.args.get(‘limit’)) : user_data[‘limit’] = request.args.get(‘limit’) Then using it as if ‘limit’ in user_data : limit = user_data[‘limit’] conditions[‘id’] = {‘id’:1} int(limit) print type(limit) data = db.entry.find(conditions).limit(limit) It prints : <type …

Total answers: 2