Skip to content

TST: Fixed timezone issues post DatetimeArray refactor #24634

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

Merged
merged 7 commits into from
Jan 6, 2019
Merged
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
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,7 @@ Timezones
- Bug in :func:`to_datetime` where ``utc=True`` was not respected when specifying a ``unit`` and ``errors='ignore'`` (:issue:`23758`)
- Bug in :func:`to_datetime` where ``utc=True`` was not respected when passing a :class:`Timestamp` (:issue:`24415`)
- Bug in :meth:`DataFrame.any` returns wrong value when ``axis=1`` and the data is of datetimelike type (:issue:`23070`)
- Bug in :meth:`DatetimeIndex.to_period` where a timezone aware index was converted to UTC first before creating :class:`PeriodIndex` (:issue:`22905`)

Offsets
^^^^^^^
Expand Down Expand Up @@ -1802,6 +1803,9 @@ Reshaping
- Constructing a DataFrame with an index argument that wasn't already an instance of :class:`~pandas.core.Index` was broken (:issue:`22227`).
- Bug in :class:`DataFrame` prevented list subclasses to be used to construction (:issue:`21226`)
- 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`)
- Bug in :func:`DataFrame.unstack` where a ``ValueError`` was raised when unstacking timezone aware values (:issue:`18338`)
- Bug in :func:`DataFrame.stack` where timezone aware values were converted to timezone naive values (:issue:`19420`)
- Bug in :func:`merge_asof` where a ``TypeError`` was raised when ``by_col`` were timezone aware values (:issue:`21184`)

.. _whatsnew_0240.bug_fixes.sparse:

Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/frame/test_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,3 +936,36 @@ def test_unstack_fill_frame_object():
index=list('xyz')
)
assert_frame_equal(result, expected)


def test_unstack_timezone_aware_values():
# GH 18338
df = pd.DataFrame({
'timestamp': [
pd.Timestamp('2017-08-27 01:00:00.709949+0000', tz='UTC')],
'a': ['a'],
'b': ['b'],
'c': ['c'],
}, columns=['timestamp', 'a', 'b', 'c'])
result = df.set_index(['a', 'b']).unstack()
expected = pd.DataFrame([[pd.Timestamp('2017-08-27 01:00:00.709949+0000',
tz='UTC'),
'c']],
index=pd.Index(['a'], name='a'),
columns=pd.MultiIndex(
levels=[['timestamp', 'c'], ['b']],
codes=[[0, 1], [0, 0]],
names=[None, 'b']))
assert_frame_equal(result, expected)


def test_stack_timezone_aware_values():
# GH 19420
ts = pd.date_range(freq="D", start="20180101", end="20180103",
tz="America/New_York")
df = pd.DataFrame({"A": ts}, index=["a", "b", "c"])
result = df.stack()
expected = pd.Series(ts,
index=pd.MultiIndex(levels=[['a', 'b', 'c'], ['A']],
codes=[[0, 1, 2], [0, 0, 0]]))
assert_series_equal(result, expected)
9 changes: 9 additions & 0 deletions pandas/tests/indexes/datetimes/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,15 @@ def test_to_period_tz(self, tz):

tm.assert_index_equal(result, expected)

@pytest.mark.parametrize('tz', ['Etc/GMT-1', 'Etc/GMT+1'])
def test_to_period_tz_utc_offset_consistency(self, tz):
# GH 22905
ts = pd.date_range('1/1/2000', '2/1/2000', tz='Etc/GMT-1')
with tm.assert_produces_warning(UserWarning):
result = ts.to_period()[0]
expected = ts[0].to_period()
assert result == expected

def test_to_period_nofreq(self):
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-04'])
with pytest.raises(ValueError):
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/reshape/merge/test_merge_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,3 +1022,17 @@ def test_merge_on_nans(self, func, side):
merge_asof(df_null, df, on='a')
else:
merge_asof(df, df_null, on='a')

def test_merge_by_col_tz_aware(self):
# GH 21184
left = pd.DataFrame(
{'by_col': pd.DatetimeIndex(['2018-01-01']).tz_localize('UTC'),
'on_col': [2], 'values': ['a']})
right = pd.DataFrame(
{'by_col': pd.DatetimeIndex(['2018-01-01']).tz_localize('UTC'),
'on_col': [1], 'values': ['b']})
result = pd.merge_asof(left, right, by='by_col', on='on_col')
expected = pd.DataFrame([
[pd.Timestamp('2018-01-01', tz='UTC'), 2, 'a', 'b']
], columns=['by_col', 'on_col', 'values_x', 'values_y'])
assert_frame_equal(result, expected)