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

Prevent np.str_ column names in SourceCatalog table #1956

Merged
merged 1 commit into from
Nov 20, 2024
Merged
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
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