簡體   English   中英

Python Pandas在多個索引上按小時搜索

[英]Python Pandas search by hour on multiple index

我有一個多索引熊貓數組,我試圖在上午9:30〜10:00之間找到最小值和最大值。 我可以迭代數組並檢查時間是否匹配。 但這應該是通過熊貓的方式...

有沒有辦法對交易日的30分鍾進行分組/搜索? 我正在嘗試對其進行分組,但是由於其具有上市前的價值,因此僅使用[:30]就無法正常工作。

import pytz
from datetime import datetime, date, time
from datetime import timedelta
import matplotlib.pyplot as pyplot
from collections import defaultdict
import pandas as pd
from pandas.tseries.resample import TimeGrouper
from pandas.tseries.offsets import DateOffset
from pandas.tseries.index import DatetimeIndex
from pandas import Timestamp

def my_grouper(ts):
    "Function to apply to the index of the DataFrame to break it into groups."
    #pdb.set_trace()
    # Returns midnight of the supplied date.
    return pd.core.datetools.normalize_date(ts)


def first_thirty_minutes(frame):
    "Function to apply to the resulting groups."
    start = frame.index.searchsorted("09:30:00")
    end = frame.index.searchsorted("10:00:00")
    return frame.iloc[start:end]


hist = pd.read_csv("ES2.txt" ,index_col=0 )
eastern = pytz.timezone('US/Eastern')
hist.index = pd.DatetimeIndex(hist.index).tz_localize('UTC').tz_convert('US/Eastern')
data = hist.groupby(my_grouper).apply(first_thirty_minutes)

數據:

DateTime,Hour,Open,High,Low,Close,Volume
1997-09-11 00:00:00-04:00,1997-09-11 00:33:00-04:00,1176.25,1176.25,1174.5,1174.5,4
1997-09-11 00:00:00-04:00,1997-09-11 00:34:00-04:00,1173.75,1173.75,1173.75,1173.75,1
1997-09-11 00:00:00-04:00,1997-09-11 01:45:00-04:00,1173.25,1173.25,1173.25,1173.25,1
1997-09-11 00:00:00-04:00,1997-09-11 04:08:00-04:00,1172.75,1172.75,1172.75,1172.75,1
1997-09-11 00:00:00-04:00,1997-09-11 04:09:00-04:00,1172.5,1172.5,1172.5,1172.5,2
1997-09-11 00:00:00-04:00,1997-09-11 04:10:00-04:00,1172.5,1172.5,1172.5,1172.5,1
1997-09-11 00:00:00-04:00,1997-09-11 04:11:00-04:00,1172.0,1172.0,1172.0,1172.0,1
1997-09-11 00:00:00-04:00,1997-09-11 04:20:00-04:00,1172.0,1172.0,1172.0,1172.0,1
1997-09-11 00:00:00-04:00,1997-09-11 04:21:00-04:00,1171.75,1172.25,1171.75,1172.25,4
1997-09-11 00:00:00-04:00,1997-09-11 04:22:00-04:00,1172.0,1172.0,1171.5,1171.5,2
1997-09-11 00:00:00-04:00,1997-09-11 04:25:00-04:00,1171.0,1171.0,1171.0,1171.0,1
1997-09-11 00:00:00-04:00,1997-09-11 04:31:00-04:00,1170.5,1170.5,1170.5,1170.5,1

經過更多的挖掘后,我發現這條線通過使用between_time修復了算法

返回frame.between_time(start_time ='9:30',end_time = '10:00')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM