user308827
user308827

Reputation: 21981

rearrange dataframe based on day and month from another dataframe index

I have 2 dataframes:

df_a

datetime      var
2016-10-15    110.232790
2016-10-16    111.020661
2016-10-17    112.193496
2016-10-18    113.638143
2016-10-19    115.241448

and df_b

datetime      var
2000-01-01    165.792185
2000-01-02    166.066959
2000-01-03    166.411669
2000-01-04    167.816046
2000-01-05    169.777814
2000-10-15    114.232790
2000-10-16    113.020661

df_a has information for the year 2016 and df_b has information for years from 2000 to 2015 (there is no overlap in the years).

Can I arrange the df_b dataframe to have the same order in terms of day of year as df_a?

Upvotes: 1

Views: 18

Answers (1)

jezrael
jezrael

Reputation: 862761

You can use boolean indexing with isin and month & day or numpy.in1d with strftime:

Notice: datetimes are indices in both DataFrames

df = df_b[df_b.index.month.isin(df_a.index.month) & df_b.index.day.isin(df_a.index.day)]
print (df)
                   var
datetime              
2000-10-15  114.232790
2000-10-16  113.020661

Or:

df = df_b[np.in1d(df_b.index.strftime('%m%d'), df_a.index.strftime('%m%d'))]
print (df)
                   var
datetime              
2000-10-15  114.232790
2000-10-16  113.020661

Upvotes: 1

Related Questions