user2846226
user2846226

Reputation: 447

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.

Upvotes: 1

Views: 1853

Answers (1)

Nipun Batra
Nipun Batra

Reputation: 11367

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]

Upvotes: 1

Related Questions