Mainland
Mainland

Reputation: 4554

Python dataframe datetime based if and else condition

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()

Upvotes: 1

Views: 951

Answers (2)

Celius Stingher
Celius Stingher

Reputation: 18367

Helping you fix the list comprehension approach you took, you can use:

df['Cond'] = ['After' if x >= pd.to_datetime(ref_date) else 'Before' for x in df.index]

Upvotes: 1

Andrej Kesely
Andrej Kesely

Reputation: 195408

ref_date = "2020-05-08"

df["Cond"] = np.where(df.index < ref_date, "Before", "After")
print(df)

Prints:

                       A    Cond
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

Upvotes: 5

Related Questions