我的面板中充满了在交易日未对齐的股票数据。你可以使用代码:dataFrame.ix [dateSet]从pandas DataFrame中选择日期,但是我想做一些类似于面板的事情,并且已经使用循环遍历面板并重新加入它们。所以某种Panel.ix [daterange]会很有用。
def slicePanelOverTradingDates(panel = fpStockPanel, security = 'SPY'):
# get the trading dates of the S&P starting with the first date of the panel's adjusted close item
validSPXDates = DataReader('SPY','yahoo',panel['Adj Close'].index[0].date()).index
# take the dates that are valid in the stock panel that intersect with the S&P's dates and makes sure they're in order
panelDatesOnSPXDays = list(set(panel['Adj Close'].index).intersection(set(validSPXDates)))
panelDatesOnSPXDays.sort()
# remakes the panel sliced only over the correct dates
panelFrame = {}
for x in panel.items:
panelFrame[x] = panel[x].ix[panelDatesOnSPXDays]
finalPanel = pd.Panel(panelFrame)
return finalPanel
答案 0 :(得分:1)
.loc
支持多维选择,请参阅here;这同样适用于.ix
和.iloc
In [18]: p = tm.makePanel()
In [19]: p
Out[19]:
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 30 (major_axis) x 4 (minor_axis)
Items axis: ItemA to ItemC
Major_axis axis: 2000-01-03 00:00:00 to 2000-02-11 00:00:00
Minor_axis axis: A to D
In [20]: p.loc[:,'2000-1-4':'2000-1-31']
Out[20]:
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 20 (major_axis) x 4 (minor_axis)
Items axis: ItemA to ItemC
Major_axis axis: 2000-01-04 00:00:00 to 2000-01-31 00:00:00
Minor_axis axis: A to D