Skip to content

Commit 784ce1d

Browse files
committed
BUG: asobject if appending dti and non-dti or mixed naive and aware dti
1 parent aa8025f commit 784ce1d

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

pandas/tseries/index.py

+31-10
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ def append(self, other):
861861
to_concat = self._ensure_compat_concat(to_concat)
862862
to_concat, factory = _process_concat_data(to_concat, name)
863863

864-
return factory(com._concat_compat(to_concat))
864+
return factory(to_concat)
865865

866866
def join(self, other, how='left', level=None, return_indexers=False):
867867
"""
@@ -1636,29 +1636,50 @@ def _time_to_micros(time):
16361636
def _process_concat_data(to_concat, name):
16371637
klass = Index
16381638
kwargs = {}
1639+
concat = np.concatenate
16391640

16401641
all_dti = True
16411642
need_utc_convert = False
1643+
has_naive = False
16421644
tz = None
1645+
16431646
for x in to_concat:
16441647
if not isinstance(x, DatetimeIndex):
16451648
all_dti = False
16461649
else:
16471650
if tz is None:
16481651
tz = x.tz
1649-
elif x.tz != tz:
1652+
1653+
if x.tz is None:
1654+
has_naive = True
1655+
1656+
if x.tz != tz:
16501657
need_utc_convert = True
16511658
tz = 'UTC'
16521659

1653-
if need_utc_convert:
1654-
to_concat = [x.tz_convert('UTC') for x in to_concat]
1655-
16561660
if all_dti:
1657-
klass = DatetimeIndex
1658-
kwargs = {'tz' : tz}
1661+
need_obj_convert = False
1662+
if has_naive and tz is not None:
1663+
need_obj_convert = True
1664+
1665+
if need_obj_convert:
1666+
to_concat = [x.asobject.values for x in to_concat]
16591667

1660-
to_concat = [x.values if isinstance(x, Index) else x
1661-
for x in to_concat]
1668+
else:
1669+
if need_utc_convert:
1670+
to_concat = [x.tz_convert('UTC').values for x in to_concat]
1671+
else:
1672+
to_concat = [x.values for x in to_concat]
1673+
1674+
klass = DatetimeIndex
1675+
kwargs = {'tz' : tz}
1676+
concat = com._concat_compat
1677+
else:
1678+
for i, x in enumerate(to_concat):
1679+
if isinstance(x, DatetimeIndex):
1680+
to_concat[i] = x.asobject.values
1681+
elif isinstance(x, Index):
1682+
to_concat[i] = x.values
16621683

1663-
factory_func = lambda x: klass(x, name=name, **kwargs)
1684+
factory_func = lambda x: klass(concat(x), name=name, **kwargs)
16641685
return to_concat, factory_func

pandas/tseries/tests/test_timezones.py

+20
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,26 @@ def test_append_aware(self):
705705
ts_result = ts1.append(ts2)
706706
self.assertEqual(utc, ts_result.index.tz)
707707

708+
def test_append_aware_naive(self):
709+
rng1 = date_range('1/1/2011 01:00', periods=1, freq='H')
710+
rng2 = date_range('1/1/2011 02:00', periods=1, freq='H',
711+
tz='US/Eastern')
712+
ts1 = Series(np.random.randn(len(rng1)), index=rng1)
713+
ts2 = Series(np.random.randn(len(rng2)), index=rng2)
714+
ts_result = ts1.append(ts2)
715+
self.assert_(ts_result.index.equals(
716+
ts1.index.asobject.append(ts2.index.asobject)))
717+
718+
#mixed
719+
720+
rng1 = date_range('1/1/2011 01:00', periods=1, freq='H')
721+
rng2 = range(100)
722+
ts1 = Series(np.random.randn(len(rng1)), index=rng1)
723+
ts2 = Series(np.random.randn(len(rng2)), index=rng2)
724+
ts_result = ts1.append(ts2)
725+
self.assert_(ts_result.index.equals(
726+
ts1.index.asobject.append(ts2.index)))
727+
708728
def test_equal_join_ensure_utc(self):
709729
rng = date_range('1/1/2011', periods=10, freq='H', tz='US/Eastern')
710730
ts = Series(np.random.randn(len(rng)), index=rng)

0 commit comments

Comments
 (0)