Skip to content

Commit c832f37

Browse files
jbrockmendelluckyvs1
authored andcommitted
TST: share tz_convert/tz_localize tests (pandas-dev#38756)
1 parent b20b009 commit c832f37

File tree

4 files changed

+61
-44
lines changed

4 files changed

+61
-44
lines changed

pandas/tests/frame/methods/test_tz_convert.py

+31-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,47 @@
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
class TestTZConvert:
9-
def test_frame_tz_convert(self):
9+
def test_tz_convert(self, frame_or_series):
1010
rng = date_range("1/1/2011", periods=200, freq="D", tz="US/Eastern")
1111

12-
df = DataFrame({"a": 1}, index=rng)
13-
result = df.tz_convert("Europe/Berlin")
12+
obj = DataFrame({"a": 1}, index=rng)
13+
if frame_or_series is not DataFrame:
14+
obj = obj["a"]
15+
16+
result = obj.tz_convert("Europe/Berlin")
1417
expected = DataFrame({"a": 1}, rng.tz_convert("Europe/Berlin"))
18+
if frame_or_series is not DataFrame:
19+
expected = expected["a"]
20+
1521
assert result.index.tz.zone == "Europe/Berlin"
16-
tm.assert_frame_equal(result, expected)
22+
tm.assert_equal(result, expected)
23+
24+
def test_tz_convert_axis1(self):
25+
rng = date_range("1/1/2011", periods=200, freq="D", tz="US/Eastern")
26+
27+
obj = DataFrame({"a": 1}, index=rng)
1728

18-
df = df.T
19-
result = df.tz_convert("Europe/Berlin", axis=1)
29+
obj = obj.T
30+
result = obj.tz_convert("Europe/Berlin", axis=1)
2031
assert result.columns.tz.zone == "Europe/Berlin"
21-
tm.assert_frame_equal(result, expected.T)
32+
33+
expected = DataFrame({"a": 1}, rng.tz_convert("Europe/Berlin"))
34+
35+
tm.assert_equal(result, expected.T)
36+
37+
def test_tz_convert_naive(self, frame_or_series):
38+
# can't convert tz-naive
39+
rng = date_range("1/1/2011", periods=200, freq="D")
40+
ts = Series(1, index=rng)
41+
ts = frame_or_series(ts)
42+
43+
with pytest.raises(TypeError, match="Cannot convert tz-naive"):
44+
ts.tz_convert("US/Eastern")
2245

2346
@pytest.mark.parametrize("fn", ["tz_localize", "tz_convert"])
2447
def test_tz_convert_and_localize(self, fn):

pandas/tests/frame/methods/test_tz_localize.py

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,52 @@
11
import numpy as np
22
import pytest
33

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

77

88
class TestTZLocalize:
99
# See also:
1010
# test_tz_convert_and_localize in test_tz_convert
1111

12-
def test_frame_tz_localize(self):
12+
def test_tz_localize(self, frame_or_series):
1313
rng = date_range("1/1/2011", periods=100, freq="H")
1414

15-
df = DataFrame({"a": 1}, index=rng)
16-
result = df.tz_localize("utc")
15+
obj = DataFrame({"a": 1}, index=rng)
16+
if frame_or_series is not DataFrame:
17+
obj = obj["a"]
18+
19+
result = obj.tz_localize("utc")
1720
expected = DataFrame({"a": 1}, rng.tz_localize("UTC"))
21+
if frame_or_series is not DataFrame:
22+
expected = expected["a"]
23+
1824
assert result.index.tz.zone == "UTC"
19-
tm.assert_frame_equal(result, expected)
25+
tm.assert_equal(result, expected)
26+
27+
def test_tz_localize_axis1(self):
28+
rng = date_range("1/1/2011", periods=100, freq="H")
29+
30+
df = DataFrame({"a": 1}, index=rng)
2031

2132
df = df.T
2233
result = df.tz_localize("utc", axis=1)
2334
assert result.columns.tz.zone == "UTC"
35+
36+
expected = DataFrame({"a": 1}, rng.tz_localize("UTC"))
37+
2438
tm.assert_frame_equal(result, expected.T)
2539

40+
def test_tz_localize_naive(self, frame_or_series):
41+
42+
# Can't localize if already tz-aware
43+
rng = date_range("1/1/2011", periods=100, freq="H", tz="utc")
44+
ts = Series(1, index=rng)
45+
ts = frame_or_series(ts)
46+
47+
with pytest.raises(TypeError, match="Already tz-aware"):
48+
ts.tz_localize("US/Eastern")
49+
2650
@pytest.mark.parametrize("copy", [True, False])
2751
def test_tz_localize_copy_inplace_mutate(self, copy, frame_or_series):
2852
# GH#6326

pandas/tests/series/methods/test_tz_convert.py

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
11
import numpy as np
2-
import pytest
32

4-
from pandas import DatetimeIndex, Series, date_range
3+
from pandas import DatetimeIndex, Series
54
import pandas._testing as tm
65

76

87
class TestTZConvert:
9-
def test_series_tz_convert(self):
10-
rng = date_range("1/1/2011", periods=200, freq="D", tz="US/Eastern")
11-
ts = Series(1, index=rng)
12-
13-
result = ts.tz_convert("Europe/Berlin")
14-
assert result.index.tz.zone == "Europe/Berlin"
15-
16-
# can't convert tz-naive
17-
rng = date_range("1/1/2011", periods=200, freq="D")
18-
ts = Series(1, index=rng)
19-
20-
with pytest.raises(TypeError, match="Cannot convert tz-naive"):
21-
ts.tz_convert("US/Eastern")
22-
238
def test_series_tz_convert_to_utc(self):
249
base = DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03"], tz="UTC")
2510
idx1 = base.tz_convert("Asia/Tokyo")[:2]

pandas/tests/series/methods/test_tz_localize.py

-15
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,6 @@
88

99

1010
class TestTZLocalize:
11-
def test_series_tz_localize(self):
12-
13-
rng = date_range("1/1/2011", periods=100, freq="H")
14-
ts = Series(1, index=rng)
15-
16-
result = ts.tz_localize("utc")
17-
assert result.index.tz.zone == "UTC"
18-
19-
# Can't localize if already tz-aware
20-
rng = date_range("1/1/2011", periods=100, freq="H", tz="utc")
21-
ts = Series(1, index=rng)
22-
23-
with pytest.raises(TypeError, match="Already tz-aware"):
24-
ts.tz_localize("US/Eastern")
25-
2611
def test_series_tz_localize_ambiguous_bool(self):
2712
# make sure that we are correctly accepting bool values as ambiguous
2813

0 commit comments

Comments
 (0)