equality

Pandas DataFrames with NaNs equality comparison

Pandas DataFrames with NaNs equality comparison Question: In the context of unit testing some functions, I’m trying to establish the equality of 2 DataFrames using python pandas: ipdb> expect 1 2 2012-01-01 00:00:00+00:00 NaN 3 2013-05-14 12:00:00+00:00 3 NaN ipdb> df identifier 1 2 timestamp 2012-01-01 00:00:00+00:00 NaN 3 2013-05-14 12:00:00+00:00 3 NaN ipdb> df[1][0] …

Total answers: 5

Why don't methods have reference equality?

Why don't methods have reference equality? Question: I had a bug where I was relying on methods being equal to each other when using is. It turns out that’s not the case: >>> class What: … def meth(self): … pass >>> What.meth is What.meth # This is False in Python 2 True >>> inst = …

Total answers: 2

double equals vs is in python

double equals vs is in python Question: I run the following in the Python interpreter: >>> foo = 10 >>> dir(foo) == dir(10) True >>> dir(foo) is dir(10) False >>> Why is this? Asked By: ben || Source Answers: is checks that 2 arguments refer to the same object, == checks that 2 arguments have …

Total answers: 1

Set "in" operator: uses equality or identity?

Set "in" operator: uses equality or identity? Question: class A(object): def __cmp__(self): print ‘__cmp__’ return object.__cmp__(self) def __eq__(self, rhs): print ‘__eq__’ return True a1 = A() a2 = A() print a1 in set([a1]) print a1 in set([a2]) Why does first line prints True, but second prints False? And neither enters operator eq? I am using …

Total answers: 5

Why can a Python dict have multiple keys with the same hash?

Why can a Python dict have multiple keys with the same hash? Question: I am trying to understand the Python hash function under the hood. I created a custom class where all instances return the same hash value. class C: def __hash__(self): return 42 I just assumed that only one instance of the above class …

Total answers: 5

Determine if 2 lists have the same elements, regardless of order?

Determine if 2 lists have the same elements, regardless of order? Question: Sorry for the simple question, but I’m having a hard time finding the answer. When I compare 2 lists, I want to know if they are “equal” in that they have the same contents, but in different order. Ex: x = [‘a’, ‘b’] …

Total answers: 4

Testing for reference equality in Python

Testing for reference equality in Python Question: Say I have a class in Python that has an eq method defined for comparing attributes for equality: class Foo(object): # init code… def __eq__(self, other): # usual eq code here…. How can I then compare two instances of Foo for reference equality (that is test if they …

Total answers: 3

When to use == and when to use is?

When to use == and when to use is? Question: Curiously: >>> a = 123 >>> b = 123 >>> a is b True >>> a = 123. >>> b = 123. >>> a is b False Seems a is b being more or less defined as id(a) == id(b). It is easy to make …

Total answers: 5

How do I check if two variables reference the same object in Python?

How do I check if two variables reference the same object in Python? Question: x and y are two variables. I can check if they’re equal using x == y, but how can I check if they have the same identity? Example: x = [1, 2, 3] y = [1, 2, 3] Now x == …

Total answers: 2

String comparison in Python: is vs. ==

String comparison in Python: is vs. == Question: I noticed a Python script I was writing was acting squirrelly, and traced it to an infinite loop, where the loop condition was while line is not ”. Running through it in the debugger, it turned out that line was in fact ”. When I changed it …

Total answers: 4