Skip to content

Commit 69baf4c

Browse files
committed
BUG: Bug in DataFrame.set_index() with tz-aware Series
closes #12358 Author: Jeff Reback <[email protected]> Closes #12365 from jreback/set_index_tz and squashes the following commits: 46d5c9d [Jeff Reback] BUG: Bug in DataFrame.set_index() with tz-aware Series
1 parent f1aad46 commit 69baf4c

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

doc/source/whatsnew/v0.18.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ Bug Fixes
10451045
- Bug in ``DataFrame.info`` when duplicated column names exist (:issue:`11761`)
10461046
- Bug in ``.copy`` of datetime tz-aware objects (:issue:`11794`)
10471047
- Bug in ``Series.apply`` and ``Series.map`` where ``timedelta64`` was not boxed (:issue:`11349`)
1048-
1048+
- Bug in ``DataFrame.set_index()`` with tz-aware ``Series`` (:issue:`12358`)
10491049

10501050

10511051

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2810,7 +2810,7 @@ def set_index(self, keys, drop=True, append=False, inplace=False,
28102810
level = col.get_level_values(col.nlevels - 1)
28112811
names.extend(col.names)
28122812
elif isinstance(col, Series):
2813-
level = col.values
2813+
level = col._values
28142814
names.append(col.name)
28152815
elif isinstance(col, Index):
28162816
level = col

pandas/tests/frame/test_alter_axes.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import numpy as np
88

99
from pandas.compat import lrange
10-
from pandas import DataFrame, Series, Index, MultiIndex, RangeIndex
10+
from pandas import (DataFrame, Series, Index, MultiIndex,
11+
RangeIndex)
1112
import pandas as pd
1213

1314
from pandas.util.testing import (assert_series_equal,
@@ -267,6 +268,15 @@ def test_set_index_cast_datetimeindex(self):
267268
lambda d: pd.Timestamp(d, tz=tz))
268269
assert_frame_equal(df.reset_index(), expected)
269270

271+
# GH 12358
272+
# tz-aware Series should retain the tz
273+
i = pd.to_datetime(["2014-01-01 10:10:10"],
274+
utc=True).tz_convert('Europe/Rome')
275+
df = DataFrame({'i': i})
276+
self.assertEqual(df.set_index(i).index[0].hour, 11)
277+
self.assertEqual(pd.DatetimeIndex(pd.Series(df.i))[0].hour, 11)
278+
self.assertEqual(df.set_index(df.i).index[0].hour, 11)
279+
270280
def test_set_index_multiindexcolumns(self):
271281
columns = MultiIndex.from_tuples([('foo', 1), ('foo', 2), ('bar', 1)])
272282
df = DataFrame(np.random.randn(3, 3), columns=columns)

0 commit comments

Comments
 (0)