Skip to content

Commit cc216ad

Browse files
sinhrksjreback
authored andcommitted
TST: AmbiguousTimeError with set_index() (pandas-dev#13814)
1 parent 31f8e4d commit cc216ad

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

doc/source/whatsnew/v0.19.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,8 @@ Bug Fixes
756756
- Bug in ``.unstack`` with ``Categorical`` dtype resets ``.ordered`` to ``True`` (:issue:`13249`)
757757
- Clean some compile time warnings in datetime parsing (:issue:`13607`)
758758
- Bug in ``factorize`` raises ``AmbiguousTimeError`` if data contains datetime near DST boundary (:issue:`13750`)
759+
- Bug in ``.set_index`` raises ``AmbiguousTimeError`` if new index contains DST boundary and multi levels (:issue:`12920`)
760+
759761

760762
- Bug in ``Series`` comparison operators when dealing with zero dim NumPy arrays (:issue:`13006`)
761763
- Bug in ``groupby`` where ``apply`` returns different result depending on whether first result is ``None`` or not (:issue:`12824`)

pandas/tests/frame/test_alter_axes.py

+20
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ def test_set_index_cast_datetimeindex(self):
268268
lambda d: pd.Timestamp(d, tz=tz))
269269
assert_frame_equal(df.reset_index(), expected)
270270

271+
def test_set_index_timezone(self):
271272
# GH 12358
272273
# tz-aware Series should retain the tz
273274
i = pd.to_datetime(["2014-01-01 10:10:10"],
@@ -277,6 +278,25 @@ def test_set_index_cast_datetimeindex(self):
277278
self.assertEqual(pd.DatetimeIndex(pd.Series(df.i))[0].hour, 11)
278279
self.assertEqual(df.set_index(df.i).index[0].hour, 11)
279280

281+
def test_set_index_dst(self):
282+
di = pd.date_range('2006-10-29 00:00:00', periods=3,
283+
req='H', tz='US/Pacific')
284+
285+
df = pd.DataFrame(data={'a': [0, 1, 2], 'b': [3, 4, 5]},
286+
index=di).reset_index()
287+
# single level
288+
res = df.set_index('index')
289+
exp = pd.DataFrame(data={'a': [0, 1, 2], 'b': [3, 4, 5]},
290+
index=pd.Index(di, name='index'))
291+
tm.assert_frame_equal(res, exp)
292+
293+
# GH 12920
294+
res = df.set_index(['index', 'a'])
295+
exp_index = pd.MultiIndex.from_arrays([di, [0, 1, 2]],
296+
names=['index', 'a'])
297+
exp = pd.DataFrame({'b': [3, 4, 5]}, index=exp_index)
298+
tm.assert_frame_equal(res, exp)
299+
280300
def test_set_index_multiindexcolumns(self):
281301
columns = MultiIndex.from_tuples([('foo', 1), ('foo', 2), ('bar', 1)])
282302
df = DataFrame(np.random.randn(3, 3), columns=columns)

0 commit comments

Comments
 (0)