Pandas:如何从带有日期时间索引的数据帧中提取一组日期?

2024-10-01 19:34:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个带timeseriesIndex的数据帧,df1,df2。在

df2的索引是df1.index的子集。
如何从df1中提取同样包含在df2中的索引日期,以便对这些日期运行分析。在


Tags: 数据index子集df1df2timeseriesindex
1条回答
网友
1楼 · 发布于 2024-10-01 19:34:22

取他们指数的交点。在

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

对于您的情况,请执行以下操作:

^{pr2}$

然后取前面定义的交点。 稍后,您可能希望执行以下操作以获得df在交叉点处的索引。在

df1_intersection =df1.ix[index1 & index2]

相关问题 更多 >

    热门问题