diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 90618cd6e235f..aca7060ea1a3d 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -34,6 +34,12 @@ def verify_pickle(self, index): unpickled = tm.round_trip_pickle(index) assert index.equals(unpickled) + def _fix_tz(self, new_index, orig_index): + if hasattr(orig_index, 'tz'): + assert new_index.tz is None + new_index = new_index.tz_localize('UTC').tz_convert(orig_index.tz) + return new_index + def test_pickle_compat_construction(self): # this is testing for pickle compat if self._holder is None: @@ -278,6 +284,8 @@ def test_ensure_copied_data(self): index_type = index.__class__ result = index_type(index.values, copy=True, **init_kwargs) + result = self._fix_tz(result, index) + tm.assert_index_equal(index, result) tm.assert_numpy_array_equal(index.values, result.values, check_same='copy') @@ -501,11 +509,13 @@ def test_repeat(self): rep = 2 i = self.create_index() expected = pd.Index(i.values.repeat(rep), name=i.name) + expected = self._fix_tz(expected, i) tm.assert_index_equal(i.repeat(rep), expected) i = self.create_index() rep = np.arange(len(i)) expected = pd.Index(i.values.repeat(rep), name=i.name) + expected = self._fix_tz(expected, i) tm.assert_index_equal(i.repeat(rep), expected) def test_numpy_repeat(self): @@ -726,7 +736,9 @@ def test_equals(self): assert not idx.equals(np.array(idx)) # Cannot pass in non-int64 dtype to RangeIndex - if not isinstance(idx, RangeIndex): + # In tz-aware DatetimeIndex constructor, + # subarr.tz: ValueError: cannot localize from non-UTC data + if not isinstance(idx, RangeIndex) and not hasattr(idx, 'tz'): same_values = Index(idx, dtype=object) assert idx.equals(same_values) assert same_values.equals(idx) diff --git a/pandas/tests/indexes/datetimelike.py b/pandas/tests/indexes/datetimelike.py index 114940009377c..d24eac89caffa 100644 --- a/pandas/tests/indexes/datetimelike.py +++ b/pandas/tests/indexes/datetimelike.py @@ -22,7 +22,7 @@ def test_str(self): if hasattr(idx, 'tz'): if idx.tz is not None: - assert idx.tz in str(idx) + assert str(idx.tz) in str(idx) if hasattr(idx, 'freq'): assert "freq='%s'" % idx.freqstr in str(idx) diff --git a/pandas/tests/indexes/datetimes/test_datetimelike.py b/pandas/tests/indexes/datetimes/test_datetimelike.py index 538e10e6011ec..94e5e05ff3b91 100644 --- a/pandas/tests/indexes/datetimes/test_datetimelike.py +++ b/pandas/tests/indexes/datetimes/test_datetimelike.py @@ -76,3 +76,85 @@ def test_union(self): for case in cases: result = first.union(case) assert tm.equalContents(result, everything) + + +class TestDatetimeTZIndex(DatetimeLike): + _holder = DatetimeIndex + + def setup_method(self, method): + # TODO: Consider adding another test instance that crosses + # DST boundary? Currently such fails a lot of tests. + tz = "US/Eastern" + self.indices = dict(index=tm.makeDateIndex(10).tz_localize(tz), + index_dec=date_range('20130101', periods=10, + freq='-1D').tz_localize(tz)) + self.setup_indices() + + def test_pickle_compat_construction(self): + pass + + def test_repr_roundtrip(self): + # idx = self.create_index() + # tm.assert_index_equal(eval(repr(idx)), idx) + + # The constructor is re-localizing to the dtype's TZ: + # Index values are different (100.0 %) + # [left]: DatetimeIndex(['2013-01-01 05:00:00-05:00', + # '2013-01-02 05:00:00-05:00', + # '2013-01-03 05:00:00-05:00', + # '2013-01-04 05:00:00-05:00', + # '2013-01-05 05:00:00-05:00'], + # dtype='datetime64[ns, US/Eastern]', freq='D') + # [right]: DatetimeIndex(['2013-01-01 00:00:00-05:00', + # '2013-01-02 00:00:00-05:00', + # '2013-01-03 00:00:00-05:00', + # '2013-01-04 00:00:00-05:00', + # '2013-01-05 00:00:00-05:00'], + # dtype='datetime64[ns, US/Eastern]', freq='D') + pass + + # Disable following tests currently because they test comparing values + # with numpy arrays etc which are tz-naive, leading to issues. + def test_intersection_base(self): + # Subset of the full test + for name, idx in pd.compat.iteritems(self.indices): + first = idx[:5] + second = idx[:3] + intersect = first.intersection(second) + + assert tm.equalContents(intersect, second) + # TODO: intersection with numpy array doesn't place nice + # because of the mixture of tz-naive and tz-aware timestamps + + def test_union_base(self): + pass + + def test_symmetric_difference(self): + pass + + def test_shift(self): + + # test shift for datetimeIndex and non datetimeIndex + # GH8083 + + drange = self.create_index() + result = drange.shift(1) + expected = DatetimeIndex(['2013-01-02', '2013-01-03', '2013-01-04', + '2013-01-05', + '2013-01-06'], freq='D', dtype=drange.dtype) + tm.assert_index_equal(result, expected) + + result = drange.shift(-1) + expected = DatetimeIndex(['2012-12-31', '2013-01-01', '2013-01-02', + '2013-01-03', '2013-01-04'], + freq='D', dtype=drange.dtype) + tm.assert_index_equal(result, expected) + + result = drange.shift(3, freq='2D') + expected = DatetimeIndex(['2013-01-07', '2013-01-08', '2013-01-09', + '2013-01-10', + '2013-01-11'], freq='D', dtype=drange.dtype) + tm.assert_index_equal(result, expected) + + def create_index(self): + return date_range('20130101', periods=5).tz_localize("US/Eastern")