Skip to content


Subversion checkout URL

You can clone with
Download ZIP


DateTime indexing: DateParseError: global name 'tz' is not defined #2658

Bklyn opened this Issue · 4 comments

2 participants


I have a DataFrame with a DateTime index that I have read from a CSV file:

In [131]: d=pd.read_csv('data.csv', parse_dates=[0], index_col=0)

The index values consist of ISO-8601-formatted timestamps of the form "2012-12-28T09:00:54.273000-0500", which Pandas seems to handle perfectly:

In [132]: d.index
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-12-28 09:00:54.272000, ..., 2012-12-28 09:59:46.518000]
Length: 1310, Freq: None, Timezone: tzoffset(None, -18000)

But if I try to access data using stringified times, I get this error (I've left the last 2 stack fames for brevity):

In [133]: d['2012-12-28 09:30:00-05:00':]
DateParseError                            Traceback (most recent call last)
<ipython-input-133-4564ed4dbf71> in <module>()
----> 1 d['2012-12-28 09:30:00-05:00':]


/usr/local/lib/python2.7/dist-packages/pandas/tseries/index.pyc in _get_string_slice(self, key)
   1126         freq = getattr(self, 'freqstr',
   1127                        getattr(self, 'inferred_freq', None))
-> 1128         asdt, parsed, reso = parse_time_string(key, freq)
   1129         key = asdt
   1130         loc = self._partial_date_slice(reso, parsed)

/usr/local/lib/python2.7/dist-packages/pandas/tseries/tools.pyc in parse_time_string(arg, freq, dayfirst, yearfirst)
    234                                       yearfirst=yearfirst)
    235     except Exception, e:
--> 236         raise DateParseError(e)
    238     if parsed is None:

DateParseError: global name 'tz' is not defined

If I specify the timestamp without the GMT offset, I must specify it in GMT (e.g. 9:30am my time is "2012-12-28 14:30:00") which is awkward. I can use localized datetime objects instead of strings, but this is also a bit of a pain.

Is there a fix for this "tz" error so I can use plain ol' strings? Thanks!


To follow up on the "localized datetime" assertion above, some more info:

# Not strictly necessary, but makes #145 nice and simple
In [143]: d=d.tz_convert('US/Eastern')

In [144]:
Out[144]: <DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>

In [145]: mktopen=datetime.datetime(2012,12,28,9,30,0,0,

In [146]: mktopen
Out[146]: datetime.datetime(2012, 12, 28, 9, 30, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>)

In [147]: d[mktopen:]
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1027 entries, 2012-12-28 09:30:00.009000-05:00 to 2012-12-28 09:59:46.518000-05:00

If I use a string with no GMT offset, it behaves as if the value were in UTC:

In [153]: d['2012-12-28 09:30:00':]
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1310 entries, 2012-12-28 09:00:54.272000-05:00 to 2012-12-28 09:59:46.518000-05:00

In [154]: d.index
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-12-28 09:00:54.272000, ..., 2012-12-28 09:59:46.518000]
Length: 1310, Freq: None, Timezone: US/Eastern
y-p commented

thanks for the bug report. some missing imports in code reused from dateutil.


This fixes the bug mentioned above, though there is one more. I find I need to call tz_convert() on my DataFrame before I can plot it. By default the index tz is a "tzoffset" I get an exception when plottng.

AttributeError: 'module' object has no attribute 'tz'

d=pd.read_csv('vix.dlmp.csv', parse_dates=[0], index_col=0)
Out[23]: tzoffset(None, -18000)

d['2012-12-28 09:30:15-05:00':]
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 981 entries, 2012-12-28 09:30:16.311000-05:00 to 2012-12-28 09:59:46.518000-05:00
Data columns:
Und            981  non-null values
BidPrice       981  non-null values
AskPrice       981  non-null values
TradePrice     981  non-null values
BidSize        981  non-null values
AskSize        981  non-null values
TradeSize      981  non-null values
T0             981  non-null values
T1             981  non-null values
T2             981  non-null values
T3             981  non-null values
T4             981  non-null values
T5             981  non-null values
dtypes: float64(9), int64(3), object(1)

d['2012-12-28 09:30:15-05:00':].plot(y=['BidPrice', 'AskPrice', 'TradePrice', 'T0', 'T1'],style=['g', 'b', 'r', 'k--', 'g--'],drawstyle='steps',figsize=(16,9))

AttributeError                            Traceback (most recent call last)
<ipython-input-25-d7985a840c41> in <module>()
----> 1 d['2012-12-28 09:30:15-05:00':].plot(y=['BidPrice', 'AskPrice', 'TradePrice', 'T0', 'T1'],style=['g', 'b', 'r', 'k--', 'g--'],drawstyle='steps',figsize=(16,9))

/usr/local/lib/python2.7/dist-packages/pandas/tools/plotting.pyc in plot_frame(frame, x, y, subplots, sharex, sharey, use_index, figsize, grid, legend, rot, ax, style, title, xlim, ylim, logy, xticks, yticks, kind, sort_columns, fontsize, secondary_y, **kwds)
   1419                            grid=grid, logy=logy, secondary_y=secondary_y,
   1420                            title=title, figsize=figsize, fontsize=fontsize,
-> 1421                            **kwds)
   1423     plot_obj = klass(frame, kind=kind, subplots=subplots, rot=rot,

/usr/local/lib/python2.7/dist-packages/pandas/tools/plotting.pyc in plot_series(series, label, kind, use_index, rot, xticks, yticks, xlim, ylim, ax, style, grid, legend, logy, secondary_y, **kwds)
   1504                      secondary_y=secondary_y, **kwds)
-> 1506     plot_obj.generate()
   1507     plot_obj.draw()

/usr/local/lib/python2.7/dist-packages/pandas/tools/plotting.pyc in generate(self)
    731         self._compute_plot_data()
    732         self._setup_subplots()
--> 733         self._make_plot()
    734         self._post_plot_logic()
    735         self._adorn_subplots()

/usr/local/lib/python2.7/dist-packages/pandas/tools/plotting.pyc in _make_plot(self)
   1063             lines = []
   1064             labels = []
-> 1065             x = self._get_xticks(convert_period=True)
   1067             plotf = self._get_plot_function()

/usr/local/lib/python2.7/dist-packages/pandas/tools/plotting.pyc in _get_xticks(self, convert_period)
    878                 values for xaxis, several actions are already taken by plt.
    879                 """
--> 880                 x = index._mpl_repr()
    881             else:
    882                 self._need_to_set_index = True

/usr/local/lib/python2.7/dist-packages/pandas/tseries/index.pyc in _mpl_repr(self)
    474     def _mpl_repr(self):
    475         # how to represent ourselves to matplotlib
--> 476         return tslib.ints_to_pydatetime(self.asi8,
    478     def __repr__(self):

/usr/local/lib/python2.7/dist-packages/pandas/ in pandas.tslib.ints_to_pydatetime (pandas/tslib.c:2998)()

AttributeError: 'tzoffset' object has no attribute '_transition_info'

In [26]: d=d.tz_convert('US/Eastern')

In [27]: d['2012-12-28 09:30:15-05:00':].plot(y=['BidPrice', 'AskPrice', 'TradePrice', 'T0', 'T1'],style=['g', 'b', 'r', 'k--', 'g--'],drawstyle='steps',figsize=(16,9))
Out[27]: <matplotlib.axes.AxesSubplot at 0x4c25a50>

closing this, moved the new issue to another ticket.

@y-p y-p closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.