Skip to content

[ENH] Make empty Triangle behavior consistent with Pandas #575

@genedan

Description

@genedan

So when defining an empty triangle:

foo = cl.Triangle()

The way it works is that if there is no value provided to the data argument, __init__() terminates near the beginning and skips over the rest of the initialization code. This leaves the attributes such as Triangle.values undefined.

Accessing such attributes leads to a traceback:

foo.values
Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/envs/cl_dev/lib/python3.12/site-packages/IPython/core/interactiveshell.py", line 3549, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-48-beca352ff75c>", line 1, in <module>
    foo.values
AttributeError: 'Triangle' object has no attribute 'values'

On the other hand, initializing a pandas DataFrame has some placeholder values for the analogous attributes:

bar = pd.DataFrame()
bar.values
Out[40]: array([], shape=(0, 0), dtype=float64)

If users are like me (who haven't thoroughly gone through all the documentation), they sometimes try pandas methods on a triangle just to see if they'd work like how they would on a DataFrame, and in lots of cases it works.

I think, making such behavior consistent with pandas as much as we can would be a good enhancement, such as creating a .empty attribute for an empty Triangle, which can be used in conditional statements:

if foo.empty:
    print("I'm empty!")
    
Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/envs/cl_dev/lib/python3.12/site-packages/IPython/core/interactiveshell.py", line 3549, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-49-da5cfd440d07>", line 1, in <module>
    if foo.empty:
       ^^^^^^^^^
AttributeError: 'Triangle' object has no attribute 'empty'

vs.

if bar.empty:
    print("I'm empty!")
I'm empty!

I realize this is a big request, since there are a lot more other features like appending records and assigning columns that you can perform on an empty DataFrame. But for starters I'd say we just handle the initialization attributes for this ticket and then create other tickets as we encounter those other features in the field.

I know even doing this will break some code such as Triangle.__repr__() which defines an empty triangle as one that does not have a .value attribute defined. Defining it as None or an empty ndarray will break this method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Effort > Brief 🐇Small tasks expected to take a few hours up to a couple of days.Great First Contribution! 🌱Beginner friendly tickets with narrow scope and huge impact. Perfect to join our community!Impact > Moderate 🔶User-visible but non-breaking change. Treated like a minor version bump (e.g., 0.6.5 → 0.7.0).

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions