diff --git a/pandas/tests/frame/methods/test_tz_convert.py b/pandas/tests/frame/methods/test_tz_convert.py index d2ab7a386a92d..c70e479723644 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,19 @@ 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 + obj = klass( + np.arange(0, 5), + index=date_range("20131027", periods=5, freq="1H", tz="Europe/Berlin"), + ) + 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) + 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 1d4e26a6999b7..183b81ca5298e 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,21 @@ 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 + obj = klass( + np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz=None) + ) + 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) + tm.assert_equal(obj, orig) + assert result.index is not obj.index + assert result is not obj 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 05792dc4f00d2..0000000000000 --- a/pandas/tests/series/test_timezones.py +++ /dev/null @@ -1,43 +0,0 @@ -""" -Tests for Series timezone-related methods -""" -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: - 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]) - - @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)