From 7b9f34aec6389310b32c2386436f04aadd10269d Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 29 Oct 2020 13:50:00 -0700 Subject: [PATCH 1/3] TST: fix tzlocalize/tz_convert test asserting wrong thing --- pandas/tests/frame/methods/test_tz_convert.py | 16 +++++++++++++- .../tests/frame/methods/test_tz_localize.py | 21 ++++++++++++++++++- pandas/tests/series/test_timezones.py | 19 ----------------- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/pandas/tests/frame/methods/test_tz_convert.py b/pandas/tests/frame/methods/test_tz_convert.py index d2ab7a386a92d..3ea28d088d473 100644 --- a/pandas/tests/frame/methods/test_tz_convert.py +++ b/pandas/tests/frame/methods/test_tz_convert.py @@ -1,7 +1,7 @@ import numpy as np import pytest -from pandas import DataFrame, Index, MultiIndex, date_range +from pandas import DataFrame, Index, MultiIndex, Series, date_range import pandas._testing as tm @@ -88,3 +88,17 @@ def test_tz_convert_and_localize(self, fn): with pytest.raises(ValueError, match="not valid"): df = DataFrame(index=l0) df = getattr(df, fn)("US/Pacific", level=1) + + @pytest.mark.parametrize("klass", [Series, DataFrame]) + @pytest.mark.parametrize("copy", [True, False]) + def test_tz_convert_copy_inplace_mutate(self, copy, klass): + # GH#6326 + ser = klass( + np.arange(0, 5), + index=date_range("20131027", periods=5, freq="1H", tz="Europe/Berlin"), + ) + result = ser.tz_convert("UTC", copy=copy) + expected = klass(np.arange(0, 5), index=ser.index.tz_convert("UTC")) + tm.assert_equal(result, expected) + assert result.index is not ser.index + assert result is not ser diff --git a/pandas/tests/frame/methods/test_tz_localize.py b/pandas/tests/frame/methods/test_tz_localize.py index 1d4e26a6999b7..20d38bf80f223 100644 --- a/pandas/tests/frame/methods/test_tz_localize.py +++ b/pandas/tests/frame/methods/test_tz_localize.py @@ -1,4 +1,7 @@ -from pandas import DataFrame, date_range +import numpy as np +import pytest + +from pandas import DataFrame, Series, date_range import pandas._testing as tm @@ -19,3 +22,19 @@ def test_frame_tz_localize(self): result = df.tz_localize("utc", axis=1) assert result.columns.tz.zone == "UTC" tm.assert_frame_equal(result, expected.T) + + @pytest.mark.parametrize("klass", [Series, DataFrame]) + @pytest.mark.parametrize("copy", [True, False]) + def test_tz_localize_copy_inplace_mutate(self, copy, klass): + # GH#6326 + ser = klass( + np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz=None) + ) + result = ser.tz_localize("UTC", copy=copy) + expected = klass( + np.arange(0, 5), + index=date_range("20131027", periods=5, freq="1H", tz="UTC"), + ) + tm.assert_equal(result, expected) + assert result.index is not ser.index + assert result is not ser diff --git a/pandas/tests/series/test_timezones.py b/pandas/tests/series/test_timezones.py index 05792dc4f00d2..c78b4fddf7298 100644 --- a/pandas/tests/series/test_timezones.py +++ b/pandas/tests/series/test_timezones.py @@ -4,12 +4,8 @@ from datetime import datetime from dateutil.tz import tzoffset -import numpy as np -import pytest from pandas import Series -import pandas._testing as tm -from pandas.core.indexes.datetimes import date_range class TestSeriesTimezones: @@ -26,18 +22,3 @@ def test_dateutil_tzoffset_support(self): # it works! #2443 repr(series.index[0]) - - @pytest.mark.parametrize("copy", [True, False]) - @pytest.mark.parametrize( - "method, tz", [["tz_localize", None], ["tz_convert", "Europe/Berlin"]] - ) - def test_tz_localize_convert_copy_inplace_mutate(self, copy, method, tz): - # GH 6326 - result = Series( - np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz=tz) - ) - getattr(result, method)("UTC", copy=copy) - expected = Series( - np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz=tz) - ) - tm.assert_series_equal(result, expected) From b6796ca8db748d016cff5f4fe52659c7b00a06cd Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 29 Oct 2020 14:18:22 -0700 Subject: [PATCH 2/3] TST: move test to test_constructors --- pandas/tests/series/test_constructors.py | 15 +++++++++++++++ pandas/tests/series/test_timezones.py | 24 ------------------------ 2 files changed, 15 insertions(+), 24 deletions(-) delete mode 100644 pandas/tests/series/test_timezones.py diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index a2fab5c7e0f0e..bb091ba1beb2d 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1,6 +1,7 @@ from collections import OrderedDict from datetime import datetime, timedelta +from dateutil.tz import tzoffset import numpy as np import numpy.ma as ma import pytest @@ -1588,6 +1589,20 @@ def test_constructor_dict_timedelta_index(self): ) tm.assert_series_equal(result, expected) + def test_constructor_infer_index_tz(self): + values = [188.5, 328.25] + tzinfo = tzoffset(None, 7200) + index = [ + datetime(2012, 5, 11, 11, tzinfo=tzinfo), + datetime(2012, 5, 11, 12, tzinfo=tzinfo), + ] + series = Series(data=values, index=index) + + assert series.index.tz == tzinfo + + # it works! GH#2443 + repr(series.index[0]) + class TestSeriesConstructorIndexCoercion: def test_series_constructor_datetimelike_index_coercion(self): diff --git a/pandas/tests/series/test_timezones.py b/pandas/tests/series/test_timezones.py deleted file mode 100644 index c78b4fddf7298..0000000000000 --- a/pandas/tests/series/test_timezones.py +++ /dev/null @@ -1,24 +0,0 @@ -""" -Tests for Series timezone-related methods -""" -from datetime import datetime - -from dateutil.tz import tzoffset - -from pandas import Series - - -class TestSeriesTimezones: - def test_dateutil_tzoffset_support(self): - values = [188.5, 328.25] - tzinfo = tzoffset(None, 7200) - index = [ - datetime(2012, 5, 11, 11, tzinfo=tzinfo), - datetime(2012, 5, 11, 12, tzinfo=tzinfo), - ] - series = Series(data=values, index=index) - - assert series.index.tz == tzinfo - - # it works! #2443 - repr(series.index[0]) From a0f45270804e29ab24dcd80f3b80845efe996a7b Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 29 Oct 2020 16:37:52 -0700 Subject: [PATCH 3/3] update assertions --- pandas/tests/frame/methods/test_tz_convert.py | 12 +++++++----- pandas/tests/frame/methods/test_tz_localize.py | 10 ++++++---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pandas/tests/frame/methods/test_tz_convert.py b/pandas/tests/frame/methods/test_tz_convert.py index 3ea28d088d473..c70e479723644 100644 --- a/pandas/tests/frame/methods/test_tz_convert.py +++ b/pandas/tests/frame/methods/test_tz_convert.py @@ -93,12 +93,14 @@ def test_tz_convert_and_localize(self, fn): @pytest.mark.parametrize("copy", [True, False]) def test_tz_convert_copy_inplace_mutate(self, copy, klass): # GH#6326 - ser = klass( + obj = klass( np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz="Europe/Berlin"), ) - result = ser.tz_convert("UTC", copy=copy) - expected = klass(np.arange(0, 5), index=ser.index.tz_convert("UTC")) + orig = obj.copy() + result = obj.tz_convert("UTC", copy=copy) + expected = klass(np.arange(0, 5), index=obj.index.tz_convert("UTC")) tm.assert_equal(result, expected) - assert result.index is not ser.index - assert result is not ser + tm.assert_equal(obj, orig) + assert result.index is not obj.index + assert result is not obj diff --git a/pandas/tests/frame/methods/test_tz_localize.py b/pandas/tests/frame/methods/test_tz_localize.py index 20d38bf80f223..183b81ca5298e 100644 --- a/pandas/tests/frame/methods/test_tz_localize.py +++ b/pandas/tests/frame/methods/test_tz_localize.py @@ -27,14 +27,16 @@ def test_frame_tz_localize(self): @pytest.mark.parametrize("copy", [True, False]) def test_tz_localize_copy_inplace_mutate(self, copy, klass): # GH#6326 - ser = klass( + obj = klass( np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz=None) ) - result = ser.tz_localize("UTC", copy=copy) + orig = obj.copy() + result = obj.tz_localize("UTC", copy=copy) expected = klass( np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz="UTC"), ) tm.assert_equal(result, expected) - assert result.index is not ser.index - assert result is not ser + tm.assert_equal(obj, orig) + assert result.index is not obj.index + assert result is not obj