As you find out your comparison returned an array with 2 booleans. You could use all(...) to check all the values in that array are True. But what if your compared arrays are not in the same order? Or you have more unique values than planned?
>>> all(pd.array(['Yes', 'No']) == pd.array(['Yes', 'No']))
True
>>> all(pd.array(['Yes', 'No']) == pd.array(['No', 'Yes']))
False
>>> all(pd.array(['Yes', 'No']) == pd.array(['Yes', 'No', 'Foo']))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python3.8/site-packages/pandas/core/ops/common.py", line 65, in new_method
return method(self, other)
File "/usr/lib64/python3.8/site-packages/pandas/core/arraylike.py", line 29, in __eq__
return self._cmp_method(other, operator.eq)
File "/usr/lib64/python3.8/site-packages/pandas/core/arrays/string_.py", line 351, in _cmp_method
mask = isna(self) | isna(other)
ValueError: operands could not be broadcast together with shapes (2,) (3,)
There’s a “pure pandas” way that could use pd.array.equals() but that still doesn’t solve sorting:
>>> pd.array(['Yes', 'No']).equals(pd.array(['Yes', 'No']))
True
>>> pd.array(['Yes', 'No']).equals(pd.array(['No', 'Yes']))
False
>>> pd.array(['Yes', 'No']).equals(pd.array(['Yes', 'No', 'Foo']))
False
Since unique doesn’t sort for you, just make sure to use a sorted array as your basis for comparison.
>>> pd.array(['No', 'Yes']).equals(pd.array(sorted(df['PhoneService'].unique())))
True
That’s rather tedious, so we can use a number of numpy utility functions, depending on what you find most explicit:
>>> np.array_equal(['No', 'Yes'], sorted(df['PhoneService'].unique()))
True
>>> np.setdiff1d(['No', 'Yes'], df['PhoneService'].unique()).size == 0
True
Alternately you can do the comparison on all the elements and aggregate the resulting boolean series:
>>> df['PhoneService'].isin(['No', 'Yes']).all()
True