Skip to content

Commit c015147

Browse files
committed
Reorder columns before returning the table
1 parent 78e7340 commit c015147

File tree

4 files changed

+51
-23
lines changed

4 files changed

+51
-23
lines changed

astroquery/eso/core.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
from ..query import QueryWithLogin
3939
from ..utils import schema
4040
from .utils import py2adql, _split_str_as_list_of_str, \
41-
adql_sanitize_val, are_coords_valid, reorder_columns
41+
adql_sanitize_val, are_coords_valid, reorder_columns, \
42+
DEFAULT_LEAD_COLS_PHASE3, DEFAULT_LEAD_COLS_RAW
43+
4244

4345
__doctest_skip__ = ['EsoClass.*']
4446

@@ -301,7 +303,6 @@ def query_tap_service(self,
301303
table_to_return = None
302304
tap_service = self._tap_service(authenticated)
303305
table_to_return = self._try_download_pyvo_table(query_str, tap_service)
304-
table_to_return = reorder_columns(table_to_return)
305306
return table_to_return
306307

307308
@unlimited_max_rec
@@ -522,7 +523,7 @@ def query_surveys(
522523
_ = open_form, cache # make explicit that we are aware these arguments are unused
523524
c = column_filters if column_filters else {}
524525
kwargs = {**kwargs, **c}
525-
return self._query_on_allowed_values(table_name=EsoNames.phase3_table,
526+
t = self._query_on_allowed_values(table_name=EsoNames.phase3_table,
526527
column_name=EsoNames.phase3_surveys_column,
527528
allowed_values=surveys,
528529
cone_ra=cone_ra,
@@ -535,6 +536,8 @@ def query_surveys(
535536
print_help=help,
536537
authenticated=authenticated,
537538
**kwargs)
539+
t = reorder_columns(t, DEFAULT_LEAD_COLS_PHASE3)
540+
return t
538541

539542
@deprecated_renamed_argument(('open_form', 'cache'), (None, None),
540543
since=['0.4.11', '0.4.11'])
@@ -615,7 +618,7 @@ def query_main(
615618
_ = open_form, cache # make explicit that we are aware these arguments are unused
616619
c = column_filters if column_filters else {}
617620
kwargs = {**kwargs, **c}
618-
return self._query_on_allowed_values(table_name=EsoNames.raw_table,
621+
t = self._query_on_allowed_values(table_name=EsoNames.raw_table,
619622
column_name=EsoNames.raw_instruments_column,
620623
allowed_values=instruments,
621624
cone_ra=cone_ra,
@@ -628,6 +631,8 @@ def query_main(
628631
print_help=help,
629632
authenticated=authenticated,
630633
**kwargs)
634+
t = reorder_columns(t, DEFAULT_LEAD_COLS_RAW)
635+
return t
631636

632637
@deprecated_renamed_argument(('open_form', 'cache'), (None, None),
633638
since=['0.4.11', '0.4.11'])
@@ -707,19 +712,21 @@ def query_instrument(
707712
_ = open_form, cache # make explicit that we are aware these arguments are unused
708713
c = column_filters if column_filters else {}
709714
kwargs = {**kwargs, **c}
710-
return self._query_on_allowed_values(table_name=EsoNames.ist_table(instrument),
711-
column_name=None,
712-
allowed_values=None,
713-
cone_ra=cone_ra,
714-
cone_dec=cone_dec,
715-
cone_radius=cone_radius,
716-
columns=columns,
717-
top=top,
718-
count_only=count_only,
719-
query_str_only=query_str_only,
720-
print_help=help,
721-
authenticated=authenticated,
722-
**kwargs)
715+
t = self._query_on_allowed_values(table_name=EsoNames.ist_table(instrument),
716+
column_name=None,
717+
allowed_values=None,
718+
cone_ra=cone_ra,
719+
cone_dec=cone_dec,
720+
cone_radius=cone_radius,
721+
columns=columns,
722+
top=top,
723+
count_only=count_only,
724+
query_str_only=query_str_only,
725+
print_help=help,
726+
authenticated=authenticated,
727+
**kwargs)
728+
t = reorder_columns(t, DEFAULT_LEAD_COLS_RAW)
729+
return t
723730

724731
def get_headers(self, product_ids, *, cache=True):
725732
"""

astroquery/eso/tests/test_eso.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,32 @@ def test_reorder_columns(monkeypatch):
342342
table2 = reorder_columns(table)
343343
names_after = table2.colnames[:]
344344

345+
# the columns we want to change are actually in the table
345346
assert set(DEFAULT_LEAD_COLS_RAW).issubset(names_before)
346347
assert set(DEFAULT_LEAD_COLS_RAW).issubset(names_after)
348+
349+
# no columns are removed nor added
347350
assert set(names_before) == set(names_after)
348-
assert table != table2
349-
assert names_before[:5] != names_after[:5]
350351

351-
for n in table.colnames:
352-
assert table[[n]].values_equal(table2[[n]]), n
352+
# Column by column, the contents are the same
353+
for cname in table.colnames:
354+
assert table[[cname]].values_equal(table2[[cname]]), f"Error for {cname}"
355+
356+
# empty table doesn't cause a crash
357+
empty_1 = Table()
358+
empty_2 = reorder_columns(empty_1)
359+
assert len(empty_1) == 0 and isinstance(empty_1, Table)
360+
assert len(empty_2) == 0 and isinstance(empty_1, Table)
361+
362+
# If the values we're looking for as leading columns, everything stays the same
363+
some_table = Table({"x": [1, 2, 3], "y": [4, 5, 6]})
364+
same_table = reorder_columns(some_table)
365+
assert all(some_table == same_table), "Table with no cols to change fails"
366+
367+
# If what we pas is not a table, the function has no effect
368+
not_a_table_1 = object()
369+
not_a_table_2 = reorder_columns(not_a_table_1)
370+
assert not_a_table_1 == not_a_table_2
353371

354372

355373
def test_py2adql():

astroquery/eso/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ def reorder_columns(table: Table,
2626
['object', 'ra', 'dec', 'dp_id', 'date_obs']
2727
Returns a table with the columns reordered.
2828
"""
29+
if not isinstance(table, Table):
30+
return table
31+
2932
leading_cols = leading_cols or DEFAULT_LEAD_COLS_RAW
3033
first_cols = []
3134
last_cols = table.colnames[:]

docs/eso/eso.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ return two columns: the date of observation and the name of the object.
196196

197197
.. doctest-remote-data::
198198
>>> table = eso.query_instrument('midi', column_filters={'object':'NGC4151'}, columns=['object', 'date_obs'])
199-
>>> t_left = table[table["date_obs"] >= "2008-01-01"]
200-
>>> t_2008_2009 = t_left[t_left["date_obs"] <= "2009-05-12"]
199+
>>> t_2008 = table[table["date_obs"] >= "2008-01-01"]
200+
>>> t_2008_2009 = t_2008[t_2008["date_obs"] <= "2009-05-12"]
201201
>>> t_2008_2009
202202
<Table length=196>
203203
object date_obs

0 commit comments

Comments
 (0)