0

我有一个 DataFrame,D1,如下所示:

close	Date	Symbol	ICO_to
2.71	6/12/2017 18:00	MYST	5/30/2017
2.18	6/13/2017 18:00	MYST	5/30/2017
2.1	6/14/2017 18:00	MYST	5/30/2017
2.17	6/15/2017 18:00	MYST	5/30/2017
2.34	6/16/2017 18:00	MYST	5/30/2017
2.24	6/17/2017 18:00	MYST	5/30/2017
3.32	6/18/2017 18:00	MYST	5/30/2017
2.73	6/19/2017 18:00	MYST	5/30/2017
2.03	6/20/2017 18:00	MYST	5/30/2017
2.1	6/21/2017 18:00	MYST	5/30/2017
2.26	6/22/2017 18:00	MYST	5/30/2017
2.17	6/23/2017 18:00	MYST	5/30/2017
1.88	6/24/2017 18:00	MYST	5/30/2017
1.64	6/25/2017 18:00	MYST	5/30/2017
1.96	6/26/2017 18:00	MYST	5/30/2017
1.79	6/27/2017 18:00	MYST	5/30/2017
1.69	6/28/2017 18:00	MYST	5/30/2017
1.45	6/29/2017 18:00	MYST	5/30/2017
1.38	6/30/2017 18:00	MYST	5/30/2017
1.35	7/1/2017 18:00	MYST	5/30/2017
1.37	7/2/2017 18:00	MYST	5/30/2017

我试图通过引用 Date 列(所有 ICO_to 值都相同)在 ICO_to 值之后的第 30 天将回报率提高到收盘价。这需要将最近的“日期”值与“ICO_to”日期匹配:

##Convert to datetime, format   
    D1.rename(columns={'timestamp': 'Date'}, inplace=True)
    D1.Date = pd.to_datetime(D1.Date)

    D1.rename(columns={'ICO to': 'ICO_to'}, inplace=True)
    D1.ICO_to = pd.to_datetime(D1.ICO_to)

## Create a variable identifying the first ICO_to value (even though all ICO_to values are the same)
    ICO_to = D1.loc[0, 'ICO_to']
    ICO_to = pd.to_datetime(ICO_to)

## Identify the first date where 'Date' column most closely matches 'ICO_to' column value, remove all Date values prior

    D1.sort_values(by='Date', inplace=True)
    D1.reset_index(drop=True)
    D1 = D1[D1.index >= D1[min(abs(D1.Date - D1.ICO_to)) == abs(D1.Date - D1.ICO_to)].index[0]]
    D1.reset_index(drop=True)

## Identify the date 30 days from ICO_to value
    ThirtyDaysfromICO = ICO_to + pd.Timedelta(30, unit='d')
    Nearest_30_day_row = D1[min(abs(D1.Date - ThirtyDaysfromICO)) == abs(D1.Date - ThirtyDaysfromICO)] ##Find the 30-days-out date row value
    Nearest_30_day_row_DF = pd.DataFrame(Nearest_30_day_row) ##Make sure it's still a dataframe
    Nearest30_day_row_index = Nearest_30_day_row_DF.index ##Find the index value for the 30-days-out row
    Nearest30_day_row_index = pd.to_numeric(Nearest30_day_row_index) ##attempt to convert index to numeric value
    #Nearest30_day_row_index = Nearest30_day_row_index.astype(str).astype(int) ## Second attempt to convert to numeric value, same error

## Find  returns from'close' value up to 30-days-out-period
    D1['First30DReturn'] = D1.loc[0:Nearest30_day_row_index, 'close'].pct_change()

但我得到了错误:

TypeError: 'Int64Index([16], dtype='int64')' is an invalid key 

...指的是最后一行 (D1['First30DReturn'])。我认为索引没有因为日期时间而增加价值?我试图通过将索引值正式转换为上面的整数来对此进行调整。这应该会返回 2017 年 6 月 28 日的收盘价 - 1.69。

为什么这个错误出现在最后一行?

4

0 回答 0