Pandas comparing a date to datetime index using if statement

Viewed 1392

I'm using a for loop to compare a variable/ date to each datetime period. The code below produces 'False' for every period, which is incorrect. The first five dates on my list are all in 2006, the last five are all in 2011. The dtype for data['date'] is datetime64 and I convert the variable to datetime in my code.

Thanks in advance for any help or comments.

import numpy as np
import pandas as pd
from datetime import datetime
from dateutil.parser import parse

data = pd.read_csv("test_data_3.csv").fillna(0)
data['date'] = pd.to_datetime(data['date'])
data.set_index(data['date'], inplace=True)

lockout_pd_2_begin = '2009-06-01'
lockout_pd_2_begin = datetime.strptime(lockout_pd_2_begin,'%Y-%m-%d')

for row in data:
    if (lockout_pd_2_begin >= data['date']).all():
        data['lockout_test_result'] = 'True'
    else:
        data['lockout_test_result'] = 'False'
1 Answers

If I get the question right, you simply want a boolean mask. In pandas you can derive that using conditionals like

# trying to reproduce the data:
data = pd.DataFrame({'date': pd.date_range("2006-01-01", "2011-12-31", freq='M')})

# get a boolean Series reflecting the condition:
data['lockout_test_result'] = data['date'] >= pd.Timestamp('2009-06-01')

# data
#          date  lockout_test_result
# 0  2006-01-31                False
# 1  2006-02-28                False
# 2  2006-03-31                False
# 3  2006-04-30                False
# 4  2006-05-31                False
# ..        ...                  ...
# 67 2011-08-31                 True
# 68 2011-09-30                 True
# 69 2011-10-31                 True
# 70 2011-11-30                 True
# 71 2011-12-31                 True

# [72 rows x 2 columns]
Related