CSV文件无法在Pandas

时间:2018-01-23 05:43:47

标签: python pandas csv dataframe trading

我正在尝试将从Yahoo Finance下载的CSV文件导入到作为数据帧的pandas中。美国股票的作用,例如代号SPY或FB,但在加拿大交易所上市的股票不会,例如SU或CRE。在文本编辑器中打开时,来自Yahoo的CSV文件显示完全相同:

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

每个格式都完全相同(例如,每个日期都是YYYY-MM-DD。)但是我收到了这个错误:" ValueError:只能将大小为1的数组转换为Python标量&# 34;任何时候我尝试加拿大的股票代码。这是我的代码:

import os
import pandas as pd
import matplotlib.pyplot as plt

def symbol_to_path(symbol, base_dir="data"):
    """Return CSV file path given ticker symbol."""
    return os.path.join(base_dir, "{}.csv".format(str(symbol)))

def get_data(symbols, dates):
    """Read stock data (adjusted close) for given symbols from CSV files."""
    df = pd.DataFrame(index=dates)
    if 'SPY' not in symbols:  # add SPY for reference, if absent
        symbols.insert(0, 'SPY')

    for symbol in symbols:
        df_temp = pd.read_csv(symbol_to_path(symbol), index_col='Date',
                              parse_dates=True,
                              usecols=['Date', 'Adj Close'],
                              na_values=['nan'])
        df_temp = df_temp.rename(columns={'Adj Close': symbol})
        print df_temp
        df = df.join(df_temp)
        if symbol == 'SPY':  # drop dates SPY did not trade
            df = df.dropna(subset=["SPY"])

    return df


def plot_data(df, title="Stock prices", xlabel="Date", ylabel="Price"):
    """Plot stock prices with a custom title and meaningful axis labels."""
    ax = df.plot(title=title, fontsize=12)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    plt.show()


def compute_daily_returns(df):
    """Compute and return the daily return values."""
    # TODO: Your code here
    # Note: Returned DataFrame must have the same number of rows
    # daily_returns = df.copy() # copy DataFrame to match size and column names
    # compute daily returns for row 1 onwards
    # daily_returns[1:] = (df[1:] / df[:-1].values) - 1
    # daily_returns.ix[0, :] = 0 # set daily returns for row 0 to 0

    # Another way to compute daily returns using .shift
    daily_returns = (df / df.shift(1)) - 1
    return daily_returns

def compute_cumulative_returns(df):
    cumulative_return = df.copy()
    cumulative_return = (df.iloc[:] / df.iloc[0]) - 1
    return cumulative_return


def normalize_data(df):
  # Normalize stock prices by dividing by the first row of the dataframe
  return df / df.iloc[0,:]

def test_run():
    # Read data
    dates = pd.date_range('2017-01-01', '2017-12-31')
    # SPY, FB both work perfectly, SU breaks
    symbols = ['SPY', 'FB', 'SU']
    df = get_data(symbols, dates)

    plot_data(df)
    plot_data(normalize_data(df), title="Normalized return", ylabel="Normalized return")

    # Compute daily returns
    daily_returns = compute_daily_returns(df)
    plot_data(daily_returns, title="Daily returns", ylabel="Daily returns")

    # Compute cumulative returns
    cumulative_returns = compute_cumulative_returns(df)
    plot_data(cumulative_returns, title="Cumulative returns", ylabel = "Cumulative return", xlabel = "Time")

if __name__ == "__main__":
    test_run()

此代码假定在正在运行的目录中,有一个名为' data'的文件夹。 (不带引号)包含与“'符号”中提供的代码相同的CSV文件。 test_run()中的数组。从这里为SU下载的数据:

https://ca.finance.yahoo.com/quote/SU.TO/history?period1=789894000&period2=1516604400&interval=1d&filter=history&frequency=1d

来自SPY:

https://ca.finance.yahoo.com/quote/SPY/history?period1=728290800&period2=1516604400&interval=1d&filter=history&frequency=1d

我知道SPY在SU之前开始交易,但FB开始交易的时间比SPY晚得多,并且可以使用提供的代码。

1 个答案:

答案 0 :(得分:1)

SU.TO.csv文件包含' null'值。

例如第68行:

1995-04-13,2.383130,2.383130,2.359380,2.359380,0.963797,3283200
1995-04-14,null,null,null,null,null,null
1995-04-17,2.390620,2.390620,2.359380,2.359380,0.963797,600000

所以你必须在你的代码中处理它。

也许您可以使用na_values=['null']代替na_values=['nan']