Christopher Pfeifer
Christopher Pfeifer

Reputation: 99

Decimal point acting bad - ValueError: could not convert string to float: '.'

I have SP500 data downloaded from the Fed, a very simple .csv file with two fields; date and price. When I do a pd.read_csv() to load into a dataframe I get two errors:

TypeError: Cannot cast array from dtype('O') to dtype('float64') according to the rule 'safe'

ValueError: could not convert string to float: '.'

It seems to be telling me that the decimal point '.' in the price field is throwing the error.

I have many files that handle the decimal point when converting a string object to a float.

here is the data being used:

observation_date,price
2008-04-04,1340.40
2008-04-07,1372.54
2008-04-08,1365.54
2008-04-09,1354.49
2008-04-10,1360.55
2008-04-11,1332.83
2008-04-14,1328.32
2008-04-15,1334.43
2008-04-16,1364.71

I have also used:

SP500 = pd.read_csv(csv_file)

to load the file give both fields as object dtype then using

SP500.price = SP500.price.astype(float).fillna(0.0)

this throws the same two errors

Upvotes: 2

Views: 868

Answers (1)

piRSquared
piRSquared

Reputation: 294218

Try specifying that '.' is a NaN value

SP500 = pd.read_csv('SP500.csv', na_values=['.'])

Upvotes: 1

Related Questions