0

I am trying to create a new dataframe from an existing one by conditioning holiday datetime. train dataframe is existing and I want to create train_holiday from it by taking day and month values of holiday dataframe, my purpose is similar below:

    date    values
2015-02-01  10
2015-02-02  20
2015-02-03  30
2015-02-04  40
2015-02-05  50
2015-02-06  60

    date
2012-02-02
2012-02-05

now first one is existing, and second dataframe shows holidays. I want to create a new dataframe from first one that only contains 2015 holidays similar below:

 date      values
2015-02-02  20
2015-02-05  50

I tried

train_holiday = train.loc[train["date"].dt.day== holidays["date"].dt.day]

but it gives error. could you please help me about this?

0

1 Answer 1

2

In your problem you care only the month and the day components, and one way to extract that is by dt.strftime() (ref). Applying that extraction on both date columns and use .isin() to keep month-day in df1 that matches that in df2.

df1[
    df1['date'].dt.strftime('%m%d').isin(
    df2['date'].dt.strftime('%m%d')
    )
   ]

Make sure both date columns are in date-time format so that .dt can work. For example,

df1['date'] = pd.to_datetime(df1['date'])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.