Skip to content

Commit

Permalink
Prevent np.str_ column names in SourceCatalog table
Browse files Browse the repository at this point in the history
  • Loading branch information
larrybradley committed Nov 20, 2024
1 parent d73bfb7 commit fa2c9a9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ New Features
Bug Fixes
^^^^^^^^^

- ``photutils.segmentation``

- Fixed a bug where the table output from the ``SourceCatalog``
``to_table`` method could have column names with a ``np.str_``
representation instead of ``str`` representation when using NumPy
2.0+. [#1956]

API Changes
^^^^^^^^^^^

Expand Down
4 changes: 3 additions & 1 deletion photutils/segmentation/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,10 @@ def to_table(self, columns=None):
"""
if columns is None:
table_columns = self.default_columns
elif isinstance(columns, str):
table_columns = [columns]
else:
table_columns = np.atleast_1d(columns)
table_columns = columns

tbl = QTable()
tbl.meta.update(self.meta) # keep tbl.meta type
Expand Down
10 changes: 10 additions & 0 deletions photutils/segmentation/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,16 @@ def test_table(self):
assert len(tbl) == 7
assert tbl.colnames == columns

tbl = self.cat.to_table(self.cat.default_columns)
for col in tbl.columns:
assert isinstance(col, str)
assert not isinstance(col, np.str_)

tbl = self.cat.to_table('label')
for col in tbl.columns:
assert isinstance(col, str)
assert not isinstance(col, np.str_)

def test_invalid_inputs(self):
segm = SegmentationImage(np.zeros(self.data.shape, dtype=int))
match = 'segment_img must have at least one non-zero label'
Expand Down

0 comments on commit fa2c9a9

Please sign in to comment.