Skip to content
Open
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
46 changes: 37 additions & 9 deletions astropy/timeseries/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,46 @@ def _check_required_columns(self):
required_columns = self._required_columns

plural = 's' if len(required_columns) > 1 else ''
required_count = len(required_columns)

if not self._required_columns_relax and len(self.colnames) == 0:

raise ValueError("{} object is invalid - expected '{}' "
"as the first column{} but time series has no columns"
.format(self.__class__.__name__, required_columns[0], plural))

elif self.colnames[:len(required_columns)] != required_columns:

raise ValueError("{} object is invalid - expected '{}' "
"as the first column{} but found '{}'"
.format(self.__class__.__name__, required_columns[0], plural, self.colnames[0]))
if required_count == 1:
raise ValueError("{} object is invalid - expected '{}' "
"as the first column{} but time series has no columns"
.format(self.__class__.__name__, required_columns[0], plural))

raise ValueError(
"{} object is invalid - required {!r} as the first columns "
"but time series has no columns".format(
self.__class__.__name__,
required_columns,
)
)

actual_prefix = self.colnames[:required_count]

if actual_prefix != required_columns:

if required_count == 1:
raise ValueError(
"{} object is invalid - expected '{}' "
"as the first column{} but found '{}'".format(
self.__class__.__name__,
required_columns[0],
plural,
self.colnames[0],
)
)

raise ValueError(
"{} object is invalid - required {!r} as the first columns "
"but found {!r}".format(
self.__class__.__name__,
required_columns,
actual_prefix,
)
)

if (self._required_columns_relax
and self._required_columns == self.colnames[:len(self._required_columns)]):
Expand Down
25 changes: 23 additions & 2 deletions astropy/timeseries/tests/test_sampled.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ def test_empty_initialization_invalid():
"'time' as the first column but found 'flux'")


def test_required_columns_missing_non_time_required():
ts = TimeSeries(time=INPUT_TIME)
ts['a'] = [1, 2, 3]

ts._required_columns = ['time', 'flux']
ts.add_column([1, 2, 3], name='flux', index=1)

with pytest.raises(ValueError) as exc:
ts.remove_column('flux')

assert str(exc.value) == (
"TimeSeries object is invalid - required ['time', 'flux'] as the first "
"columns but found ['time', 'a']"
)


def test_initialize_only_time():
ts = TimeSeries(time=INPUT_TIME)
assert ts['time'] is ts.time
Expand Down Expand Up @@ -304,7 +320,10 @@ def test_pandas():
def test_read_time_missing():
with pytest.raises(ValueError) as exc:
TimeSeries.read(CSV_FILE, format='csv')
assert exc.value.args[0] == '``time_column`` should be provided since the default Table readers are being used.'
assert exc.value.args[0] == (
'``time_column`` should be provided since the default Table readers are '
'being used.'
)


def test_read_time_wrong():
Expand Down Expand Up @@ -339,7 +358,9 @@ def test_kepler_astropy():

@pytest.mark.remote_data(source='astropy')
def test_tess_astropy():
filename = get_pkg_data_filename('timeseries/hlsp_tess-data-alerts_tess_phot_00025155310-s01_tess_v1_lc.fits')
filename = get_pkg_data_filename(
'timeseries/hlsp_tess-data-alerts_tess_phot_00025155310-s01_tess_v1_lc.fits'
)
with pytest.warns(UserWarning, match='Ignoring 815 rows with NaN times'):
timeseries = TimeSeries.read(filename, format='tess.fits')
assert timeseries["time"].format == 'isot'
Expand Down