Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5f51c80

Browse files
changhiskhanwesm
authored andcommittedDec 10, 2012
BUG: DatetimeIndex.append does not preserve timezone #2260
1 parent 0669f92 commit 5f51c80

File tree

4 files changed

+104
-33
lines changed

4 files changed

+104
-33
lines changed
 

‎pandas/io/tests/test_excel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ def _check_extension_indexlabels(self, ext):
490490
os.remove(path)
491491

492492
def test_excel_roundtrip_indexname(self):
493-
_skip_if_no_excelsuite()
493+
_skip_if_no_xlrd()
494+
_skip_if_no_xlwt()
494495

495496
path = '%s.xls' % tm.rands(10)
496497

@@ -809,4 +810,3 @@ def test_to_excel_header_styling_xlsx(self):
809810
if __name__ == '__main__':
810811
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
811812
exit=False)
812-

‎pandas/tseries/index.py

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -605,37 +605,6 @@ def summary(self, name=None):
605605

606606
return result
607607

608-
def append(self, other):
609-
"""
610-
Append a collection of Index options together
611-
612-
Parameters
613-
----------
614-
other : Index or list/tuple of indices
615-
616-
Returns
617-
-------
618-
appended : Index
619-
"""
620-
name = self.name
621-
to_concat = [self]
622-
623-
if isinstance(other, (list, tuple)):
624-
to_concat = to_concat + list(other)
625-
else:
626-
to_concat.append(other)
627-
628-
for obj in to_concat:
629-
if isinstance(obj, Index) and obj.name != name:
630-
name = None
631-
break
632-
633-
to_concat = self._ensure_compat_concat(to_concat)
634-
to_concat = [x.values if isinstance(x, Index) else x
635-
for x in to_concat]
636-
637-
return Index(com._concat_compat(to_concat), name=name)
638-
639608
def get_duplicates(self):
640609
values = Index.get_duplicates(self)
641610
return DatetimeIndex(values)
@@ -864,6 +833,36 @@ def union_many(self, others):
864833
this.offset = to_offset(this.inferred_freq)
865834
return this
866835

836+
def append(self, other):
837+
"""
838+
Append a collection of Index options together
839+
840+
Parameters
841+
----------
842+
other : Index or list/tuple of indices
843+
844+
Returns
845+
-------
846+
appended : Index
847+
"""
848+
name = self.name
849+
to_concat = [self]
850+
851+
if isinstance(other, (list, tuple)):
852+
to_concat = to_concat + list(other)
853+
else:
854+
to_concat.append(other)
855+
856+
for obj in to_concat:
857+
if isinstance(obj, Index) and obj.name != name:
858+
name = None
859+
break
860+
861+
to_concat = self._ensure_compat_concat(to_concat)
862+
to_concat, factory = _process_concat_data(to_concat, name)
863+
864+
return factory(com._concat_compat(to_concat))
865+
867866
def join(self, other, how='left', level=None, return_indexers=False):
868867
"""
869868
See Index.join
@@ -1633,3 +1632,33 @@ def _in_range(start, end, rng_start, rng_end):
16331632
def _time_to_micros(time):
16341633
seconds = time.hour * 60 * 60 + 60 * time.minute + time.second
16351634
return 1000000 * seconds + time.microsecond
1635+
1636+
def _process_concat_data(to_concat, name):
1637+
klass = Index
1638+
kwargs = {}
1639+
1640+
all_dti = True
1641+
need_utc_convert = False
1642+
tz = None
1643+
for x in to_concat:
1644+
if not isinstance(x, DatetimeIndex):
1645+
all_dti = False
1646+
else:
1647+
if tz is None:
1648+
tz = x.tz
1649+
elif x.tz != tz:
1650+
need_utc_convert = True
1651+
tz = 'UTC'
1652+
1653+
if need_utc_convert:
1654+
to_concat = [x.tz_convert('UTC') for x in to_concat]
1655+
1656+
if all_dti:
1657+
klass = DatetimeIndex
1658+
kwargs = {'tz' : tz}
1659+
1660+
to_concat = [x.values if isinstance(x, Index) else x
1661+
for x in to_concat]
1662+
1663+
factory_func = lambda x: klass(x, name=name, **kwargs)
1664+
return to_concat, factory_func

‎pandas/tseries/tests/test_timezones.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,35 @@ def test_align_aware(self):
668668
self.assertEqual(df1.index.tz, new1.index.tz)
669669
self.assertEqual(df2.index.tz, new2.index.tz)
670670

671+
def test_append_aware(self):
672+
rng1 = date_range('1/1/2011 01:00', periods=1, freq='H',
673+
tz='US/Eastern')
674+
rng2 = date_range('1/1/2011 02:00', periods=1, freq='H',
675+
tz='US/Eastern')
676+
ts1 = Series(np.random.randn(len(rng1)), index=rng1)
677+
ts2 = Series(np.random.randn(len(rng2)), index=rng2)
678+
ts_result = ts1.append(ts2)
679+
self.assertEqual(ts_result.index.tz, rng1.tz)
680+
681+
rng1 = date_range('1/1/2011 01:00', periods=1, freq='H',
682+
tz='UTC')
683+
rng2 = date_range('1/1/2011 02:00', periods=1, freq='H',
684+
tz='UTC')
685+
ts1 = Series(np.random.randn(len(rng1)), index=rng1)
686+
ts2 = Series(np.random.randn(len(rng2)), index=rng2)
687+
ts_result = ts1.append(ts2)
688+
utc = rng1.tz
689+
self.assertEqual(utc, ts_result.index.tz)
690+
691+
rng1 = date_range('1/1/2011 01:00', periods=1, freq='H',
692+
tz='US/Eastern')
693+
rng2 = date_range('1/1/2011 02:00', periods=1, freq='H',
694+
tz='US/Central')
695+
ts1 = Series(np.random.randn(len(rng1)), index=rng1)
696+
ts2 = Series(np.random.randn(len(rng2)), index=rng2)
697+
ts_result = ts1.append(ts2)
698+
self.assertEqual(utc, ts_result.index.tz)
699+
671700
def test_equal_join_ensure_utc(self):
672701
rng = date_range('1/1/2011', periods=10, freq='H', tz='US/Eastern')
673702
ts = Series(np.random.randn(len(rng)), index=rng)

‎vb_suite/timeseries.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,16 @@ def date_range(start=None, end=None, periods=None, freq=None):
175175
datetimeindex_normalize = \
176176
Benchmark('rng.normalize()', setup,
177177
start_date=datetime(2012, 9, 1))
178+
179+
setup = common_setup + """
180+
from pandas.tseries.offsets import Second
181+
s1 = date_range('1/1/2000', periods=100, freq='S')
182+
curr = s1[-1]
183+
slst = []
184+
for i in range(100):
185+
slst.append(curr + Second(), periods=100, freq='S')
186+
curr = slst[-1][-1]
187+
"""
188+
189+
dti_append_tz = \
190+
Benchmark('s1.append(slst)', setup, start_date=datetime(2012, 9 ,1))

0 commit comments

Comments
 (0)
Please sign in to comment.