Could not convert string to float: '2021-04-09' when loading from another CSV file
As the title suggests, I am having problems loading raw csv into my code. It is giving ValueError, but I don't know why. I tried to turn my date column in numbers and it still gives me the same error
Here is my code:
if name == 'main':
cerebro = bt.Cerebro()
# Create a Data Feed data = bt.feeds.GenericCSVData( dataname='12 Apr 2021 _Raw.csv', fromdate=datetime.datetime(2021, 1, 1), todate=datetime.datetime(2021, 4, 5), nullvalue = 0.0, dtformat=('%Y-%m-%d'), datetime = 1, ) print(data) cerebro.adddata(data) cerebro.broker.setcash(100000.0) print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.run() print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.plot()
Any help is appreciated. Thank you so much!
Please share few lines if your csv file..
Thanks , so my csv compose of below
@punajunior It looks like the date format in your file is
"%m/%d/%Y"but you specified
"%Y-%m-%d"in your script. Try changing the
So i printed my DF , my date column format is "%Y-%m-%d"
I also tried changing the Dtformat to "%m/%d/%Y" but it gives me the same error
if i updated back to "%Y-%m-%d" it gives me the same error couldnt covert string to float.
@punajunior Can you just print the first few lines of the actual CSV file, not the output of Pandas (not even sure where you got it into a DF as you aren't using a Pandas data feed)? Just type
head "12 Apr 2021 _Raw.csv"at the command line.
Those last two errors you posted are for different issues. The first is that it's trying to parse the string '2021-04-09' as a date using the format '%m/%d/%Y' (which you fixed to use the correct format), but the second error is that it's trying to convert the string '2021-04-09' to a float. It makes me think in the second case your date is no longer in the second column (as you specified with
date time = 1in the GenericCSVData call) and it thinks that column is one of the price fields.
head "12 Apr 2021 _Raw.csv" <- this doesnt work.
so I just use texteditor to open my csv , so it goes like below
Date should be second column thus i am using "1" and i've also set the dtformat to match but it prompted the below
@punajunior My guess is that this error is thrown when it's trying to parse the "Open" field which defaults to column 1, so after parsing the datetime correctly, it's then trying to parse that same field as a float. Try adding explicit column indicators for all fields in your call to GenericCSVData:
BTW, it looks like you created the CSV file using Pandas
.to_csv()method without the
index=Falseparameter. That's why it has that unnamed first column. If you can recreate the file without the index, this probably would have just worked.