Skip to content

Commit 736761f

Browse files
authored
TST: misplaced arithmetic tests (#32275)
1 parent 922f932 commit 736761f

File tree

6 files changed

+99
-84
lines changed

6 files changed

+99
-84
lines changed

pandas/tests/frame/test_arithmetic.py

+33
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import numpy as np
66
import pytest
7+
import pytz
78

89
import pandas as pd
910
import pandas._testing as tm
@@ -771,3 +772,35 @@ def test_frame_single_columns_object_sum_axis_1():
771772
result = df.sum(axis=1)
772773
expected = pd.Series(["A", 1.2, 0])
773774
tm.assert_series_equal(result, expected)
775+
776+
777+
# -------------------------------------------------------------------
778+
# Unsorted
779+
# These arithmetic tests were previously in other files, eventually
780+
# should be parametrized and put into tests.arithmetic
781+
782+
783+
class TestFrameArithmeticUnsorted:
784+
def test_frame_add_tz_mismatch_converts_to_utc(self):
785+
rng = pd.date_range("1/1/2011", periods=10, freq="H", tz="US/Eastern")
786+
df = pd.DataFrame(np.random.randn(len(rng)), index=rng, columns=["a"])
787+
788+
df_moscow = df.tz_convert("Europe/Moscow")
789+
result = df + df_moscow
790+
assert result.index.tz is pytz.utc
791+
792+
result = df_moscow + df
793+
assert result.index.tz is pytz.utc
794+
795+
def test_align_frame(self):
796+
rng = pd.period_range("1/1/2000", "1/1/2010", freq="A")
797+
ts = pd.DataFrame(np.random.randn(len(rng), 3), index=rng)
798+
799+
result = ts + ts[::2]
800+
expected = ts + ts
801+
expected.values[1::2] = np.nan
802+
tm.assert_frame_equal(result, expected)
803+
804+
half = ts[::2]
805+
result = ts + half.take(np.random.permutation(len(half)))
806+
tm.assert_frame_equal(result, expected)

pandas/tests/frame/test_period.py

-16
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
import pandas._testing as tm
55

66

7-
def _permute(obj):
8-
return obj.take(np.random.permutation(len(obj)))
9-
10-
117
class TestPeriodIndex:
128
def test_as_frame_columns(self):
139
rng = period_range("1/1/2000", periods=5)
@@ -42,15 +38,3 @@ def test_frame_index_to_string(self):
4238

4339
# it works!
4440
frame.to_string()
45-
46-
def test_align_frame(self):
47-
rng = period_range("1/1/2000", "1/1/2010", freq="A")
48-
ts = DataFrame(np.random.randn(len(rng), 3), index=rng)
49-
50-
result = ts + ts[::2]
51-
expected = ts + ts
52-
expected.values[1::2] = np.nan
53-
tm.assert_frame_equal(result, expected)
54-
55-
result = ts + _permute(ts[::2])
56-
tm.assert_frame_equal(result, expected)

pandas/tests/frame/test_timezones.py

-11
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,6 @@ def test_frame_join_tzaware(self):
8080
tm.assert_index_equal(result.index, ex_index)
8181
assert result.index.tz.zone == "US/Central"
8282

83-
def test_frame_add_tz_mismatch_converts_to_utc(self):
84-
rng = date_range("1/1/2011", periods=10, freq="H", tz="US/Eastern")
85-
df = DataFrame(np.random.randn(len(rng)), index=rng, columns=["a"])
86-
87-
df_moscow = df.tz_convert("Europe/Moscow")
88-
result = df + df_moscow
89-
assert result.index.tz is pytz.utc
90-
91-
result = df_moscow + df
92-
assert result.index.tz is pytz.utc
93-
9483
def test_frame_align_aware(self):
9584
idx1 = date_range("2001", periods=5, freq="H", tz="US/Eastern")
9685
idx2 = date_range("2001", periods=5, freq="2H", tz="US/Eastern")

pandas/tests/series/test_arithmetic.py

+66-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import numpy as np
44
import pytest
5+
import pytz
56

67
from pandas._libs.tslibs import IncompatibleFrequency
78

89
import pandas as pd
9-
from pandas import Series
10+
from pandas import Series, date_range
1011
import pandas._testing as tm
1112

1213

@@ -203,3 +204,67 @@ def test_ser_cmp_result_names(self, names, op):
203204
ser = Series(cidx).rename(names[1])
204205
result = op(ser, cidx)
205206
assert result.name == names[2]
207+
208+
209+
# ------------------------------------------------------------------
210+
# Unsorted
211+
# These arithmetic tests were previously in other files, eventually
212+
# should be parametrized and put into tests.arithmetic
213+
214+
215+
class TestTimeSeriesArithmetic:
216+
# TODO: De-duplicate with test below
217+
def test_series_add_tz_mismatch_converts_to_utc_duplicate(self):
218+
rng = date_range("1/1/2011", periods=10, freq="H", tz="US/Eastern")
219+
ser = Series(np.random.randn(len(rng)), index=rng)
220+
221+
ts_moscow = ser.tz_convert("Europe/Moscow")
222+
223+
result = ser + ts_moscow
224+
assert result.index.tz is pytz.utc
225+
226+
result = ts_moscow + ser
227+
assert result.index.tz is pytz.utc
228+
229+
def test_series_add_tz_mismatch_converts_to_utc(self):
230+
rng = date_range("1/1/2011", periods=100, freq="H", tz="utc")
231+
232+
perm = np.random.permutation(100)[:90]
233+
ser1 = Series(
234+
np.random.randn(90), index=rng.take(perm).tz_convert("US/Eastern")
235+
)
236+
237+
perm = np.random.permutation(100)[:90]
238+
ser2 = Series(
239+
np.random.randn(90), index=rng.take(perm).tz_convert("Europe/Berlin")
240+
)
241+
242+
result = ser1 + ser2
243+
244+
uts1 = ser1.tz_convert("utc")
245+
uts2 = ser2.tz_convert("utc")
246+
expected = uts1 + uts2
247+
248+
assert result.index.tz == pytz.UTC
249+
tm.assert_series_equal(result, expected)
250+
251+
def test_series_add_aware_naive_raises(self):
252+
rng = date_range("1/1/2011", periods=10, freq="H")
253+
ser = Series(np.random.randn(len(rng)), index=rng)
254+
255+
ser_utc = ser.tz_localize("utc")
256+
257+
with pytest.raises(Exception):
258+
ser + ser_utc
259+
260+
with pytest.raises(Exception):
261+
ser_utc + ser
262+
263+
def test_datetime_understood(self):
264+
# Ensures it doesn't fail to create the right series
265+
# reported in issue#16726
266+
series = pd.Series(pd.date_range("2012-01-01", periods=3))
267+
offset = pd.offsets.DateOffset(days=6)
268+
result = series - offset
269+
expected = pd.Series(pd.to_datetime(["2011-12-26", "2011-12-27", "2011-12-28"]))
270+
tm.assert_series_equal(result, expected)

pandas/tests/series/test_datetime_values.py

-9
Original file line numberDiff line numberDiff line change
@@ -632,15 +632,6 @@ def test_date_tz(self):
632632
tm.assert_series_equal(s.dt.date, expected)
633633
tm.assert_series_equal(s.apply(lambda x: x.date()), expected)
634634

635-
def test_datetime_understood(self):
636-
# Ensures it doesn't fail to create the right series
637-
# reported in issue#16726
638-
series = pd.Series(pd.date_range("2012-01-01", periods=3))
639-
offset = pd.offsets.DateOffset(days=6)
640-
result = series - offset
641-
expected = pd.Series(pd.to_datetime(["2011-12-26", "2011-12-27", "2011-12-28"]))
642-
tm.assert_series_equal(result, expected)
643-
644635
def test_dt_timetz_accessor(self, tz_naive_fixture):
645636
# GH21358
646637
tz = maybe_get_tz(tz_naive_fixture)

pandas/tests/series/test_timezones.py

-47
Original file line numberDiff line numberDiff line change
@@ -38,53 +38,6 @@ def test_string_index_alias_tz_aware(self, tz):
3838
result = ser["1/3/2000"]
3939
tm.assert_almost_equal(result, ser[2])
4040

41-
# TODO: De-duplicate with test below
42-
def test_series_add_tz_mismatch_converts_to_utc_duplicate(self):
43-
rng = date_range("1/1/2011", periods=10, freq="H", tz="US/Eastern")
44-
ser = Series(np.random.randn(len(rng)), index=rng)
45-
46-
ts_moscow = ser.tz_convert("Europe/Moscow")
47-
48-
result = ser + ts_moscow
49-
assert result.index.tz is pytz.utc
50-
51-
result = ts_moscow + ser
52-
assert result.index.tz is pytz.utc
53-
54-
def test_series_add_tz_mismatch_converts_to_utc(self):
55-
rng = date_range("1/1/2011", periods=100, freq="H", tz="utc")
56-
57-
perm = np.random.permutation(100)[:90]
58-
ser1 = Series(
59-
np.random.randn(90), index=rng.take(perm).tz_convert("US/Eastern")
60-
)
61-
62-
perm = np.random.permutation(100)[:90]
63-
ser2 = Series(
64-
np.random.randn(90), index=rng.take(perm).tz_convert("Europe/Berlin")
65-
)
66-
67-
result = ser1 + ser2
68-
69-
uts1 = ser1.tz_convert("utc")
70-
uts2 = ser2.tz_convert("utc")
71-
expected = uts1 + uts2
72-
73-
assert result.index.tz == pytz.UTC
74-
tm.assert_series_equal(result, expected)
75-
76-
def test_series_add_aware_naive_raises(self):
77-
rng = date_range("1/1/2011", periods=10, freq="H")
78-
ser = Series(np.random.randn(len(rng)), index=rng)
79-
80-
ser_utc = ser.tz_localize("utc")
81-
82-
with pytest.raises(Exception):
83-
ser + ser_utc
84-
85-
with pytest.raises(Exception):
86-
ser_utc + ser
87-
8841
def test_series_align_aware(self):
8942
idx1 = date_range("2001", periods=5, freq="H", tz="US/Eastern")
9043
ser = Series(np.random.randn(len(idx1)), index=idx1)

0 commit comments

Comments
 (0)