2

I want to do something like:

if table['STATUS']=='A'or 'P':
    table['END_DATE']=end_date 

In order to replace the value of END_DATE for those rows that have a S or P in STATUS Which yields the

ValueError: The truth value of an array is ambiguous. Use a.empty, a.any() or a.all().

I get that it is due to the array adressing... This seems to be a petty common issue but I can't seem to find a suitable answer what is the Right constuct to use here? I can't figure it out from the docs...which say to use .any()

0

1 Answer 1

7

The if statement is ambiguous because it evaluates to a Series that maybe be empty, maybe have have some True values or False values. Hence its impossible to evaluate. See here: http://pandas.pydata.org/pandas-docs/dev/gotchas.html#using-if-truth-statements-with-pandas, http://pandas.pydata.org/pandas-docs/dev/indexing.html#boolean-indexing, http://pandas.pydata.org/pandas-docs/dev/basics.html#flexible-comparisons

This works in 0.12

In [2]: df = DataFrame(np.arange(20).reshape(10,2),columns=['A','B'])

In [3]: df['status'] = ['A']*4 + ['invalid'] * 2 + ['P'] * 4

This step won't be needed in 0.13

In [4]: df['end_date'] = np.nan

Edit from @DSM

In [5]: df.loc[(df['status'].isin(['A','P'])),'end_date'] = Timestamp('20130101')

In [6]: df
Out[6]: 
    A   B   status             end_date
0   0   1        A  2013-01-01 00:00:00
1   2   3        A  2013-01-01 00:00:00
2   4   5        A  2013-01-01 00:00:00
3   6   7        A  2013-01-01 00:00:00
4   8   9  invalid                  NaN
5  10  11  invalid                  NaN
6  12  13        P  2013-01-01 00:00:00
7  14  15        P  2013-01-01 00:00:00
8  16  17        P  2013-01-01 00:00:00
9  18  19        P  2013-01-01 00:00:00
Sign up to request clarification or add additional context in comments.

3 Comments

I might use df.loc[df['status'].isin(["A", "P"]), 'end_date'] instead.
I used : table.loc[table['STATUS'].isin(["A", "P"]), 'END_DATE']= end_date works like a charm... y'all the best!
@AndyHayden and if it had been only 1 element it would have worked! (incorrectly of course)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.