Skip to content

Commit 0850fbd

Browse files
mroeschkejreback
authored andcommitted
CLN: Old timezone issues (#22201)
1 parent 486e626 commit 0850fbd

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

doc/source/whatsnew/v0.24.0.txt

+4
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,10 @@ Timezones
561561
- Fixed bug where :meth:`DataFrame.describe` and :meth:`Series.describe` on tz-aware datetimes did not show `first` and `last` result (:issue:`21328`)
562562
- Bug in :class:`DatetimeIndex` comparisons failing to raise ``TypeError`` when comparing timezone-aware ``DatetimeIndex`` against ``np.datetime64`` (:issue:`22074`)
563563
- Bug in ``DataFrame`` assignment with a timezone-aware scalar (:issue:`19843`)
564+
- Bug when constructing a :class:`DatetimeIndex` with :class:`Timestamp`s constructed with the ``replace`` method across DST (:issue:`18785`)
565+
- Bug when setting a new value with :meth:`DataFrame.loc` with a :class:`DatetimeIndex` with a DST transition (:issue:`18308`, :issue:`20724`)
566+
- Bug in :meth:`DatetimeIndex.unique` that did not re-localize tz-aware dates correctly (:issue:`21737`)
567+
- Bug when indexing a :class:`Series` with a DST transition (:issue:`21846`)
564568

565569
Offsets
566570
^^^^^^^

pandas/tests/indexes/datetimes/test_construction.py

+18
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,24 @@ def test_construction_int_rountrip(self, tz_naive_fixture):
503503
expected = DatetimeIndex([1293858000000000000], tz=tz).asi8[0]
504504
assert result == expected
505505

506+
def test_construction_from_replaced_timestamps_with_dst(self):
507+
# GH 18785
508+
index = pd.date_range(pd.Timestamp(2000, 1, 1),
509+
pd.Timestamp(2005, 1, 1),
510+
freq='MS', tz='Australia/Melbourne')
511+
test = pd.DataFrame({'data': range(len(index))}, index=index)
512+
test = test.resample('Y').mean()
513+
result = pd.DatetimeIndex([x.replace(month=6, day=1)
514+
for x in test.index])
515+
expected = pd.DatetimeIndex(['2000-06-01 00:00:00',
516+
'2001-06-01 00:00:00',
517+
'2002-06-01 00:00:00',
518+
'2003-06-01 00:00:00',
519+
'2004-06-01 00:00:00',
520+
'2005-06-01 00:00:00'],
521+
tz='Australia/Melbourne')
522+
tm.assert_index_equal(result, expected)
523+
506524

507525
class TestTimeSeries(object):
508526

pandas/tests/indexes/datetimes/test_datetime.py

+3
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,6 @@ def test_factorize_dst(self):
394394
def test_unique(self, arr, expected):
395395
result = arr.unique()
396396
tm.assert_index_equal(result, expected)
397+
# GH 21737
398+
# Ensure the underlying data is consistent
399+
assert result[0] == expected[0]

pandas/tests/indexing/test_datetime.py

+47
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
from datetime import datetime, timedelta
2+
13
import numpy as np
24
import pandas as pd
5+
from dateutil import tz
6+
37
from pandas import date_range, Index, DataFrame, Series, Timestamp
48
from pandas.util import testing as tm
59

@@ -266,3 +270,46 @@ def test_nanosecond_getitem_setitem_with_tz(self):
266270
result.loc[df.index[0], 'a'] = -1
267271
expected = DataFrame(-1, index=index, columns=['a'])
268272
tm.assert_frame_equal(result, expected)
273+
274+
def test_loc_getitem_across_dst(self):
275+
# GH 21846
276+
idx = pd.date_range('2017-10-29 01:30:00',
277+
tz='Europe/Berlin', periods=5, freq='30 min')
278+
series2 = pd.Series([0, 1, 2, 3, 4],
279+
index=idx)
280+
281+
t_1 = pd.Timestamp('2017-10-29 02:30:00+02:00', tz='Europe/Berlin',
282+
freq='30min')
283+
t_2 = pd.Timestamp('2017-10-29 02:00:00+01:00', tz='Europe/Berlin',
284+
freq='30min')
285+
result = series2.loc[t_1:t_2]
286+
expected = pd.Series([2, 3], index=idx[2:4])
287+
tm.assert_series_equal(result, expected)
288+
289+
result = series2[t_1]
290+
expected = 2
291+
assert result == expected
292+
293+
def test_loc_incremental_setitem_with_dst(self):
294+
# GH 20724
295+
base = datetime(2015, 11, 1, tzinfo=tz.gettz("US/Pacific"))
296+
idxs = [base + timedelta(seconds=i * 900) for i in range(16)]
297+
result = pd.Series([0], index=[idxs[0]])
298+
for ts in idxs:
299+
result.loc[ts] = 1
300+
expected = pd.Series(1, index=idxs)
301+
tm.assert_series_equal(result, expected)
302+
303+
def test_loc_setitem_with_existing_dst(self):
304+
# GH 18308
305+
start = pd.Timestamp('2017-10-29 00:00:00+0200', tz='Europe/Madrid')
306+
end = pd.Timestamp('2017-10-29 03:00:00+0100', tz='Europe/Madrid')
307+
ts = pd.Timestamp('2016-10-10 03:00:00', tz='Europe/Madrid')
308+
idx = pd.date_range(start, end, closed='left', freq="H")
309+
result = pd.DataFrame(index=idx, columns=['value'])
310+
result.loc[ts, 'value'] = 12
311+
expected = pd.DataFrame([np.nan] * len(idx) + [12],
312+
index=idx.append(pd.DatetimeIndex([ts])),
313+
columns=['value'],
314+
dtype=object)
315+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)