1- from pandas .util .py3compat import StringIO , BytesIO
2- from datetime import datetime
3- import csv
4- import os
5- import sys
6- import re
71import unittest
8- import pandas .io .data as pd
92import nose
10- from pandas .util .testing import network
3+ from datetime import datetime
4+
5+ from pandas .util .py3compat import StringIO , BytesIO
6+
7+ import pandas as pd
8+ import pandas .io .data as web
9+ from pandas .util .testing import (network , assert_frame_equal ,
10+ assert_series_equal ,
11+ assert_almost_equal )
1112from numpy .testing .decorators import slow
13+
1214import urllib2
1315
1416
@@ -21,16 +23,16 @@ def test_yahoo(self):
2123 # an excecption when DataReader can't get a 200 response from
2224 # yahoo
2325 start = datetime (2010 , 1 , 1 )
24- end = datetime (2012 , 1 , 24 )
26+ end = datetime (2013 , 01 , 27 )
2527
2628 try :
2729 self .assertEquals (
28- pd .DataReader ("F" , 'yahoo' , start , end )['Close' ][- 1 ],
29- 12.82 )
30+ web .DataReader ("F" , 'yahoo' , start , end )['Close' ][- 1 ],
31+ 13.68 )
3032
3133 self .assertRaises (
3234 Exception ,
33- lambda : pd .DataReader ("NON EXISTENT TICKER" , 'yahoo' ,
35+ lambda : web .DataReader ("NON EXISTENT TICKER" , 'yahoo' ,
3436 start , end ))
3537 except urllib2 .URLError :
3638 try :
@@ -40,7 +42,69 @@ def test_yahoo(self):
4042 else :
4143 raise
4244
45+ @slow
46+ @network
47+ def test_get_quote (self ):
48+ df = web .get_quote_yahoo (pd .Series (['GOOG' , 'AAPL' , 'GOOG' ]))
49+ assert_series_equal (df .ix [0 ], df .ix [2 ])
50+
51+ @slow
52+ @network
53+ def test_get_components (self ):
54+
55+ df = web .get_components_yahoo () #Dow Jones (default)
56+ assert isinstance (df , pd .DataFrame )
57+ assert len (df ) == 30
58+
59+ df = web .get_components_yahoo ('^GDAXI' ) #DAX
60+ assert isinstance (df , pd .DataFrame )
61+ assert len (df ) == 30
62+ assert df [df .name .str .contains ('adidas' , case = False )].index == 'ADS.DE'
63+
64+ df = web .get_components_yahoo ('^NDX' ) #NASDAQ-100
65+ assert isinstance (df , pd .DataFrame )
66+ assert len (df ) == 100
67+ #Usual culprits, should be around for a while
68+ assert 'AAPL' in df .index
69+ assert 'GOOG' in df .index
70+ assert 'AMZN' in df .index
71+
72+ @slow
73+ @network
74+ def test_get_data (self ):
75+ #single symbol
76+ #http://finance.yahoo.com/q/hp?s=GOOG&a=09&b=08&c=2010&d=09&e=10&f=2010&g=d
77+ df = web .get_data_yahoo ('GOOG' )
78+ assert df .Volume .ix ['OCT-08-2010' ] == 2859200
79+
80+ sl = ['AAPL' , 'AMZN' , 'GOOG' ]
81+ pan = web .get_data_yahoo (sl , '2012' )
82+ ts = pan .Close .GOOG .index [pan .Close .AAPL > pan .Close .GOOG ]
83+ assert ts [0 ].dayofyear == 96
84+
85+ dfi = web .get_components_yahoo ('^DJI' )
86+ pan = web .get_data_yahoo (dfi , 'JAN-01-12' , 'JAN-31-13' )
87+ expected = [19.02 , 28.23 , 25.39 ]
88+ result = pan .Close .ix ['01-18-12' ][['GE' , 'MSFT' , 'INTC' ]].tolist ()
89+ assert result == expected
90+
91+ pan = web .get_data_yahoo (dfi , 'JAN-01-12' , 'JAN-31-13' ,
92+ adjust_price = True )
93+ expected = [18.38 , 27.45 , 24.54 ]
94+ result = pan .Close .ix ['01-18-12' ][['GE' , 'MSFT' , 'INTC' ]].tolist ()
95+ assert result == expected
96+
97+ pan = web .get_data_yahoo (dfi , '2011' , ret_index = True )
98+ d = [[ 1.31810193 , 1.08170606 , 1.05281026 ],
99+ [ 1.31810193 , 1.09352518 , 1.05658242 ],
100+ [ 1.30228471 , 1.09815005 , 1.05054696 ],
101+ [ 1.30521383 , 1.08119219 , 1.03545832 ]]
102+
103+ expected = pd .DataFrame (d )
104+ result = pan .Ret_Index [['GE' , 'INTC' , 'MSFT' ]].ix [- 5 :- 1 ]
105+ assert_almost_equal (result .values , expected .values )
106+
107+
43108if __name__ == '__main__' :
44- import nose
45109 nose .runmodule (argv = [__file__ , '-vvs' , '-x' , '--pdb' , '--pdb-failure' ],
46110 exit = False )
0 commit comments