Repository: pandas-dev/pandas

Issue:
DateTime indexing: DateParseError: global name 'tz' is not defined
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
Out[132]: 
<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':]

<...elided...>

/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)
    237 
    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!



Discussion:
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]: d.index.tz
Out[144]: <DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>

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

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:]
Out[147]: 
<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':]
Out[153]: 
<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
Out[154]: 
<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
```


Resolving patch:
diff --git a/pandas/tseries/tools.py b/pandas/tseries/tools.py
--- a/pandas/tseries/tools.py
+++ b/pandas/tseries/tools.py
@@ -245,6 +245,9 @@ def dateutil_parse(timestr, default,
                    ignoretz=False, tzinfos=None,
                    **kwargs):
     """ lifted from dateutil to get resolution"""
+    from dateutil import tz
+    import time
+
     res = DEFAULTPARSER._parse(StringIO(timestr), **kwargs)
 
     if res is None:


Test patch:
diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py
--- a/pandas/tseries/tests/test_timeseries.py
+++ b/pandas/tseries/tests/test_timeseries.py
@@ -1399,6 +1399,14 @@ def _simple_ts(start, end, freq='D'):
 class TestDatetimeIndex(unittest.TestCase):
     _multiprocess_can_split_ = True
 
+    def test_stringified_slice_with_tz(self):
+        #GH2658
+        import datetime
+        start=datetime.datetime.now()
+        idx=DatetimeIndex(start=start,freq="1d",periods=10)
+        df=DataFrame(range(10),index=idx)
+        df["2013-01-14 23:44:34.437768-05:00":] # no exception here
+
     def test_append_join_nondatetimeindex(self):
         rng = date_range('1/1/2000', periods=10)
         idx = Index(['a', 'b', 'c', 'd'])