0

when i use plt to draw the plots(more the one in the same pic), then given a bug: TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'UnitData'

how to fix this bug? thanks

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.style as psl
import datetime
import os
os.chdir(r'D:\Code\python code')

from statsmodels.tsa.arima.model import ARIMA
import pmdarima as pm

date_parse = lambda dates: pd.datetime.strptime(dates, '%Y/%m/%d')
df = pd.read_table('ch4_time_series.txt', sep=',', index_col='Date', date_parser=date_parse)
print(df.head())
print(df.shape)

train_df = df.iloc[:125]
test_df = df.iloc[125:]

model = pm.auto_arima(train_df.Orders, start_p=1, d=None, start_q=1,
                      information_criterion='aic',
                      test='adf', # 
                      max_p=3, max_q=3,
                      # max_d=2, 
                      m=1,
                      seasonal=False,
                      start_P=0,
                      D=0,
                      trace=True,
                      error_action='ignore',
                      suppress_warnings=True,
                      stepwise=True)
print(model.summary())

n_periods = 10 # The number of periods in the future to forecast.
fc, confit = model.predict(n_periods=n_periods,
                           return_conf_int = True 
                           )
index_of_fc = pd.date_range(max(train_df.index), periods = n_periods)
fc_series = pd.Series(fc, index=index_of_fc)
lower = pd.Series(confit[:, 0], index=index_of_fc)
upper = pd.Series(confit[:, 1], index=index_of_fc)

plt.figure()
plt.subplot(1,1,1)
psl.use('seaborn-dark')
plt.plot(train_df.Orders, ls='-', label='True_train_df')
plt.plot(fc_series, color='darkgreen')
plt.fill_between(index_of_fc,
                  lower,
                  upper,
                  color='k',
                  alpha=0.15)
plt.xticks(rotation=60)
plt.legend(loc=1)

The error I get is: TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'UnitData'

and the version info as follow: numpy version: 1.21.5 pandas version: 1.4.2 matplotlib version: 3.5.1

this is the Traceback:

[<matplotlib.lines.Line2D at 0x1914b1a44c0>]Traceback (most recent call last):
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\backends\backend_qt.py", line 455, in _draw_idle
    self.draw()
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 436, in draw
    self.figure.draw(self.renderer)
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\artist.py", line 73, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\figure.py", line 2810, in draw
    mimage._draw_list_compositing_images(
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 3082, in draw
    mimage._draw_list_compositing_images(
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\axis.py", line 1158, in draw
    ticks_to_draw = self._update_ticks()
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\axis.py", line 1045, in _update_ticks
    major_locs = self.get_majorticklocs()
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\axis.py", line 1277, in get_majorticklocs
    return self.major.locator()
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\dates.py", line 1340, in __call__
    dmin, dmax = self.viewlim_to_dt()
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\dates.py", line 1125, in viewlim_to_dt
    return num2date(vmin, self.tz), num2date(vmax, self.tz)
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\dates.py", line 528, in num2date
    return _from_ordinalf_np_vectorized(x, tz).tolist()
  File "D:\Program Files\Anaconda3\lib\site-packages\numpy\lib\function_base.py", line 2163, in __call__
    return self._vectorize_call(func=func, args=vargs)
  File "D:\Program Files\Anaconda3\lib\site-packages\numpy\lib\function_base.py", line 2246, in _vectorize_call
    outputs = ufunc(*inputs)
  File "D:\Program Files\Anaconda3\lib\site-packages\matplotlib\dates.py", line 359, in _from_ordinalf
    dt = dt.astimezone(tz)
TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'UnitData'
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • There's no `tzinfo` anywhere in your code. Please add the full traceback so we can see where this is happening. – Barmar Jul 21 '22 at 09:15
  • @Barmar I've added the traceback, thanks so much! – Elon Jiang Jul 21 '22 at 10:17
  • This seems to be similar: https://stackoverflow.com/questions/66466481/trying-to-connect-scatter-plotted-datetimes-gives-me-tz-error-from-matplot – Barmar Jul 21 '22 at 10:25
  • And this: https://stackoverflow.com/questions/65753940/why-do-i-get-keep-getting-this-tzinfo-error The common factor seems to be trying to plot a date range. – Barmar Jul 21 '22 at 10:26
  • Thanks so much! @Barmar This loop for date is the answer! from this link that you sent to me: https://stackoverflow.com/questions/66466481/trying-to-connect-scatter-plotted-datetimes-gives-me-tz-error-from-matplot the new code: date_parse = lambda dates: pd.datetime.strptime(dates, '%Y/%m/%d') df = pd.read_table('ch4_time_series.txt', sep=',', date_parser=date_parse) # index_col='Date', df['Date'] = [datetime.datetime.strptime(dates, '%Y/%m/%d').date() for dates in df['Date']] – Elon Jiang Jul 21 '22 at 11:21
  • Please do not add "answers" to the question body. Instead, add them as a separate answer, see [answer]. Given the question is closed as a duplicate, you can add the answer there (provided it is unique and adds something to the question of course) – Adriaan Jul 21 '22 at 11:28

0 Answers0