0

I have the following code, where I want to determine if a datetime object exists in a data frame.

Here is the code:

df_grid['Date'] = pd.to_datetime(df_grid['Date'])
start_date = df_grid['Date'].iloc[0].date() 
end_date = df_grid['Date'].iloc[-1].date()
current_date = start_date

while current_date < end_date:
    current_date += datetime.timedelta(days=1)
    print(current_date)
    if current_date in df_grid['Date']: continue
    print('not in')

And here is what the dataframe column looks like:

Date
2021-11-01
2021-11-01
2021-11-01
2021-11-02
2021-11-02
2021-11-03
2021-11-03
...

Most of the dates do exist in the column; however, when I run the code, it indicates that none of the dates exist in the dataframe column. I've tried matching with and without .date() and get the same results.

4
  • 1
    Did you check data type of the columns after loading the data? You need to pass the date fields to parse_date parameter. Commented Mar 31, 2022 at 15:56
  • I did convert the column to datetime when I imported the csv file. Don't know why they would be different data types, but it's worth a check. Added that bit of code to the question. Commented Mar 31, 2022 at 16:14
  • 1
    Let's take a step back and tell us what you want to do. Are you looking for sequential dates that do not exist between the start_date and end_date? Commented Apr 1, 2022 at 0:23
  • That is exactly what I'm trying to do. And the dates that do exist aren't unique. So say 11/1/2021 is in the df. It will occur multiple times. Commented Apr 1, 2022 at 13:56

1 Answer 1

1

Well, this is what I ended up doing to identify missing dates in the dateframe

    while current_date < end_date:
        missing = True
        for row in df_grid.index:
            if current_date == df_grid['Date'].iloc[row]:
                missing = False
                break
        if missing == True:
            [code to execute when date is missing]
        current_date += datetime.timedelta(days=1)

Seems a lot more inefficient than what I originally tried to do, but at least this works.

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.