Matplotlib Tutorial 9 - converting data from the internet

preview_player
Показать описание
This tutorial is focused around converting the datestamps from the Yahoo finance API to times that Matplotlib understands. To do this, we're going to write a new function, bytespdate2num.

Рекомендации по теме
Комментарии
Автор

You know that moment when it finally works? Satisfying. Thanks man!! Great video

ronnymichael
Автор

I love your tutorials. this one is super complicated and I cannot for the life of me get this to work.

nm
Автор

here in above coed def bytesconverter(b): WHAT DOES parameter b referred TO ?

raviraaja
Автор

We can simply use pandas library to load the Data set

import pandas as pd
stock_data = pd.read_csv('paste the url of the file')

To convert the date column to datetime format :
stock['Date'] = pd.to_datetime(stock['Date'], format = '%Y-%m-%d')

Plotting the graph is similar:

plt.plot(stock.date, stock.Open)

tanmayagrawal
Автор

Thanks for the video. I'm learning lots from you. Haha

BrowFinGarf
Автор

Very awesome to be seeing some real results at this point! Confused on the bytespdate2num function -- just hoping that since this video was published that there is a solution where we don't have to implement our own function

kas
Автор

@sentdex: Your code seems a lot complicated. I tried a simpler version from your another tutorial, which seems to work just fine and is a way more easier to understand and implement. Have a look here:

from pandas_datareader import data, wb
import datetime
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2016, 1, 1)
df = data.DataReader('TSLA', 'yahoo', start, end)
df.reset_index(inplace = True)
plt.plot_date(df.Date, df.Close, '-')
plt.show()

That's it! It worked on a Jupyter notebook using Python 3 kernel, without the decoding function.

Your expert comments are highly welcomed! :P

vhoramuiz
Автор

1. link to the source code:
2. You also don't have to define bytespdate2num(fmt, encoding='utf-8'), it will work without.

Also realize that the new data is not representing the TESLA stock price (it starts in 2000)

DJaaaIntoYourAss
Автор

Excellent video. The plot showed up the first time. For some reason I didn't get that first error you did.
Side note: The price of TSLA has dropped a bit from were it was on your plot back in June 2015.

viridianroad
Автор

Just like yours i'm also getting ValueError: time data '1/2/17' does not match format '%m/%d/%y'. But this is not letting me run the program and display plot.

JagjotSingh
Автор

It can also be directly converted using csv files although code gets bigger but it is easier to understand that way.

abhishekchauhan
Автор


if len(split_line) == 7 (it must be 7 because there is a new column of data that was added)
if 'Date' not in line: (Values and Label can be removed and replace with 'Date' or just add it on in the line of code)

then in the line for np.loadtxt you need to add another variable and change the date format.

date, openp, highp, lowp, closep, adjclose, volume = np.loadtxt(

coverters={0: bytespdate2num('%Y-%m-%d')}) (just add dashes because the format is also different)

Then it should work, goodluck.

francoispaulsen
Автор

How to convert the dates so that we can plot it using matplotlib while the data frame is already loaded in pandas in python 3. I have this pandas dataframe which has a date field in string format. I need to convert it in matplotlib date time objects so that i can use it with plot_date

mayukh_
Автор

Hi sentdex,

I enjoy your videos and am planning to go through most of them. I am currently having trouble understanding the bytes to string conversion you made. I have spent a long time reading the help for functions online and browsing the internet for answers but still do not have complete understanding of what is happening. My questions are:

1. How does numpy.loadtxt converters function work without actually inputting the list into the bytespdate2num function? The bytespdate2num function only inputs the date format, not the data itself.

2. The mdates.strpdate2num function does not have an input when I read the help, yet you input the date format. How does Python interpret this?

3. Within the bytespdate2num function you have the bytesconverter function with the input b. In the last line of bytespdate2num function you return the bytesconverter function without the function input. How does bytesconverter work without defining the input, b, in your return line?

Thank you for any information you can provide!

sahgerlad
Автор

For those who are getting this error
'urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)>'
use the following code

import requests
from bs4 import BeautifulSoup

'and instead of -> source_code = ' write the following codes
page = requests.get(stock_price_url)
soup = BeautifulSoup(page.content, 'html.parser')
source_code =

iamkishankumar
Автор

Unfortunately, I could not follow from split_source = source_code.split('/n') till stock_data.append(line). Could you please explain why we did this?

chhabratanuj
Автор

Hi sentdex,

How is the date string passed to bytespdate2num?

david
Автор

For anyone watching this now, it appears that Yahoo has started to use https instead of http so I followed the tutorial at this link:
and it fixed the problem. All you have to do is import ssl
and change

source_code =

to

gcontext =
source_code = urllib.request.urlopen(stock_price_url, context = gcontext).read().decode()

then it will work.

runfunmc
Автор

If you get : "ValueError: Too many values to unpack", the new test page has 7 values, not 6. There is Adjusted_close.

j.n.hervey
Автор

Hi Sentdex - Loving the videos.
I think there's a big bug with the provided replacement data in this video. The date as (year-month-day) seems to send the day to the next line, creating jumbled data. I have year and month at the end of my line of data, and the day is on the next line. I'm sure this is easy to fix, but am not sure how to do that myself.

alexelliott