Analysing Time Series in Python - pandas formatting error - statsmodels
Asked Answered
D

1

17

I am trying to analyse stars' data. I have light time series of the stars and I want to predict to which class (among 4 different types) they belong. I have light time series of those stars, and I want to analyse those time series by doing deseasonalisation, frequencies analysis and other potentially relevant studies.

The object time_series is a panda DataFrame, including 10 columns : time_points_b, light_points_b (the b being for blue), etc...

I first want to study the blue light time series.

import statsmodels.api as sm;
import pandas as pd
import matplotlib.pyplot as plt
pd.options.display.mpl_style = 'default'
%matplotlib inline

def star_key(slab_id, star_id_b):
    return str(slab_id) + '_' + str(star_id_b)

raw_time_series = pd.read_csv("data/public/train_varlength_features.csv.gz", index_col=0, compression='gzip')
time_series = raw_time_series.applymap(csv_array_to_float)


time_points = np.array(time_series.loc[star_key(patch_id, star_id_b)]['time_points_b'])
light_points = np.array(time_series.loc[star_key(patch_id, star_id_b)]['light_points_b'])
error_points = np.array(time_series.loc[star_key(patch_id, star_id_b)]['error_points_b'])

light_data = pd.DataFrame({'time':time_points[:], 'light':light_points[:]})
residuals = sm.tsa.seasonal_decompose(light_data);

light_plt = residuals.plot()
light_plt.set_size_inches(10, 5)
light_plt.tight_layout()

This code gives me an attribute error when I apply the seasonal_decompose method : AttributeError: 'Int64Index' object has no attribute 'inferred_freq'

Doug answered 17/12, 2015 at 19:7 Comment(0)
M
25

seasonal_decompose() expects a DateTimeIndex on your DataFrame. Here's an example:

enter image description here

Madson answered 17/12, 2015 at 19:21 Comment(2)
Where is the documentation of this "seasonal_decompose" function? How did you know it is expecting a DateTimeIndex? I tried to find it online, but couldnt, only tiny examples of use. And I would like to see the inputs and outputs this function expects/produces. Thanks!Unassuming
You can get the basics of usage from the function's docstring (there's several ways to do that, but one way is print sm.tsa.seasonal_decompose.__doc__). As for the DateTimeIndex, I had to piece that together from the 'Int64Index' object has no attribute 'inferred_freq' error. A quick Google search turned up that it's a part of pandas' DateTimeIndex, so just gave that a shot and it worked.Madson

© 2022 - 2024 — McMap. All rights reserved.