Skip to content

Commit 393d0aa

Browse files
mroeschkejreback
authored andcommitted
TST: Fixed timezone issues post DatetimeArray refactor (#24634)
1 parent 1218de7 commit 393d0aa

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

doc/source/whatsnew/v0.24.0.rst

+4
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,7 @@ Timezones
15951595
- Bug in :func:`to_datetime` where ``utc=True`` was not respected when specifying a ``unit`` and ``errors='ignore'`` (:issue:`23758`)
15961596
- Bug in :func:`to_datetime` where ``utc=True`` was not respected when passing a :class:`Timestamp` (:issue:`24415`)
15971597
- Bug in :meth:`DataFrame.any` returns wrong value when ``axis=1`` and the data is of datetimelike type (:issue:`23070`)
1598+
- Bug in :meth:`DatetimeIndex.to_period` where a timezone aware index was converted to UTC first before creating :class:`PeriodIndex` (:issue:`22905`)
15981599

15991600
Offsets
16001601
^^^^^^^
@@ -1806,6 +1807,9 @@ Reshaping
18061807
- Constructing a DataFrame with an index argument that wasn't already an instance of :class:`~pandas.core.Index` was broken (:issue:`22227`).
18071808
- Bug in :class:`DataFrame` prevented list subclasses to be used to construction (:issue:`21226`)
18081809
- Bug in :func:`DataFrame.unstack` and :func:`DataFrame.pivot_table` returning a missleading error message when the resulting DataFrame has more elements than int32 can handle. Now, the error message is improved, pointing towards the actual problem (:issue:`20601`)
1810+
- Bug in :func:`DataFrame.unstack` where a ``ValueError`` was raised when unstacking timezone aware values (:issue:`18338`)
1811+
- Bug in :func:`DataFrame.stack` where timezone aware values were converted to timezone naive values (:issue:`19420`)
1812+
- Bug in :func:`merge_asof` where a ``TypeError`` was raised when ``by_col`` were timezone aware values (:issue:`21184`)
18091813

18101814
.. _whatsnew_0240.bug_fixes.sparse:
18111815

pandas/tests/frame/test_reshape.py

+33
Original file line numberDiff line numberDiff line change
@@ -936,3 +936,36 @@ def test_unstack_fill_frame_object():
936936
index=list('xyz')
937937
)
938938
assert_frame_equal(result, expected)
939+
940+
941+
def test_unstack_timezone_aware_values():
942+
# GH 18338
943+
df = pd.DataFrame({
944+
'timestamp': [
945+
pd.Timestamp('2017-08-27 01:00:00.709949+0000', tz='UTC')],
946+
'a': ['a'],
947+
'b': ['b'],
948+
'c': ['c'],
949+
}, columns=['timestamp', 'a', 'b', 'c'])
950+
result = df.set_index(['a', 'b']).unstack()
951+
expected = pd.DataFrame([[pd.Timestamp('2017-08-27 01:00:00.709949+0000',
952+
tz='UTC'),
953+
'c']],
954+
index=pd.Index(['a'], name='a'),
955+
columns=pd.MultiIndex(
956+
levels=[['timestamp', 'c'], ['b']],
957+
codes=[[0, 1], [0, 0]],
958+
names=[None, 'b']))
959+
assert_frame_equal(result, expected)
960+
961+
962+
def test_stack_timezone_aware_values():
963+
# GH 19420
964+
ts = pd.date_range(freq="D", start="20180101", end="20180103",
965+
tz="America/New_York")
966+
df = pd.DataFrame({"A": ts}, index=["a", "b", "c"])
967+
result = df.stack()
968+
expected = pd.Series(ts,
969+
index=pd.MultiIndex(levels=[['a', 'b', 'c'], ['A']],
970+
codes=[[0, 1, 2], [0, 0, 0]]))
971+
assert_series_equal(result, expected)

pandas/tests/indexes/datetimes/test_astype.py

+9
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,15 @@ def test_to_period_tz(self, tz):
293293

294294
tm.assert_index_equal(result, expected)
295295

296+
@pytest.mark.parametrize('tz', ['Etc/GMT-1', 'Etc/GMT+1'])
297+
def test_to_period_tz_utc_offset_consistency(self, tz):
298+
# GH 22905
299+
ts = pd.date_range('1/1/2000', '2/1/2000', tz='Etc/GMT-1')
300+
with tm.assert_produces_warning(UserWarning):
301+
result = ts.to_period()[0]
302+
expected = ts[0].to_period()
303+
assert result == expected
304+
296305
def test_to_period_nofreq(self):
297306
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-04'])
298307
with pytest.raises(ValueError):

pandas/tests/reshape/merge/test_merge_asof.py

+14
Original file line numberDiff line numberDiff line change
@@ -1022,3 +1022,17 @@ def test_merge_on_nans(self, func, side):
10221022
merge_asof(df_null, df, on='a')
10231023
else:
10241024
merge_asof(df, df_null, on='a')
1025+
1026+
def test_merge_by_col_tz_aware(self):
1027+
# GH 21184
1028+
left = pd.DataFrame(
1029+
{'by_col': pd.DatetimeIndex(['2018-01-01']).tz_localize('UTC'),
1030+
'on_col': [2], 'values': ['a']})
1031+
right = pd.DataFrame(
1032+
{'by_col': pd.DatetimeIndex(['2018-01-01']).tz_localize('UTC'),
1033+
'on_col': [1], 'values': ['b']})
1034+
result = pd.merge_asof(left, right, by='by_col', on='on_col')
1035+
expected = pd.DataFrame([
1036+
[pd.Timestamp('2018-01-01', tz='UTC'), 2, 'a', 'b']
1037+
], columns=['by_col', 'on_col', 'values_x', 'values_y'])
1038+
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)