Here is what the CVS file sp50019500103to20161007csv looks l

Here is what the CVS file (sp500_1950-01-03_to_2016-10-07.csv) looks like in Python window:

Date,Open,High,Low,Close,Volume,Adj Close

1950-01-03,16.66,16.66,16.66,16.66,1260000,16.66

1950-01-04,16.85,16.85,16.85,16.85,1890000,16.85

1950-01-05,16.93,16.93,16.93,16.93,2550000,16.93

1950-01-06,16.98,16.98,16.98,16.98,2010000,16.98

1950-01-09,17.08,17.08,17.08,17.08,2520000,17.08

1950-01-10,17.030001,17.030001,17.030001,17.030001,2160000,17.030001

1950-01-11,17.09,17.09,17.09,17.09,2630000,17.09

1950-01-12,16.76,16.76,16.76,16.76,2970000,16.76

1950-01-13,16.67,16.67,16.67,16.67,3330000,16.67

1950-01-16,16.719999,16.719999,16.719999,16.719999,1460000,16.719999

1950-01-17,16.860001,16.860001,16.860001,16.860001,1790000,16.860001

1950-01-18,16.85,16.85,16.85,16.85,1570000,16.85

1.    Write a function convert_date_to_float that takes a string of the format yyyy-mm-dd and converts it to a floating point number. Thus, \'19500131\' as input results in a return value of 19500131.0

2.    Reads the contents of the data file into an array data2 without using genfromtxt or any other pre-written file-reading function. Thus, you have to create a file object, use string methods, etc. Make the first column be yyyy-mm-dd as a floating point number. You can use the function you wrote for #1 to do this. For this task, you do not have to write a function.

3.    Reads the contents of the data file into an array data3 using genfromtxt. Using what we\'ve covered in this class, the first column of the array this call to genfromtxt returns will be filled with \"nan\"s (i.e., \"not-a-number\" values). That\'s okay. For this task, you do not have to write a function.

4.    Compare all values (except the first column) in data2 and data3 to confirm they are \"equal\" to each other. Since they\'re all floating point values, you\'re checking that they are \"close\" to each other. Hint: There\'s a function in NumPy called allclose that will help here. For this task, you do not have to write a function.

Solution

1)


def convert_date_to_float(date):
   date=date.replace(\"-\",\"\")
   fdate=float(date)
   return fdate

2)

import csv
csvdata = open(\'sp500_1950-01-03_to_2016-10-07.csv\', \"r\")

readFile = csv.reader(csvdata)
datalist = list(readFile)

print(datalist)

3)

For this question numpy should be installed...


import numpy
data = numpy.genfromtxt(datafile, delimiter = \',\')

Here is what the CVS file (sp500_1950-01-03_to_2016-10-07.csv) looks like in Python window: Date,Open,High,Low,Close,Volume,Adj Close 1950-01-03,16.66,16.66,16.
Here is what the CVS file (sp500_1950-01-03_to_2016-10-07.csv) looks like in Python window: Date,Open,High,Low,Close,Volume,Adj Close 1950-01-03,16.66,16.66,16.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site