1

I have a rather strange issue with np.where that is making question whether I should use another function instead.

The logic is the following: If and only if "trendup" AND "trendown" are 'False', then "No_trend", otherwise "trending"

somehow the output is wrong but i am not sure what is off.

    Id  Trendup  Trendown
0  001    False     False
1  002    False     False

and the line of code is:

forecast['trend_status'] = np.where((forecast['Trendup'] == 'False') & (forecast['Trendown'] == 'False'), 'Not_trending', 'trending')

and the wrong output:

    Id  Trendup  Trendown trend_status
0  001    False     False     trending
1  002    False     False     trending

In this case it should be "Not_trending" in the trend_status column since the two previous columns are False. I am guessing that the issue is the & statement. I have looked at np.logical_and() but it does not seem that it is what I look for. Any help would be welcome

UPDATE: Thanks to @Yatu, I have tryed:

forecast['trend_status'] = np.where(~(forecast['Trendup'] & forecast['Trendown']), 'Not_trending', 'trending')

but when I play with my dataset to show some trend the issue is still there

0  001    False      True  Not_trending
1  002    False      True  Not_trending

What could be going wrong? I have been so close to this issue now that I may be failing to see the obvious.

Murcielago
  • 905
  • 1
  • 8
  • 30

1 Answers1

2

The problem seems to be that you are confusing booleans with strings. When working with booleans you should be using logical operators You should be doing something like:

np.where(~(forecast['Trendup'] | forecast['Trendown']), 'Not_trending', 'trending')
# array(['Not_trending', 'Not_trending'], dtype='<U12')

You can check that is the case with:

print(df.dtypes)

Id          int64
Trendup      bool
Trendown     bool
dtype: object
yatu
  • 86,083
  • 12
  • 84
  • 139