I have a datatime dataframe. I want to compare it with a reference date and assign before it is less than and after if greater.
My code:
df = pd.DataFrame({'A':np.arange(1.0,9.0)},index=pd.date_range(start='2020-05-04 08:00:00', freq='1d', periods=8))
df=
A
2020-05-04 08:00:00 1.0
2020-05-05 08:00:00 2.0
2020-05-06 08:00:00 3.0
2020-05-07 08:00:00 4.0
2020-05-08 08:00:00 5.0
2020-05-09 08:00:00 6.0
2020-05-10 08:00:00 7.0
2020-05-11 08:00:00 8.0
ref_date = '2020-05-08'
Expected answer
df=
A Condi.
2020-05-04 08:00:00 1.0 Before
2020-05-05 08:00:00 2.0 Before
2020-05-06 08:00:00 3.0 Before
2020-05-07 08:00:00 4.0 Before
2020-05-08 08:00:00 5.0 After
2020-05-09 08:00:00 6.0 After
2020-05-10 08:00:00 7.0 After
2020-05-11 08:00:00 8.0 After
My solution:
df['Cond.'] = = ['After' if df.index>=(ref_date)=='True' else 'Before cleaning']
Present answer
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
df.index>=(ref_date)=='Trueno. First, boolean objects would beTruenot'True', but you never need to use== True. Perhaps more importantly, you are creating a list with a single element, although, that is failing because you are doing the vectorized operation ondf.indexwhich creates a boolean array, hence the error message