ValueError: Given a pandas object and the index does not contain dates

timeseries
pandas
python

#1

Hello friend, I am doing time series forecasting but i am stuck one place.its ValueError: Given a pandas object and the index does not contain dates. why this error occurs? here is my code
con = pymysql.connect(host=‘10’,
port=0000,
user=‘system’,
passwd=‘hq’,
db=‘reportingl’)
cur = “SELECT date(calllocalTime) as datum,count(id) as incoming from sogedes-db.odcalls where CAST(callLocalTime AS DATE) between ‘2015-09-02’ and curdate() and LastCampaign in (9807,9808,9809,9810) and dayofweek(callLocalTime) Not in (1) and calltype = 1 and FirstQueue != 0 group by datum” ;

data = pd.read_sql_query(cur,con, index_col=None, coerce_float=True, params=None, parse_dates=None)
ts = data[‘incoming’]
ts_log = np.log(ts)

expwighted_avg = pd.ewma(ts_log, halflife=12)
plt.plot(ts_log)
plt.plot(expwighted_avg, color=‘red’)

ts_log_diff = ts_log - ts_log.shift()
plt.plot(ts_log_diff)
ts_log_diff.dropna(inplace=True)
test_stationarity(ts_log_diff)

lag_acf = acf(ts_log_diff, nlags=20)
lag_pacf = pacf(ts_log_diff, nlags=20, method=‘ols’)

plt.subplot(121)
plt.plot(lag_acf)
plt.axhline(y=0,linestyle=’–’,color=‘gray’)
plt.axhline(y=-1.96/np.sqrt(len(ts_log_diff)),linestyle=’–’,color=‘gray’)
plt.axhline(y=1.96/np.sqrt(len(ts_log_diff)),linestyle=’–’,color=‘gray’)
plt.title(‘Autocorrelation Function’)

plt.subplot(122)
plt.plot(lag_pacf)
plt.axhline(y=0,linestyle=’–’,color=‘gray’)
plt.axhline(y=-1.96/np.sqrt(len(ts_log_diff)),linestyle=’–’,color=‘gray’)
plt.axhline(y=1.96/np.sqrt(len(ts_log_diff)),linestyle=’–’,color=‘gray’)
plt.title(‘Partial Autocorrelation Function’)
plt.tight_layout()

model = ARIMA(ts_log, order=[2, 1, 0])
results_AR = model.fit(disp=-1)
plt.plot(ts_log_diff)
plt.plot(results_AR.fittedvalues, color=‘red’)
plt.title(‘RSS: %.4f’% sum((results_AR.fittedvalues-ts_log_diff)**2))

its said model = ARIMA(ts_log, order=[2, 1, 0]) has error

ValueError: Given a pandas object and the index does not contain dates


#2

Hi @prakhar278, did you convert your index into datetime index? Here’s a similar problem and solution


#3

No … its is really important.I means my query show me answer like this 2016-01-02 181,2016-01-03 192.


#4

and how can i convert my index into datetime.


#5

Hi @prakhar278, I could not inderstand your problem correctly, could you elaborate please?

In regards to your second question, like this

pd.to_datetime(df.index)

where df is your dataframe.


#6

hi i tired this in my code data = pd.to_datetime(data.index)
but this give me error IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices
in this place ts = data[‘incoming’]