Numpy 熊猫:真想不到为数据帧编制索引

Numpy 熊猫:真想不到为数据帧编制索引,numpy,pandas,Numpy,Pandas,我有一个熊猫数据帧,df1,这是一个长达一年的5分钟时间序列,带有a-Z列 df1.shape (105121, 26) df1.index <class 'pandas.tseries.index.DatetimeIndex'> [2002-01-02 00:00:00, ..., 2003-01-02 00:00:00] Length: 105121, Freq: 5T, Timezone: None 这一次,我得到一个错误: /home/wchapman/.local/lib

我有一个熊猫数据帧,df1,这是一个长达一年的5分钟时间序列,带有a-Z列

df1.shape
(105121, 26)
df1.index
<class 'pandas.tseries.index.DatetimeIndex'>
[2002-01-02 00:00:00, ..., 2003-01-02 00:00:00]
Length: 105121, Freq: 5T, Timezone: None
这一次,我得到一个错误:

/home/wchapman/.local/lib/python2.7/site-packages/pandas-0.11.0-py2.7-linux-x86_64.egg/pandas/core/index.pyc in get_indexer(self, target, method, limit)
    844             this = self.astype(object)
    845             target = target.astype(object)
--> 846             return this.get_indexer(target, method=method, limit=limit)
    847 
    848         if not self.is_unique:

AttributeError: 'numpy.ndarray' object has no attribute 'get_indexer'

我的临时解决方案是按日期循环,但这似乎效率低下。熊猫能做这种奇特的索引吗?我在文档中没有看到任何示例。

这里有一种方法:

t_index = df1.index
d_index = df2.index
mask = t_index.map(lambda t: t.date() in d_index)
df1[mask]
稍微快一点(但想法相同)的方法是使用:

mask = pd.to_datetime([datetime.date(*t_tuple)
                           for t_tuple in zip(t_index.year,
                                              t_index.month,
                                              t_index.day)]).isin(d_index)

你可以将df2重新采样5分钟,然后填充。谢谢——应该提到我也试过了。Get-ValueError:无法使用多维键进行索引。
。date
可能是DatetimeIndex的一个有用方法(我想我会把它放在一起)。谢谢Andy,但我的问题有点复杂。我需要使用df2的(布尔)值,而不是索引,作为df1的奇特索引,如下所示(numpy):a=np.arange(5)b=np.asarray([True.False,True,False,True])a[b]-->arrray([0,2,4])。碰巧的是,上面的代码收回了整个df1,因为df1的日期时间都在df2的日期内。我需要的是返回df1的正确列——即那些由相应df2列中的真值标识的列。这一选择将因天而异。
t_index = df1.index
d_index = df2.index
mask = t_index.map(lambda t: t.date() in d_index)
df1[mask]
mask = pd.to_datetime([datetime.date(*t_tuple)
                           for t_tuple in zip(t_index.year,
                                              t_index.month,
                                              t_index.day)]).isin(d_index)