简体   繁体   中英

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

I have two DataFrames with TimesSeriesIndex, df1, df2.

df2's index is a subset of df1.index.
How can I extract the index dates from df1 which also contained by df2, so I can run analysis on these dates.

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.

df1_intersection =df1.ix[index1 & index2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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