Skip to content

Commit ce1e8d6

Browse files
authored
TST: tz_localize/tz_convert unclear test assertions (#37498)
1 parent f6275ba commit ce1e8d6

File tree

4 files changed

+54
-45
lines changed

4 files changed

+54
-45
lines changed

pandas/tests/frame/methods/test_tz_convert.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from pandas import DataFrame, Index, MultiIndex, date_range
4+
from pandas import DataFrame, Index, MultiIndex, Series, date_range
55
import pandas._testing as tm
66

77

@@ -88,3 +88,19 @@ def test_tz_convert_and_localize(self, fn):
8888
with pytest.raises(ValueError, match="not valid"):
8989
df = DataFrame(index=l0)
9090
df = getattr(df, fn)("US/Pacific", level=1)
91+
92+
@pytest.mark.parametrize("klass", [Series, DataFrame])
93+
@pytest.mark.parametrize("copy", [True, False])
94+
def test_tz_convert_copy_inplace_mutate(self, copy, klass):
95+
# GH#6326
96+
obj = klass(
97+
np.arange(0, 5),
98+
index=date_range("20131027", periods=5, freq="1H", tz="Europe/Berlin"),
99+
)
100+
orig = obj.copy()
101+
result = obj.tz_convert("UTC", copy=copy)
102+
expected = klass(np.arange(0, 5), index=obj.index.tz_convert("UTC"))
103+
tm.assert_equal(result, expected)
104+
tm.assert_equal(obj, orig)
105+
assert result.index is not obj.index
106+
assert result is not obj

pandas/tests/frame/methods/test_tz_localize.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from pandas import DataFrame, date_range
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import DataFrame, Series, date_range
25
import pandas._testing as tm
36

47

@@ -19,3 +22,21 @@ def test_frame_tz_localize(self):
1922
result = df.tz_localize("utc", axis=1)
2023
assert result.columns.tz.zone == "UTC"
2124
tm.assert_frame_equal(result, expected.T)
25+
26+
@pytest.mark.parametrize("klass", [Series, DataFrame])
27+
@pytest.mark.parametrize("copy", [True, False])
28+
def test_tz_localize_copy_inplace_mutate(self, copy, klass):
29+
# GH#6326
30+
obj = klass(
31+
np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz=None)
32+
)
33+
orig = obj.copy()
34+
result = obj.tz_localize("UTC", copy=copy)
35+
expected = klass(
36+
np.arange(0, 5),
37+
index=date_range("20131027", periods=5, freq="1H", tz="UTC"),
38+
)
39+
tm.assert_equal(result, expected)
40+
tm.assert_equal(obj, orig)
41+
assert result.index is not obj.index
42+
assert result is not obj

pandas/tests/series/test_constructors.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import OrderedDict
22
from datetime import datetime, timedelta
33

4+
from dateutil.tz import tzoffset
45
import numpy as np
56
import numpy.ma as ma
67
import pytest
@@ -1588,6 +1589,20 @@ def test_constructor_dict_timedelta_index(self):
15881589
)
15891590
tm.assert_series_equal(result, expected)
15901591

1592+
def test_constructor_infer_index_tz(self):
1593+
values = [188.5, 328.25]
1594+
tzinfo = tzoffset(None, 7200)
1595+
index = [
1596+
datetime(2012, 5, 11, 11, tzinfo=tzinfo),
1597+
datetime(2012, 5, 11, 12, tzinfo=tzinfo),
1598+
]
1599+
series = Series(data=values, index=index)
1600+
1601+
assert series.index.tz == tzinfo
1602+
1603+
# it works! GH#2443
1604+
repr(series.index[0])
1605+
15911606

15921607
class TestSeriesConstructorIndexCoercion:
15931608
def test_series_constructor_datetimelike_index_coercion(self):

pandas/tests/series/test_timezones.py

-43
This file was deleted.

0 commit comments

Comments
 (0)