pythontime-seriesastropy

Concatenate or add rows to an Astropy Timeseries


I'm trying to read multiple CSV files from nightly observations. Each CSV file has a time and magnitude column and I want to be able to stack all my nightly files into a single Timeseries object.

It is very easy to do with a panda Dataframe (pd.concat()) but I cannot find a simple, elegant way with Astropy Timeseries. I could write the merged file to disk or other solutions but I'm looking for a simple way. It must be doable within astropy Timeseries or Astropy Table I imagine. Stacking multiple TimeSeries is a common use case.

I tried the following one:

from astropy.timeseries import TimeSeries

ts = TimeSeries.read('mydata.csv', format='ascii.csv',time_column='mjd',time_format='mjd')
ts2 = ts.add_row(vals=ts.values()) #imagine that I want to stack with another observation

Doing this throw my an Error : ValueError: Unable to insert row because of exception in column 'time':

Any idea while staying in the astropy ecosystem ?


Solution

  • You can use the astropy.table.vstack function (see, e.g., here). For example, if I had two files data1.csv and data2.csv, containing:

    mjd,   mag
    54668,  2.4
    54669,  2.5
    

    and

    mjd,   mag
    54700,  2.6
    54701,  2.7
    

    respectively, I could do:

    from astropy.table import vstack
    from astropy.timeseries import TimeSeries
    
    ts = TimeSeries.read("data1.csv", format="ascii.csv", time_column="mjd", time_format="mjd")
    ts2 = TimeSeries.read("data2.csv", format="ascii.csv", time_column="mjd", time_format="mjd")
    
    ts = vstack([ts, ts2])
    print(ts)
      time  mag
    ------- ---
    54668.0 2.4
    54669.0 2.5
    54700.0 2.6
    54701.0 2.7