我有一个数据框,D1:
Date Symbol ICO_to
5/28/2017 18:00 MYST 5/30/2017
5/29/2017 18:00 MYST 5/30/2017
5/30/2017 18:00 MYST 5/30/2017
6/1/2017 18:00 MYST 5/30/2017
6/2/2017 18:00 MYST 5/30/2017
6/3/2017 18:00 MYST 5/30/2017
6/4/2017 18:00 MYST 5/30/2017
6/5/2017 18:00 MYST 5/30/2017
6/6/2017 18:00 MYST 5/30/2017
根据此链接,我正在尝试两种方法来识别最接近“ICO_to”日期值(所有行具有相同值)的“日期”值(最接近的匹配)。首先,我尝试截断,这应该删除该日期值之前的行:
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)
ICO_to = D1['ICO_to'][0] #All values in this column are the same, I just want to reference that value
ICO_to = pd.to_datetime(ICO_to) # to make sure the value is a datetime
First_date_row = D1['Date'].truncate(before=ICO_to).iloc[-1] #Remove all rows not after/= to the ICO_to date value
但是我收到此错误:
TypeError: Cannot compare type 'Timestamp' with type 'long'
好吧,我知道这些是日期时间值,所以不确定交易是什么。ICO_to 变量是一个时间戳。我试试这个:
First_date_row = D1['Date'].loc[D1.index.get_loc(datetime.datetime(D1['ICO_to'][0]),method='nearest')] #Identify the row where 'Date' nearest matches 'ICO_to' value at row 0
使用这个而不是截断,我得到这个错误:
TypeError: an integer is required
如何识别最接近 ICO_to 值的 Date 值,或者通过截断删除最接近匹配之前的所有行?任何一种方法都可以。