Skip to content

[BUG] Triangle-to-Triangle comparison fails #1394

Closed

Description

@genedan

Are you on the latest chainladder version?

  • Yes, this bug occurs on the latest version.

Describe the bug in words

A comparison such as raa < raa fails.

How can the bug be reproduced?

import chainladder as cl
raa = cl.load_sample('raa')

raa < raa
Traceback (most recent call last):
 ... 
line 524, in __lt__
    obj.values = xp.nan_to_num(obj.values) < xp.nan_to_num(value)
                                             ~~~~~~~~~~~~~^^^^^^^
TypeError: no implementation found for 'numpy.nan_to_num' on types that implement __array_function__: [<class 'chainladder.core.triangle.Triangle'>]

What is the expected behavior?

This should behave like the numpy and Pandas analogues:

import numpy as np

a = np.array([[1.0, 2.0], [3.0, np.nan]])
b = np.array([[1.0, 1.0], [9.0, 9.0]])

print(a)
array([[ 1.,  2.],
       [ 3., nan]])

print(b)

array([[1., 1.],
       [9., 9.]])

a < b
# array([[False, False],
#        [ True, False]])      NaN compares False

a <= b
# array([[ True, False],
#        [ True, False]])      differs from < at the equal cell

a < np.array([[1.0, 2.0, 3.0]])
# ValueError: operands could not be broadcast together with shapes (2,2) (1,3)


import numpy as np
import pandas as pd

d1 = pd.DataFrame({"x": [1.0, 3.0], "y": [2.0, np.nan]}, index=["a", "b"])
d2 = pd.DataFrame({"x": [1.0, 9.0], "y": [1.0, 9.0]}, index=["a", "b"])

print(d1)
     x    y
a  1.0  2.0
b  3.0  NaN

print(d2)
     x    y
a  1.0  1.0
b  9.0  9.0

d1 < d2
#        x      y
# a  False  False
# b   True  False       NaN compares False

d1 <= d2
#       x      y
# a  True  False        differs from < at the equal cell
# b  True  False

d3 = pd.DataFrame({"x": [1.0, 9.0]}, index=["a", "b"])

d1 < d3
#       x   y
# a   2.0 NaN
# b  12.0 NaN

Would you be willing to contribute this ticket?

  • Yes, absolutely!
  • Yes, but I would like some help.
  • No.

Activity

  1. added this to the v0.11.0 milestone on Sep 18, 2026
  2. updated Priority High, Effort Low and Scope Codebase on Sep 18, 2026
  3. added theissue type on Sep 18, 2026
  4. added
    Great First Contribution! 🌱Beginner friendly tickets with narrow scope and huge impact. Perfect to get your feet wet!
    on Sep 18, 2026
  5. changed the title [-][BUG] Triangle-to-Triangle comparison failes[/-] [+][BUG] Triangle-to-Triangle comparison fails[/+] on Sep 18, 2026
  6. henrydingliu commented on Sep 20, 2026

    @henrydingliu
    Member

    @genedan I believe you are giving conflicting expected behavior between this issue and #1392. In this issue, NaN should always compare to false. In #1392, NaN is treated as 0, and can compare to True. Please let me know if I'm understanding these issues as you intend

    @Abhayindia FYI

  7. Abhayindia commented on Sep 21, 2026

    @Abhayindia
    Contributor

    @henrydingliu you're reading them the same way I am, and #1412 currently sits on the #1392 side: it keeps the existing nan_to_num on both operands, so on raa, where [1982, 120] is NaN, NaN < 10000 and NaN <= 10000 both come back True (NaN read as 0) while > and >= are False. pandas gives False for all four.
    whichever way @genedan calls it, it is a small change in the shared _compare helper that PR adds: drop the nan_to_num and let NaN propagate so every comparison is False, or keep it as is. happy to switch it over, I just did not want to change NaN semantics quietly while fixing the operators.

  8. genedan commented on Sep 22, 2026

    @genedan
    MemberAuthor

    Oh yeah...those NaNs always trip me up, I was so hyperfocused on the 5012.0 cell that I wasn't paying attention to the NaNs.

    5012.0 <= 5012.0 should resolve to True, so that part of #1392 is definitely a bug.

    The NaN comparison in the lower diagonal should also evaluate to False.

    I did find the standard that Python follows, it's IEEE 754, this specifies that NaN comparisons should evaluate to False.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Great First Contribution! 🌱Beginner friendly tickets with narrow scope and huge impact. Perfect to get your feet wet!

    Type

    Fields

    Priority

    High

    Effort

    Low

    Scope

    Codebase

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions