从Yahoo!加载数据与熊猫融资

时间:2014-05-13 21:49:50

标签: python pandas

我正在通过Wes McKinney的书“Python For Data Analysis”和第139页“关联和协方差”下的工作,当我尝试运行他的代码从Yahoo!获取数据时,我收到错误金融。

这是我正在运行的:

#CORRELATION AND COVARIANCE
import pandas.io.data as web

all_data = {}
for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOG']:
    all_data[ticker] = web.get_data_yahoo(ticker, '1/1/2003', '1/1/2013')

price = DataFrame({tic: data['Adj Close']
                   for tic, data in all_data.iteritems()})
volume = DataFrame({tic: data['Volume']
                    for tic, data in all_data.iteritems()})

以下是我遇到的错误:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 390, in get_data_yahoo
    adjust_price, ret_index, chunksize, 'yahoo', name)
  File "C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 336, in _get_data_from
    hist_data = src_fn(symbols, start, end, retry_count, pause)
  File "C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 190, in _get_hist_yahoo
    return _retry_read_url(url, retry_count, pause, 'Yahoo!')
  File "C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 169, in _retry_read_url
    "return a 200 for url %r" % (retry_count, name, url))
IOError: after 3 tries, Yahoo! did not return a 200 for url 'http://ichart.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2000&d=0&e=1&f=2010&g=d&ignore=.csv'
>>> ... >>> >>> ... >>> 

对问题是什么有任何想法?

4 个答案:

答案 0 :(得分:7)

正如卡尔所指出的那样,自动收报机已经改变了雅虎返回未找到的页面的意义。

在从网络上轮询数据时,最好将试一试包裹在

之外
all_data = {}
for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOG']:
    try:
        all_data[ticker] = web.get_data_yahoo(ticker, '1/1/2003', '1/1/2013')
        price = DataFrame({tic: data['Adj Close']
                    for tic, data in all_data.iteritems()})
        volume = DataFrame({tic: data['Volume']
                    for tic, data in all_data.iteritems()})
    except:
        print "Cant find ", ticker

答案 1 :(得分:1)

遇到同样的问题并且改变了&#39; GOOG&#39;来自&#39; GOOGL&#39;一旦你按照这些说明从pandas.io.data切换到pandas_datareader.data,似乎工作正常。

http://pandas-datareader.readthedocs.org/en/latest/remote_data.html#yahoo-finance

答案 2 :(得分:1)

截至17/1/17,我从本页和其他几个方面拼凑了以下内容:

from pandas_datareader import data as web
# import pandas.io.data as web
import fix_yahoo_finance
import datetime

start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2017, 6, 1)

all_data={}
for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOGL']:
    all_data[ticker] = web.get_data_yahoo(ticker, start, end)

price = DataFrame({tic: data['Adj Close'] 
                   for tic, data in all_data.iteritems()})
volume = DataFrame({tic: data['Volume']
                     for tic, data in all_data.iteritems()})

答案 3 :(得分:0)

我正在使用下面的代码段加载Yahoo财务数据。


<?php
include('session.php');

 include_once 'orders_crud.php';

if(isset($_SESSION['login_user'])){

}
?>


<!DOCTYPE html>
<html>
<head>
   <link rel="icon" href="icon.ico">
  <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
  <title>EZ-Wedding4U : Senarai Tempahan</title>
  <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
   <style type="text/css">
      @import "main.css";


  </style>
</head>
<body>

  <?php include_once 'nav_bar3.php'; ?>

 <div class="container-fluid">

    <div class="row">
    <div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2">
      <div class="page-header">
        <h2>Tempahan Saya</h2>
      </div>
      <table class="table table-striped table-bordered">
        <tr>
          <th>Gambar</th>
          <th>Tempahan ID</th>
          <th>Tarikh Tempahan</th>
          <th>Nama Produk</th>
          <th>Kuantiti</th>
          <th>Harga per Unit</th>
          <th>Jumlah Harga (RM)</th>
          <th>Saiz</th>
          <th>Nama Pelanggan</th>
          <th>Alamat Pelanggan</th>
          <th>Notel Pelanggan</th>
          <th>Nama Syarikat</th>
          <th></th>
        </tr>

      <?php
      // Read
      $per_page = 5;
      if (isset($_GET["page"]))
        $page = $_GET["page"];
      else
        $page = 1;
      $start_from = ($page-1) * $per_page;

      try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("SELECT * FROM tbl_orders, tbl_pelanggan WHERE
          tbl_orders.nama_pelanggan = tbl_pelanggan.nama_penuh AND 
          tbl_pelanggan.nama_pengguna = '".$_SESSION['username']."' AND id =:oid  
          LIMIT $start_from, $per_page");

        $stmt->execute();
        $result = $stmt->fetchAll();
      }
      catch(PDOException $e){
            echo "Error: " . $e->getMessage();
      }
      foreach($result as $readrow) {
      ?>   
      <tr>
        <td> <?php if ($readrow['id'] == "" ) {
        echo "No image";
      }
      else { ?>
      <img src="produk/<?php echo $readrow['img'] ?>" class="img-responsive">
      <?php } ?></td>

        <td><?php echo $readrow['id']; ?></td>
        <td><?php echo $readrow['tarikh_tempah']; ?></td>
        <td><?php echo $readrow['nama_produk']; ?></td>
        <td><?php echo $readrow['kuantiti']; ?></td>
        <td><?php echo $readrow['harga']; ?></td>
        <td><?php echo $readrow['total_harga']; ?></td>
        <td><?php echo $readrow['saiz']; ?></td>
        <td><?php echo $readrow['nama_pelanggan']; ?></td>
        <td><?php echo $readrow['alamat_pelanggan']; ?></td>
        <td><?php echo $readrow['notel_pelanggan']; ?></td>
        <td><?php echo $readrow['namaSyarikat']; ?></td>



        <td>

          <a href="edit_tempahan.php?edit=<?php echo $readrow['id']; ?>" class="btn btn-success btn-xs" role="button"> Kemas kini </a>
          <a href="senarai_tempahan.php?delete=<?php echo $readrow['id']; ?>" onclick="return confirm('Anda pasti mahu membatalkan tempahan ini?');" class="btn btn-danger btn-xs" role="button">Batal</a>
        </td>
      </tr>
      <?php } ?>

    </table>
    </div>
  </div>
  <div class="row">
    <div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2">
      <nav>
          <ul class="pagination">
          <?php
          try {
            $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $stmt = $conn->prepare("SELECT * FROM tbl_orders, tbl_pelanggan 
              WHERE tbl_orders.nama_pelanggan = tbl_pelanggan.nama_penuh AND 
              tbl_pelanggan.nama_pengguna = '".$_SESSION['username']."' AND id = 
              :oid ");

            $stmt->execute();
            $result = $stmt->fetchAll();
            $total_records = count($result);
          }
          catch(PDOException $e){
                echo "Error: " . $e->getMessage();
          }
          $total_pages = ceil($total_records / $per_page);
          ?>
          <?php if ($page==1) { ?>
            <li class="disabled"><span aria-hidden="true">«</span></li>
          <?php } else { ?>
            <li><a href="senarai_tempahan.php?page=<?php echo $page-1 ?>" aria-label="Previous"><span aria-hidden="true">«</span></a></li>
          <?php
          }
          for ($i=1; $i<=$total_pages; $i++)
            if ($i == $page)
              echo "<li class=\"active\"><a href=\"senarai_tempahan.php?page=$i\">$i</a></li>";
            else
              echo "<li><a href=\"senarai_tempahan.php?page=$i\">$i</a></li>";
          ?>
          <?php if ($page==$total_pages) { ?>
            <li class="disabled"><span aria-hidden="true">»</span></li>
          <?php } else { ?>
            <li><a href="senarai_tempahan.php?page=<?php echo $page+1 ?>" aria-label="Previous"><span aria-hidden="true">»</span></a></li>
          <?php } ?>
        </ul>
      </nav>
    </div>

</div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  <script src="main.js">
      </script>
</body>
</html>

https://repl.it/repls/DevotedBetterAlgorithms