|
2 | 2 |
|
3 | 3 | import numpy as np
|
4 | 4 | import pytest
|
| 5 | +import pytz |
5 | 6 |
|
6 | 7 | from pandas._libs.tslibs import IncompatibleFrequency
|
7 | 8 |
|
8 | 9 | import pandas as pd
|
9 |
| -from pandas import Series |
| 10 | +from pandas import Series, date_range |
10 | 11 | import pandas._testing as tm
|
11 | 12 |
|
12 | 13 |
|
@@ -203,3 +204,67 @@ def test_ser_cmp_result_names(self, names, op):
|
203 | 204 | ser = Series(cidx).rename(names[1])
|
204 | 205 | result = op(ser, cidx)
|
205 | 206 | 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) |
0 commit comments