diff --git a/stellarphot/io/aavso.py b/stellarphot/io/aavso.py index 2e32d928..bae0a587 100644 --- a/stellarphot/io/aavso.py +++ b/stellarphot/io/aavso.py @@ -242,8 +242,25 @@ def to_table(self): length_limit = v.get('limit', None) if length_limit: if v['type'] == 'float': - table[k].info.format = f"{length_limit}f" + # For float, the width in the format specifier is the _minimum_ + # width of the field, not the maximum. Since, most of the time, + # any width issues will arise because some rounding is needed, + # use the width to calculate the place to which to round the + # number, EXCEPT in the case of time. There, use all the digits and + # hope for the best. + if k != 'DATE': + max_precision = length_limit - 3 + precision = min(max_precision, 6) + else: + precision = 8 + + # Check whether table column is all "na" -- if that is the case, don't + # set a format. + if not all(table[k] == "na"): + table[k].info.format = f"{length_limit}.{precision}f" elif v['type'] == 'str': + # For a string, the "precision" in the format is the maximum + # size of the field. if k == 'STARID': print(table[k]) table[k].info.format = f".{length_limit}s" diff --git a/stellarphot/io/tests/test_aavso.py b/stellarphot/io/tests/test_aavso.py index 8155fa0b..19b91c94 100644 --- a/stellarphot/io/tests/test_aavso.py +++ b/stellarphot/io/tests/test_aavso.py @@ -86,8 +86,10 @@ def test_making_table_ensemble(): star_id=26) -def test_table_column_formats(tmp_path): +@pytest.mark.parametrize("ensemble", [True, False]) +def test_table_column_formats(tmp_path, ensemble): aef, input_data = set_up_aef() + aef.ensemble = ensemble aef_table = aef.to_table() aef_table.write(tmp_path / "test.csv") with open(tmp_path / "test.csv") as f: