I recently had some time series analysis to do, and I decided to do this work in Pandas. Particualarly, I had to deal with many timeseries, stretching from different startpoints in time to different endpoints in time. Each timeseries was available as an ASCII file. The data were daily data. The time information was given in three columns, each representing year, month, and day, respectively.
I found these two posts by Randal S. Olson very useful resources:
- Using pandas DataFrames to process data from multiple replicate runs in Python (link)
- Statistical analysis made easy in Python with SciPy and pandas DataFrames (link)
Here is a cookbook style layout of what I did:
The following steps show how easy it was to deal with the data 1. Read the input data
cur_sim_ts = pa.io.parsers.read_table(os.path.join(data_path, 'test', result) , header=None , sep='\s*' , parse_dates=[[0,1, 2070]] , names=['year','month', 'day', result[:-4]] , index_col= )
where `’\s*’ means any whitespace. The dates were given in three colums, one for year, month, and day. Can it get simpler and nicer than that?
It is possible to repeat 1 multiple time, each time extending the pandas data_frame. Unfortunately, this looks a little ugly still, but works
if counter_cur_file > 0: final_df = final_df.combine_first(cur_sim_ts) else: final_df = cur_sim_ts.copy() / 10.0
In the else part, the Pandas data_frame is initialised. It so happens that this and only this series of the loop has to be divided by ten. In all other cases the time_series that was read in step 1 is “appended” (or rather combined with) the the previously initialized data_frame. The wicked thing is that each time_series is put at the propper “place” in time within the data_frame. Dates are real dates. This is beautiful, but I had to be a little carfule with the data I had at hand, in which every month has 30 days.
As soon as this data_frame is constructed, things are easy, for example
plotting, particularly plotting only a certain time-interval of the data.
saving the data_frame
For me it was of particular interest to find out, how many consecutive dry and wet days there are in each time series. I introduced a threshold of precipitation. If the daily amount of precipitation is above that threshold, this day is considered to be “wet”, else it’s considered to be “dry”. I wanted to count the number of consecutive dry and wet days, and remember them for a time series. This is the purpose of the function below. It is coded a little bute force. Still I was surprised, that it performed reasonably well. If anybody has a better idea, please let me know. Maybe it can be of use for other Pandas users. Note: a time_series in the Pandas world is obtained by looping over a data_frame
def dry_wet_spells(ts, threshold): """ returns the duration of spells below and above threshold input ----- ts a pandas timeseries threshold threshold below and above which dates are counted output ------ ntot_ts total number of measurements in ts n_lt_threshold number of measurements below threshold storage_n_cons_days array that stores the lengths of sequences storage_n_cons_days for dry days storage_n_cons_days for wet days """ # total number in ts ntot_ts = ts[~ ts.isnull()].count() # number lt threshold n_lt_threshold = ts[ts <= threshold].count() # type_day = 0 # dry # type_day = 1 # wet # initialisierung: was ist der erste Tag type_prev_day = 0 storage_n_cons_days = [,] n_cons_days = 0 for cur_day in ts[~ ts.isnull()]: # current day is dry if cur_day <= threshold: type_cur_day = 0 if type_cur_day == type_prev_day: n_cons_days += 1 else: storage_n_cons_days.append(n_cons_days) n_cons_days = 1 type_prev_day = type_cur_day else: type_cur_day = 1 if type_cur_day == type_prev_day: n_cons_days += 1 else: storage_n_cons_days.append(n_cons_days) n_cons_days = 1 type_prev_day = type_cur_day return ntot_ts, n_lt_threshold, storage_n_cons_days
- With all of this, I can produce histograms like this: