From 0ba1e72408934852d128d7608be0d6f68c81a7f2 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Wed, 7 Oct 2015 15:12:20 -0400 Subject: [PATCH] BUG/ERR: raise when trying to set a subset of values in a datetime64[ns, tz] column with another tz --- pandas/core/internals.py | 2 +- pandas/tests/test_frame.py | 24 ---------------- pandas/tests/test_indexing.py | 52 +++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 25 deletions(-) diff --git a/pandas/core/internals.py b/pandas/core/internals.py index 4790f3aa3841e..51f6c7043817f 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -2099,7 +2099,7 @@ def _try_coerce_args(self, values, other): if is_null_datelike_scalar(other): other = tslib.iNaT elif isinstance(other, self._holder): - if other.tz != self.tz: + if other.tz != self.values.tz: raise ValueError("incompatible or non tz-aware value") other = other.tz_localize(None).asi8 else: diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 5acc858840dfa..5a9b90f93bb0c 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -4322,30 +4322,6 @@ def test_constructor_with_datetime_tz(self): result = result.set_index('foo') tm.assert_index_equal(df.index,idx) - # indexing - result = df2.iloc[1] - expected = Series([Timestamp('2013-01-02 00:00:00-0500', tz='US/Eastern'), np.nan, np.nan], - index=list('ABC'), dtype='object', name=1) - assert_series_equal(result, expected) - result = df2.loc[1] - expected = Series([Timestamp('2013-01-02 00:00:00-0500', tz='US/Eastern'), np.nan, np.nan], - index=list('ABC'), dtype='object', name=1) - assert_series_equal(result, expected) - - # indexing - fast_xs - df = DataFrame({'a': date_range('2014-01-01', periods=10, tz='UTC')}) - result = df.iloc[5] - expected = Timestamp('2014-01-06 00:00:00+0000', tz='UTC', offset='D') - self.assertEqual(result, expected) - - result = df.loc[5] - self.assertEqual(result, expected) - - # indexing - boolean - result = df[df.a > df.a[3]] - expected = df.iloc[4:] - assert_frame_equal(result, expected) - def test_constructor_for_list_with_dtypes(self): intname = np.dtype(np.int_).name floatname = np.dtype(np.float_).name diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index 35467c6abb9b4..90f85b3f4576d 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -788,6 +788,58 @@ def test_loc_setitem_multiindex(self): result = df.loc[(t,n),'X'] self.assertEqual(result,3) + def test_indexing_with_datetime_tz(self): + + # 8260 + # support datetime64 with tz + + idx = Index(date_range('20130101',periods=3,tz='US/Eastern'), + name='foo') + dr = date_range('20130110',periods=3) + df = DataFrame({'A' : idx, 'B' : dr}) + df['C'] = idx + df.iloc[1,1] = pd.NaT + df.iloc[1,2] = pd.NaT + + # indexing + result = df.iloc[1] + expected = Series([Timestamp('2013-01-02 00:00:00-0500', tz='US/Eastern'), np.nan, np.nan], + index=list('ABC'), dtype='object', name=1) + assert_series_equal(result, expected) + result = df.loc[1] + expected = Series([Timestamp('2013-01-02 00:00:00-0500', tz='US/Eastern'), np.nan, np.nan], + index=list('ABC'), dtype='object', name=1) + assert_series_equal(result, expected) + + # indexing - fast_xs + df = DataFrame({'a': date_range('2014-01-01', periods=10, tz='UTC')}) + result = df.iloc[5] + expected = Timestamp('2014-01-06 00:00:00+0000', tz='UTC', offset='D') + self.assertEqual(result, expected) + + result = df.loc[5] + self.assertEqual(result, expected) + + # indexing - boolean + result = df[df.a > df.a[3]] + expected = df.iloc[4:] + assert_frame_equal(result, expected) + + # indexing - setting an element + df = DataFrame( data = pd.to_datetime(['2015-03-30 20:12:32','2015-03-12 00:11:11']) ,columns=['time'] ) + df['new_col']=['new','old'] + df.time=df.set_index('time').index.tz_localize('UTC') + v = df[df.new_col=='new'].set_index('time').index.tz_convert('US/Pacific') + + # trying to set a single element on a part of a different timezone + def f(): + df.loc[df.new_col=='new','time'] = v + self.assertRaises(ValueError, f) + + v = df.loc[df.new_col=='new','time'] + pd.Timedelta('1s') + df.loc[df.new_col=='new','time'] = v + assert_series_equal(df.loc[df.new_col=='new','time'],v) + def test_loc_setitem_dups(self): # GH 6541