I am trying to run the following code over a CSV file, but the code is showing an error that the input data type should be a str and not an int, but I have checked the data type and it is a float. I have tried every conversion from string, to float, to int, but nothing seems to work. please tell me what am I doing wrong.
print(stdized_data.X.dtypes)
for element in stdized_data:
if element != 0:
log(element + 1)
else:
log(element + 2)
###################################OUTPUT################################
float64
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-163-6e534ce31c6a> in <module>()
9 for element in stdized_data:
10 if element != 0:
---> 11 log(str(element) + 1)
12
13 else:
TypeError: must be str, not int
I have loaded the file using pd.read_csv function.
What you need to do is probably this -
log(str(element + 1))
What you are doing is -
log(str(element) + 1)
You have converted element to string but 1 is still an integer and you can't add string and integer
Related
I am trying to create a multiple alignment fasta file from an user search of a gen and a sequence. To create this fasta file I am using the following:
for seq in fasta_result:
ofile.write(">" + names[x] + "\n" + fasta_result[y] + "\n")
x +=1
y +=1
This is working fine as long as the name list is a simple list:
names = ['name_1','name_2','name_3','name_4']
It gives me a fasta file with the intended result. However, my intention is that instead of generic names like 'name_1', 'name_2', etc... I want to get the names of the genomes.
I have a function that does the job:
example = 'Rv0001'
genome = 'tuberculosis'
print(alignment_search(example, genome)
The result is a list:
['Rv0001', 'MSMEG_RS33460', 'MRA_RS00005', 'BQ2027_RS00005']
However, when I modify the first part of the code to have:
names = alignment_search(example, genome)
I get the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/var/folders/pq/ghtv3wj159j681vy0ny3tz9w0000gp/T/ipykernel_34390/940860269.py in <module>
18 genome = genome_location(before, after, alignment)
19
---> 20 filename = custom_fasta(genome, example, genome)
21
22 aln = AlignIO.read('/Users/dissertation/Documents/Dissertation-real/cgi-mycoapollo/db/genomes/' + filename + '_out.fasta','fasta')
/var/folders/pq/ghtv3wj159j681vy0ny3tz9w0000gp/T/ipykernel_34390/573793128.py in custom_fasta(locations_dict, example, genome)
55
56 for seq in fasta_result:
---> 57 for value in names:
58 ofile.write(">" + names[x] + "\n" + fasta_result[y] + "\n")
59
TypeError: 'NoneType' object is not iterable
I am not sure why it gives me this error, I have checked that the result of the function alignment_search(example, genome) is indeed a list type:
print(type(alignment_search(example, genome)))
<class 'list'>
So why is it working with a simple list but when it depends on the result of a function it gives the NoneType error?
Thanks!
I am trying to create a function that returns data starting from a given year and given month to today's year and month. For simplicity I have replaced what I want my function to do with print statements of outer loop and inner loop. I am getting error TypeError: 'str' object cannot be interpreted as an integer
Defining function
def frequency_database(df,start_month,start_year):
data = pd.DataFrame([])
import datetime
start_month=int(start_month)
start_year=int(start_year)
today = datetime.date.today()
today_year=today.strftime('%Y')
for y in range(start_year,today_year):
print('Outer loop enter for year', y)
Some function here which I want to do.............
for x in range(start_month,13):
print('Inner loop enter for month number',x)
Some function here which I want to do.............
Calling funtion
frequency_database(df,1,2015)
Error
TypeError: 'str' object cannot be interpreted as an integer
Stack Trace as requested
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-37-06567ae5d027> in <module>
12 print('Inner loop enter for month number',x)
13
---> 14 frequency_database(df,1,2015)
<ipython-input-37-06567ae5d027> in frequency_database(df, start_month, start_year)
7 today_year=today.strftime('%Y')
8
----> 9 for y in range(start_year,today_year):
10 print('Outer loop enter for year', y)
11 for x in range(start_month,13):
TypeError: 'str' object cannot be interpreted as an integer
The problem is you tried to give range a str which is today_year=today.strftime('%Y').
Just replace the line
today_year=today.strftime('%Y')
with
today_year=int(today.strftime('%Y'))
As pointed out by Stargazer, you could do,
today_year = today.year
instead of converting the str to int
Im currently working on an assignment but have encountered a problem. How do I convert a numpy.float64 to integer.
import numpy as np
bike = np.loadtxt('Bike.txt')
def count_days(year, month, day):
year_2011=[31,28,31,30,31,30,31,31,30,31,30,31]
year_2012=[31,29,31,30,31,30,31,31,30,31,30,31]
if (year == 2011):
days= sum(year_2011[:(month-1)])+day
else:
days= 365+sum(year_2012[:month-1])+day
return days
bike_2011 = bike[count_days(2011, 0, 0)-1]
bike_2012 = bike[count_days(2012, 0, 0)-1]
int(bike_2011)
int(bike_2012)
for e in len(bike_2012):
if bike[e] > 8000 : print (bike [e], x)
This returns the following error.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-25-f111e601c474> in <module>
1 bike_2012.astype(int)
----> 2 for e in len(bike_2012):
3 if bike[e] > 8000 : print (bike [e], x)
TypeError: object of type 'numpy.float64' has no len()
Replacing bike = np.loadtxt('Bike.txt') with bike = np.loadtxt('Bike.txt').astype(int) should do the trick. And while you're at it, you should delete int(bike_2011) and int(bike_2012) -- these do absolutely nothing.
On the other hand, you program has more severe problems, count_days returns a single number, so bike_2011 and bike_2012 are a plain variables, not lists or tuples, so trying to do len(bike_2011) will give you the same error, no matter if bike_2011 is integer or float. You cannot take a length of a float (or integer).
Im trying to convert midi files to csv, make changes, and then change back into midi. I wan to do this all in python using the py_midicsv module.
However, I run into an error when i try to follow the documentation on: https://github.com/timwedde/py_midicsv
#Convert back to csv
import py_midicsv
csv_string = py_midicsv.midi_to_csv("example.mid")
midi_object = py_midicsv.csv_to_midi(csv_string)
The code above is straight from the documentations but I run into the error:
ValueError Traceback (most recent call last)
<ipython-input-17-2cb6d586ec9e> in <module>
1 #Convert back to csv
2 import py_midicsv
----> 3 midi_object = py_midicsv.csv_to_midi(csv_string)
~/.local/lib/python3.6/site-packages/py_midicsv/csvmidi.py in parse(file)
44 pattern.append(track)
45 else:
---> 46 event = csv_to_midi_map[identifier](tr, time, identifier, line[3:])
47 track.append(event)
48 pattern.make_ticks_rel()
~/.local/lib/python3.6/site-packages/py_midicsv/csv_converters.py in to_AfterTouchEvent(track, time, identifier, line)
24
25 def to_AfterTouchEvent(track, time, identifier, line):
---> 26 cannel, value = map(int, line)
27 return AfterTouchEvent(tick=time, channel=channel, value=value)
28
ValueError: too many values to unpack (expected 2)
What does this error mean and how can I fix things?
cannel, value = map(int, line)
the problem is with this line.
you are trying to unpack this map object into 2 objects, but it consists of more than 2 object, so python don't know what to do with the rest of the values.
you will need to understand what this map object contains, and what data you need to get from it. try to print it and see what data it holds.
def main():
spiral = open('spiral.txt', 'r') # open input text file
dim = spiral.readline() # read first line of text
print(dim)
if (dim % 2 == 0): # check to see if even
dim += 1 # make odd
I know this is probably very obvious but I can't figure out what is going on. I am reading a file that simply has one number and checking to see if it is even. I know it is being read correctly because it prints out 10 when I call it to print dim. But then it says:
TypeError: not all arguments converted during string formatting
for the line in which I am testing to see if dim is even. I'm sure it's basic but I can't figure it out.
The readline method of file objects always returns a string; it will not convert the number into an integer for you. You need to do this explicitly:
dim = int(spiral.readline())
Otherwise, dim will be a string and doing dim % 2 will cause Python to try to perform string formatting with 2 as an argument:
>>> '10' % 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>>
Also, doing print(dim) outputed 10 instead of '10' because print automatically removes the apostrophes when printing:
>>> print('10')
10
>>>