Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scalar comparison semantics #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions cyberpandas/ip_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,19 @@ def to_bytes(self):
# ------------------------------------------------------------------------

def __eq__(self, other):
# TDOO: scalar ipaddress
if not isinstance(other, IPArray):
return NotImplemented
mask = self.isna() | other.isna()
result = self.data == other.data
if isinstance(other, (ipaddress.IPv4Address, ipaddress.IPv6Address)):
other = int(other)
hi, lo = unpack(pack(other))
other = np.array([(hi, lo)], dtype=self.dtype._record_type)
mask = self.isna()
elif isinstance(other, IPArray):
mask = self.isna() | other.isna()
other = other.data
else:
msg = ("Invalid type comparison. Can't compare IPArray to "
"type '{}'.")
raise TypeError(msg.format(other))
result = self.data == other
result[mask] = False
return result

Expand Down
21 changes: 21 additions & 0 deletions tests/ip/test_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ def test_equality():
v1.equals("a")


@pytest.mark.parametrize('other', [
1, '192.168.1.1', b'1'
])
def test_ops_other(other):
arr = ip.IPArray([1, 2, 3])

with pytest.raises(TypeError):
arr == other


@pytest.mark.parametrize('other', [
ipaddress.IPv4Address(1),
ipaddress.IPv6Address(1),
])
def test_equality_ipaddress(other):
arr = ip.IPArray([0, 1, 2**64 + 1])
result = arr == other
expected = np.array([False, True, False])
tm.assert_numpy_array_equal(result, expected)


@pytest.mark.parametrize('op', [
operator.lt,
operator.le,
Expand Down