-1

I need some help. I am learning between matplotlib and numpy. I am simply reproducing a piece of code from "Intraday candlestick charts using Matplotlib" with my own csv file to learn from that code. The different part of my code that is different from it is the following:

import numpy as np 
import matplotlib.pyplot as plt 
import datetime

from mpl_finance import candlestick_ohlc 
from matplotlib.dates import num2date

# data in a text file, 5 columns: time, opening, close, high, low
# note that I'm using the time you formated into an ordinal float data = 
np.loadtxt("/Users/paul/Documents/python/Quant/INTC.csv",       delimiter=",")

I am getting an error that says

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

I even try to use this line and still gives me the same error message

data = np.genfromtxt("/Users/paul/Documents/python/Quant/INTC.csv", delimiter=",", skip_header=1, usecols=[0,1,2,3,4], dtype=(dt, float,float,float, float))"

it's probably a basic concept that I am not understanding. much appriciated for some guidance.

sample data:

Date,Open,High,Low,Close,Volume,Adj Close
2017-11-06,46.599998,46.740002,46.090000,46.700001,46.700001,34035000
2017-11-07,46.700001,47.090000,46.389999,46.779999,46.779999,24461400
2017-11-08,46.619999,46.700001,46.279999,46.700001,46.700001,21565800
2017-11-09,46.049999,46.389999,45.650002,46.299999,46.299999,25570400
2017-11-10,46.040001,46.090000,45.380001,45.580002,45.580002,24095400
2017-11-13,45.259998,45.939999,45.250000,45.750000,45.750000,18999000
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
fishtang
  • 167
  • 3
  • 9

1 Answers1

1

You can import "Date column' (string) by converting it to a datetime object. Once you have it as a datetime object you can filter out the weekends as shown below. you can plot the filtered data in matplotlib as matplotlib understands datetime objects. hope that helps.

'''
data in csv
Date,Open,High,Low,Close,Volume,Adj Close 
2017-12-06,46.599998,46.740002,46.090000,46.700001,46.700001,34035000 
2017-12-07,46.700001,47.090000,46.389999,46.779999,46.779999,24461400
2017-12-08,46.619999,46.700001,46.279999,46.700001,46.700001,21565800
2017-12-09,46.049999,46.389999,45.650002,46.299999,46.299999,25570400 
2017-12-10,46.040001,46.090000,45.380001,45.580002,45.580002,24095400
2017-12-13,45.259998,45.939999,45.250000,45.750000,45.750000,18999000
'''

import numpy as np
from datetime import datetime

# use converter to convert a string object to datetime object. Note dtype is object for all columns
data = np.genfromtxt(r'stock.csv', delimiter = ',', names = True,
                    converters={0: lambda x: datetime.strptime(x, "%Y-%m-%d")}, dtype=object)

print data

'''
[ (datetime.datetime(2017, 12, 6, 0, 0), '46.599998', '46.740002', '46.090000', '46.700001', '46.700001', '34035000')
 (datetime.datetime(2017, 12, 7, 0, 0), '46.700001', '47.090000', '46.389999', '46.779999', '46.779999', '24461400')
 (datetime.datetime(2017, 12, 8, 0, 0), '46.619999', '46.700001', '46.279999', '46.700001', '46.700001', '21565800')
 (datetime.datetime(2017, 12, 9, 0, 0), '46.049999', '46.389999', '45.650002', '46.299999', '46.299999', '25570400')
 (datetime.datetime(2017, 12, 10, 0, 0), '46.040001', '46.090000', '45.380001', '45.580002', '45.580002', '24095400')
 (datetime.datetime(2017, 12, 13, 0, 0), '45.259998', '45.939999', '45.250000', '45.750000', '45.750000', '18999000')]
'''

# check if a day is a weekday or not
def check_weekday_or_not(datetime_object):
    if datetime_object.weekday() not in [5,6]:
        # datetime.weekday() returns 5 and 6 for saturday and Sunday
        return True
    else:
        return False

# create a function to apply on each row of the matrix
vfunc =np.vectorize(check_weekday_or_not)     
filter_mask = vfunc(data['Date'])
print filter_mask
#[ True  True  True False False  True]

# Apply the filter mask to obtain an array without weekends.
print data[filter_mask]
'''
array([ (datetime.datetime(2017, 12, 6, 0, 0), '46.599998', '46.740002', '46.090000', '46.700001', '46.700001', '34035000'),
       (datetime.datetime(2017, 12, 7, 0, 0), '46.700001', '47.090000', '46.389999', '46.779999', '46.779999', '24461400'),
       (datetime.datetime(2017, 12, 8, 0, 0), '46.619999', '46.700001', '46.279999', '46.700001', '46.700001', '21565800'),
       (datetime.datetime(2017, 12, 13, 0, 0), '45.259998', '45.939999', '45.250000', '45.750000', '45.750000', '18999000')], 
      dtype=[('Date', 'O'), ('Open', 'O'), ('High', 'O'), ('Low', 'O'), ('Close', 'O'), ('Volume', 'O'), ('Adj_Close', 'O')])
'''
plasmon360
  • 4,109
  • 1
  • 16
  • 19
  • Hi @plasmon360. I had been reading everything on time.strptime() just to make sure I at least understand the concept. I do understand the solution is trying to do. It's trying to convert the dates into a set of number between 0 and 6. Monday being 0 and saturday being 6. once we align 0-5 on the x axis and can then convert them for dates again for display. However, I can't even get up to "print (data)" to work. – fishtang Dec 11 '17 at 00:19
  • I just use your code up to the print as follows: import numpy as np from datetime import datetime data = np.genfromtxt('/Users/paul/Documents/python/Quant/INTC.csv', delimiter = ',', names = True, converters={0: lambda x: datetime.strptime(x, "%Y-%m-%d")}, dtype=object) print (data) And it gave me this error: TypeError: strptime() argument 1 must be str, not bytes. I thought the cvs dates are already is str. does it have to do with my version? or I am just not getting it? thanks. – fishtang Dec 11 '17 at 00:20
  • I am not sure why it is not working, but may be some the date characters are unicode and it is loading them as bytes.I am guess you are using python3? may be you can try this converters={0: lambda x: datetime.strptime(x.decode('ascii'), "%Y-%m-%d")} – plasmon360 Dec 11 '17 at 02:13
  • it worked up to the print (data) line!! the only thing is it has a 'b' infront. is this correct? [ (datetime.datetime(2017, 11, 10, 0, 0), b'46.040001', b'46.090000', b'45.380001', b'45.580002', b'45.580002', b'24095400') there is another error after that. I will try to attempt to figure that part out. thanks again!! – fishtang Dec 11 '17 at 02:38
  • If this answer helps you. please upvote and accept the answer. thanks – plasmon360 Dec 11 '17 at 15:39
  • For Sure, but how would I do that? – fishtang Dec 11 '17 at 16:37