Pandas datetime匹配最近的日期 - 索引错误' dtype =' int64')'是一个无效的密钥'

时间:2018-03-24 14:35:57

标签: python pandas datetime indexing loc

我有一个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'])。我认为由于日期时间指数没有提升价值?我试图通过正式将索引值转换为上面的整数来调整此值。这应该会在6/28/2017 - 1.69收盘时结束。

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

0 个答案:

没有答案