Skip to content

Commit 092078e

Browse files
committed
Merge pull request #6419 from jreback/series_tz_fix
BUG: inconcistency in contained values of a Series created from a DatetimeIndex with a tz
2 parents 150f323 + ca1e7aa commit 092078e

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

pandas/tests/test_frame.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -2097,17 +2097,23 @@ def test_set_index_cast_datetimeindex(self):
20972097
i = pd.DatetimeIndex(pd.tseries.tools.to_datetime(['2013-1-1 13:00','2013-1-2 14:00'], errors="raise")).tz_localize('US/Pacific')
20982098
df = DataFrame(np.random.randn(2,1),columns=['A'])
20992099

2100-
expected = Series(i)
2101-
self.assertTrue(expected.dtype == object)
2102-
self.assertTrue(i.equals(expected.values.values))
2100+
expected = Series(np.array([pd.Timestamp('2013-01-01 13:00:00-0800', tz='US/Pacific'),
2101+
pd.Timestamp('2013-01-02 14:00:00-0800', tz='US/Pacific')], dtype="object"))
21032102

2103+
# convert index to series
2104+
result = Series(i)
2105+
assert_series_equal(result, expected)
2106+
2107+
# assignt to frame
21042108
df['B'] = i
21052109
result = df['B']
21062110
assert_series_equal(result, expected)
21072111

2112+
# keep the timezone
21082113
result = i.to_series(keep_tz=True)
21092114
assert_series_equal(result.reset_index(drop=True), expected)
21102115

2116+
# convert to utc
21112117
df['C'] = i.to_series().reset_index(drop=True)
21122118
result = df['C']
21132119
comp = DatetimeIndex(expected.values).copy()

pandas/tseries/index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ def to_series(self, keep_tz=False):
750750
def _to_embed(self, keep_tz=False):
751751
""" return an array repr of this object, potentially casting to object """
752752
if keep_tz and self.tz is not None and str(self.tz) != 'UTC':
753-
return self.asobject
753+
return self.asobject.values
754754
return self.values
755755

756756
@property

0 commit comments

Comments
 (0)