sparc_spread
sparc_spread

Reputation: 10843

Pandas Categorical equality test

I'm using pandas 0.24.2 and am unable to perform simple equality tests for Categorical instances. For example, with this code:

d = DataFrame({"x" : [1, 2, 3, 4]})
d["y"] = pd.cut(d["x"], bins=3)

The contents of d are now:

    x   y
0   1   (0.997, 2.0]
1   2   (0.997, 2.0]
2   3   (2.0, 3.0]
3   4   (3.0, 4.0]

However, I cannot test y for equality to string values, e.g.:

d["y"] == '(3.0, 4.0]'

Produces:

0    False
1    False
2    False
3    False
Name: y, dtype: bool

I get that the underlying type of y is Categorical, due to the output of cut(). However I cannot find any special methods for equality in the Categorical; moreover, the doc even states that "Equality comparisons work with . . . scalars." What am I missing in my approach?

Upvotes: 3

Views: 359

Answers (2)

Scott Boston
Scott Boston

Reputation: 153500

Alternatively, you could cast as 'string' datatype:

d["y"].astype(str) == "(3.0, 4.0]"

Output:

0    False
1    False
2    False
3     True
Name: y, dtype: bool

Upvotes: 2

BENY
BENY

Reputation: 323326

That is interval so pass to pd.Interval

d.y==pd.Interval(3,4)
Out[255]: 
0    False
1    False
2    False
3     True
Name: y, dtype: bool

Upvotes: 4

Related Questions