以下簡短腳本使用findatapy從Dukascopy網站收集數據。請注意,此軟件包使用Pandas,並且不需要單獨導入它。Python:使用Pandas從數據框中選擇特定日期
from findatapy.market import Market, MarketDataRequest, MarketDataGenerator
market = Market(market_data_generator=MarketDataGenerator())
md_request = MarketDataRequest(start_date='08 Feb 2017', finish_date='09 Feb 2017', category='fx', fields=['bid', 'ask'], freq='tick', data_source='dukascopy', tickers=['EURUSD'])
df = market.fetch_market(md_request)
#Group everything by an hourly frequency.
df=df.groupby(pd.TimeGrouper('1H')).head(1)
#Deleting the milliseconds from the Dateframe
df.index =df.index.map(lambda t: t.strftime('%Y-%m-%d %H:%M:%S'))
#Computing Average between columns 1 and 2, and storing it in a new one.
df['Avg'] = (df['EURUSD.bid'] + df['EURUSD.ask'])/2
的結果是這樣的:
直到此時,一切運行正常,但我需要從這個數據幀提取特定小時。我想在某個時間點上午10:00:00選擇所有的值(出價,詢問,平均值或其中一個)。
通過觀察其他posts,我想我可以做這樣的事情:
match_timestamp = "10:00:00"
df.loc[(df.index.strftime("%H:%M:%S") == match_timestamp)]
但結果是一個錯誤消息說:
AttributeError: 'Index' object has no attribute 'strftime'
我甚至不能執行df.index .hour,它曾經在刪除毫秒的行之前工作(dtype是datetime64 [ns]直到該點),然後dtype是'Object'。看起來我需要反轉這種格式才能使用strftime。
你能幫我嗎?
我喜歡這個,因爲有一行我可以擺脫毫秒,並在整個數據幀操作中保持相同的dtype。我也可以使用df.loc [(df.index.strftime(「%H:%M:%S」)==「10:00:00」)],這對我正在嘗試做的更好。謝謝! :) –
@AquilesPáez沒有問題,你也會得到大約10%的速度增加使用resample vs groupby大集 –