Skip to content

BUG: Bug in DataFrame.set_index() with tz-aware Series #12365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ Bug Fixes
- Bug in ``DataFrame.info`` when duplicated column names exist (:issue:`11761`)
- Bug in ``.copy`` of datetime tz-aware objects (:issue:`11794`)
- Bug in ``Series.apply`` and ``Series.map`` where ``timedelta64`` was not boxed (:issue:`11349`)

- Bug in ``DataFrame.set_index()`` with tz-aware ``Series`` (:issue:`12358`)



Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2810,7 +2810,7 @@ def set_index(self, keys, drop=True, append=False, inplace=False,
level = col.get_level_values(col.nlevels - 1)
names.extend(col.names)
elif isinstance(col, Series):
level = col.values
level = col._values
names.append(col.name)
elif isinstance(col, Index):
level = col
Expand Down
12 changes: 11 additions & 1 deletion pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import numpy as np

from pandas.compat import lrange
from pandas import DataFrame, Series, Index, MultiIndex, RangeIndex
from pandas import (DataFrame, Series, Index, MultiIndex,
RangeIndex)
import pandas as pd

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

# GH 12358
# tz-aware Series should retain the tz
i = pd.to_datetime(["2014-01-01 10:10:10"],
utc=True).tz_convert('Europe/Rome')
df = DataFrame({'i': i})
self.assertEqual(df.set_index(i).index[0].hour, 11)
self.assertEqual(pd.DatetimeIndex(pd.Series(df.i))[0].hour, 11)
self.assertEqual(df.set_index(df.i).index[0].hour, 11)

def test_set_index_multiindexcolumns(self):
columns = MultiIndex.from_tuples([('foo', 1), ('foo', 2), ('bar', 1)])
df = DataFrame(np.random.randn(3, 3), columns=columns)
Expand Down