Skip to content

TST: misplaced arithmetic tests #32275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np
import pytest
import pytz

import pandas as pd
import pandas._testing as tm
Expand Down Expand Up @@ -771,3 +772,35 @@ def test_frame_single_columns_object_sum_axis_1():
result = df.sum(axis=1)
expected = pd.Series(["A", 1.2, 0])
tm.assert_series_equal(result, expected)


# -------------------------------------------------------------------
# Unsorted
# These arithmetic tests were previously in other files, eventually
# should be parametrized and put into tests.arithmetic


class TestFrameArithmeticUnsorted:
def test_frame_add_tz_mismatch_converts_to_utc(self):
rng = pd.date_range("1/1/2011", periods=10, freq="H", tz="US/Eastern")
df = pd.DataFrame(np.random.randn(len(rng)), index=rng, columns=["a"])

df_moscow = df.tz_convert("Europe/Moscow")
result = df + df_moscow
assert result.index.tz is pytz.utc

result = df_moscow + df
assert result.index.tz is pytz.utc

def test_align_frame(self):
rng = pd.period_range("1/1/2000", "1/1/2010", freq="A")
ts = pd.DataFrame(np.random.randn(len(rng), 3), index=rng)

result = ts + ts[::2]
expected = ts + ts
expected.values[1::2] = np.nan
tm.assert_frame_equal(result, expected)

half = ts[::2]
result = ts + half.take(np.random.permutation(len(half)))
tm.assert_frame_equal(result, expected)
16 changes: 0 additions & 16 deletions pandas/tests/frame/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import pandas._testing as tm


def _permute(obj):
return obj.take(np.random.permutation(len(obj)))


class TestPeriodIndex:
def test_as_frame_columns(self):
rng = period_range("1/1/2000", periods=5)
Expand Down Expand Up @@ -42,15 +38,3 @@ def test_frame_index_to_string(self):

# it works!
frame.to_string()

def test_align_frame(self):
rng = period_range("1/1/2000", "1/1/2010", freq="A")
ts = DataFrame(np.random.randn(len(rng), 3), index=rng)

result = ts + ts[::2]
expected = ts + ts
expected.values[1::2] = np.nan
tm.assert_frame_equal(result, expected)

result = ts + _permute(ts[::2])
tm.assert_frame_equal(result, expected)
11 changes: 0 additions & 11 deletions pandas/tests/frame/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,6 @@ def test_frame_join_tzaware(self):
tm.assert_index_equal(result.index, ex_index)
assert result.index.tz.zone == "US/Central"

def test_frame_add_tz_mismatch_converts_to_utc(self):
rng = date_range("1/1/2011", periods=10, freq="H", tz="US/Eastern")
df = DataFrame(np.random.randn(len(rng)), index=rng, columns=["a"])

df_moscow = df.tz_convert("Europe/Moscow")
result = df + df_moscow
assert result.index.tz is pytz.utc

result = df_moscow + df
assert result.index.tz is pytz.utc

def test_frame_align_aware(self):
idx1 = date_range("2001", periods=5, freq="H", tz="US/Eastern")
idx2 = date_range("2001", periods=5, freq="2H", tz="US/Eastern")
Expand Down
67 changes: 66 additions & 1 deletion pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import numpy as np
import pytest
import pytz

from pandas._libs.tslibs import IncompatibleFrequency

import pandas as pd
from pandas import Series
from pandas import Series, date_range
import pandas._testing as tm


Expand Down Expand Up @@ -203,3 +204,67 @@ def test_ser_cmp_result_names(self, names, op):
ser = Series(cidx).rename(names[1])
result = op(ser, cidx)
assert result.name == names[2]


# ------------------------------------------------------------------
# Unsorted
# These arithmetic tests were previously in other files, eventually
# should be parametrized and put into tests.arithmetic


class TestTimeSeriesArithmetic:
# TODO: De-duplicate with test below
def test_series_add_tz_mismatch_converts_to_utc_duplicate(self):
rng = date_range("1/1/2011", periods=10, freq="H", tz="US/Eastern")
ser = Series(np.random.randn(len(rng)), index=rng)

ts_moscow = ser.tz_convert("Europe/Moscow")

result = ser + ts_moscow
assert result.index.tz is pytz.utc

result = ts_moscow + ser
assert result.index.tz is pytz.utc

def test_series_add_tz_mismatch_converts_to_utc(self):
rng = date_range("1/1/2011", periods=100, freq="H", tz="utc")

perm = np.random.permutation(100)[:90]
ser1 = Series(
np.random.randn(90), index=rng.take(perm).tz_convert("US/Eastern")
)

perm = np.random.permutation(100)[:90]
ser2 = Series(
np.random.randn(90), index=rng.take(perm).tz_convert("Europe/Berlin")
)

result = ser1 + ser2

uts1 = ser1.tz_convert("utc")
uts2 = ser2.tz_convert("utc")
expected = uts1 + uts2

assert result.index.tz == pytz.UTC
tm.assert_series_equal(result, expected)

def test_series_add_aware_naive_raises(self):
rng = date_range("1/1/2011", periods=10, freq="H")
ser = Series(np.random.randn(len(rng)), index=rng)

ser_utc = ser.tz_localize("utc")

with pytest.raises(Exception):
ser + ser_utc

with pytest.raises(Exception):
ser_utc + ser

def test_datetime_understood(self):
# Ensures it doesn't fail to create the right series
# reported in issue#16726
series = pd.Series(pd.date_range("2012-01-01", periods=3))
offset = pd.offsets.DateOffset(days=6)
result = series - offset
expected = pd.Series(pd.to_datetime(["2011-12-26", "2011-12-27", "2011-12-28"]))
tm.assert_series_equal(result, expected)
9 changes: 0 additions & 9 deletions pandas/tests/series/test_datetime_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,15 +632,6 @@ def test_date_tz(self):
tm.assert_series_equal(s.dt.date, expected)
tm.assert_series_equal(s.apply(lambda x: x.date()), expected)

def test_datetime_understood(self):
# Ensures it doesn't fail to create the right series
# reported in issue#16726
series = pd.Series(pd.date_range("2012-01-01", periods=3))
offset = pd.offsets.DateOffset(days=6)
result = series - offset
expected = pd.Series(pd.to_datetime(["2011-12-26", "2011-12-27", "2011-12-28"]))
tm.assert_series_equal(result, expected)

def test_dt_timetz_accessor(self, tz_naive_fixture):
# GH21358
tz = maybe_get_tz(tz_naive_fixture)
Expand Down
47 changes: 0 additions & 47 deletions pandas/tests/series/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,53 +38,6 @@ def test_string_index_alias_tz_aware(self, tz):
result = ser["1/3/2000"]
tm.assert_almost_equal(result, ser[2])

# TODO: De-duplicate with test below
def test_series_add_tz_mismatch_converts_to_utc_duplicate(self):
rng = date_range("1/1/2011", periods=10, freq="H", tz="US/Eastern")
ser = Series(np.random.randn(len(rng)), index=rng)

ts_moscow = ser.tz_convert("Europe/Moscow")

result = ser + ts_moscow
assert result.index.tz is pytz.utc

result = ts_moscow + ser
assert result.index.tz is pytz.utc

def test_series_add_tz_mismatch_converts_to_utc(self):
rng = date_range("1/1/2011", periods=100, freq="H", tz="utc")

perm = np.random.permutation(100)[:90]
ser1 = Series(
np.random.randn(90), index=rng.take(perm).tz_convert("US/Eastern")
)

perm = np.random.permutation(100)[:90]
ser2 = Series(
np.random.randn(90), index=rng.take(perm).tz_convert("Europe/Berlin")
)

result = ser1 + ser2

uts1 = ser1.tz_convert("utc")
uts2 = ser2.tz_convert("utc")
expected = uts1 + uts2

assert result.index.tz == pytz.UTC
tm.assert_series_equal(result, expected)

def test_series_add_aware_naive_raises(self):
rng = date_range("1/1/2011", periods=10, freq="H")
ser = Series(np.random.randn(len(rng)), index=rng)

ser_utc = ser.tz_localize("utc")

with pytest.raises(Exception):
ser + ser_utc

with pytest.raises(Exception):
ser_utc + ser

def test_series_align_aware(self):
idx1 = date_range("2001", periods=5, freq="H", tz="US/Eastern")
ser = Series(np.random.randn(len(idx1)), index=idx1)
Expand Down