Skip to content

Commit ae371f2

Browse files
Added warning when adding a column to a table with different length (#58)
* test for dealing with the warning being thrown on empty tables * added to the add_column test to check for the warning to be raised
1 parent 34bcbdd commit ae371f2

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/lgdo/types/table.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(
7878

7979
# if no col_dict, just set the size (default to 1024)
8080
else:
81-
self.size = size if size is not None else 1024
81+
self.size = size if size is not None else None
8282

8383
# always start at loc=0
8484
self.loc = 0
@@ -137,8 +137,19 @@ def add_field(self, name: str, obj: LGDO, use_obj_size: bool = False) -> None:
137137

138138
super().add_field(name, obj)
139139

140+
if self.size is None:
141+
self.size = len(obj)
142+
140143
# check / update sizes
141144
if self.size != len(obj):
145+
warn(
146+
f"warning: you are trying to add {name} with length {len(obj)} to a table with size {self.size} and data might be lost. \n"
147+
f"With 'use_obj_size' set to:\n"
148+
f" - True, the table will be resized to length {len(obj)} by padding/clipping its columns.\n"
149+
f" - False (default), object {name} will be padded/clipped to length {self.size}.",
150+
UserWarning,
151+
stacklevel=2,
152+
)
142153
new_size = len(obj) if use_obj_size else self.size
143154
self.resize(new_size=new_size)
144155

tests/types/test_table.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import warnings
4+
35
import awkward as ak
46
import numpy as np
57
import pandas as pd
@@ -11,7 +13,7 @@
1113

1214
def test_init():
1315
tbl = Table()
14-
assert tbl.size == 1024
16+
assert not tbl.size
1517
assert tbl.loc == 0
1618

1719
tbl = Table(size=10)
@@ -68,6 +70,11 @@ def test_add_column():
6870
tbl = Table()
6971
tbl.add_column("a", lgdo.Array(np.array([1, 2, 3])), use_obj_size=True)
7072
assert tbl.size == 3
73+
with warnings.catch_warnings(record=True) as w:
74+
warnings.simplefilter("always")
75+
tbl.add_column("b", lgdo.Array(np.array([1, 2, 3, 4])))
76+
assert len(w) == 1
77+
assert issubclass(w[-1].category, UserWarning)
7178

7279

7380
def test_join():
@@ -88,7 +95,7 @@ def test_join():
8895

8996

9097
def test_view_as():
91-
tbl = Table(4)
98+
tbl = Table(3)
9299
tbl.add_column("a", lgdo.Array(np.array([1, 2, 3]), attrs={"units": "m"}))
93100
tbl.add_column("b", lgdo.Array(np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])))
94101
tbl.add_column(
@@ -102,8 +109,8 @@ def test_view_as():
102109
"d",
103110
lgdo.Table(
104111
col_dict={
105-
"a": lgdo.Array(np.array([2, 4, 6, 8]), attrs={"units": "m"}),
106-
"b": lgdo.Array(np.array([[1, 1], [2, 4], [3, 9], [4, 16]])),
112+
"a": lgdo.Array(np.array([2, 4, 6]), attrs={"units": "m"}),
113+
"b": lgdo.Array(np.array([[1, 1], [2, 4], [3, 9]])),
107114
}
108115
),
109116
)

0 commit comments

Comments
 (0)