1

Given a df:

Date1                   Text1    
2018-03-20 00:00:00     abc       
2018-04-01 00:00:00     abc
2018-01-01 00:00:00     abc
2018-04-01 00:00:00     xyz
                        abc

My goal to add a new column where: if text = "abc" and Date1 is with 90 days from now then "New" The output would be:

Date1                   Text1    NewText
2018-03-20 00:00:00     abc      New
2018-04-01 00:00:00     abc      New
2018-01-01 00:00:00     abc
2018-04-01 00:00:00     xyz
                        abc

This is what I have:

days90 = date.today() - timedelta(90)

df['NewText'] = np.where(df['Text1'] = "abc" & df['Date1'] < pd.to_datetime(days90), "New", np.nan)

However, I'm keep running into errors.
AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

Any suggestion? Many thanks!

jpp
  • 159,742
  • 34
  • 281
  • 339
TylerNG
  • 919
  • 2
  • 9
  • 23

1 Answers1

1

There are 3 errors in your code:

  1. Test equality of variables via == operator.
  2. Make sure you bracket individual conditions to avoid chained comparisons.
  3. For a date to be within 90 days, check your test data is greater than days90.

Combined, the following code will work:

df['NewText'] = np.where((df['Text1'] == "abc") & (df['Date1'] > days90), "New", np.nan)
jpp
  • 159,742
  • 34
  • 281
  • 339