简体   繁体   English

熊猫:如何从具有日期时间索引的DataFrame中提取一组日期?

[英]pandas: how to extract a set of dates from a DataFrame with datetime index?

I have two DataFrames with TimesSeriesIndex, df1, df2. 我有两个带有TimesSeriesIndex,df1,df2的数据框。

df2's index is a subset of df1.index. df2的索引是df1.index的子集。
How can I extract the index dates from df1 which also contained by df2, so I can run analysis on these dates. 我如何从df2中也包含的df1中提取索引日期,以便可以对这些日期进行分析。

Take the intersection of their indices. 取其索引的交集。

In [1]: import pandas as pd

In [2]: index1 = pd.DatetimeIndex(start='2000-1-1', freq='1T', periods=1000000)

In [3]: index2 = pd.DatetimeIndex(start='2000-1-1', freq='1D', periods=1000)

In [4]: index1
Out[4]: 

[2000-01-01 00:00:00, ..., 2001-11-25 10:39:00]
Length: 1000000, Freq: T, Timezone: None

In [5]: index2
Out[5]: 

[2000-01-01 00:00:00, ..., 2002-09-26 00:00:00]
Length: 1000, Freq: D, Timezone: None

In [6]: index1 & index2
Out[6]: 

[2000-01-01 00:00:00, ..., 2001-11-25 00:00:00]
Length: 695, Freq: D, Timezone: None

In your case, do the following: 对于您的情况,请执行以下操作:

index1 = df1.index
index2 = df2.index

Then take the intersection of these as defined before. 然后按照前面的定义进行交叉。 Later you may wish to do something like the following to get the df at intersection index. 稍后,您可能希望执行以下操作来获得交点索引处的df

df1_intersection =df1.ix[index1 & index2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM