Skip to content

Commit

Permalink
TST: Fix xfailing DataFrame arithmetic tests by transposing (#23620)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and gfyoung committed Nov 12, 2018
1 parent 592fd64 commit bb32564
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 117 deletions.
23 changes: 11 additions & 12 deletions pandas/tests/arithmetic/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,18 @@ def box_df_fail(request):
return request.param


@pytest.fixture(params=[
pd.Index,
pd.Series,
pytest.param(pd.DataFrame,
marks=pytest.mark.xfail(reason="Tries to broadcast "
"incorrectly",
strict=True, raises=ValueError))
], ids=lambda x: x.__name__)
def box_df_broadcast_failure(request):
"""
Fixture equivalent to `box` but with the common failing case where
the DataFrame operation tries to broadcast incorrectly.
@pytest.fixture(params=[(pd.Index, False),
(pd.Series, False),
(pd.DataFrame, False),
pytest.param((pd.DataFrame, True),
marks=pytest.mark.xfail(strict=True))],
ids=lambda x: x[0].__name__ + '-' + str(x[1]))
def box_transpose_fail(request):
"""
Fixture similar to `box` but testing both transpose cases for DataFrame,
with the tranpose=True case xfailed.
"""
# GH#23620
return request.param


Expand Down
26 changes: 17 additions & 9 deletions pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,14 +1155,18 @@ def test_dti_add_intarray_no_freq(self, box):
def test_dti_add_timedeltalike(self, tz_naive_fixture, two_hours,
box_with_datetime):
# GH#22005, GH#22163 check DataFrame doesn't raise TypeError
box = box_with_datetime

tz = tz_naive_fixture
rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz)
rng = tm.box_expected(rng, box_with_datetime)

# FIXME: calling with transpose=True raises ValueError
rng = tm.box_expected(rng, box, transpose=False)

result = rng + two_hours
expected = pd.date_range('2000-01-01 02:00',
'2000-02-01 02:00', tz=tz)
expected = tm.box_expected(expected, box_with_datetime)
expected = tm.box_expected(expected, box, transpose=False)
tm.assert_equal(result, expected)

def test_dti_iadd_timedeltalike(self, tz_naive_fixture, two_hours):
Expand Down Expand Up @@ -1192,12 +1196,15 @@ def test_dti_isub_timedeltalike(self, tz_naive_fixture, two_hours):
def test_dt64arr_add_sub_td64_nat(self, box, tz_naive_fixture):
# GH#23320 special handling for timedelta64("NaT")
tz = tz_naive_fixture

dti = pd.date_range("1994-04-01", periods=9, tz=tz, freq="QS")
other = np.timedelta64("NaT")
expected = pd.DatetimeIndex(["NaT"] * 9, tz=tz)

obj = tm.box_expected(dti, box)
expected = tm.box_expected(expected, box)
# FIXME: fails with transpose=True due to tz-aware DataFrame
# transpose bug
obj = tm.box_expected(dti, box, transpose=False)
expected = tm.box_expected(expected, box, transpose=False)

result = obj + other
tm.assert_equal(result, expected)
Expand Down Expand Up @@ -1450,10 +1457,8 @@ def test_sub_period(self, freq, box_with_datetime):
operator.sub, ops.rsub])
@pytest.mark.parametrize('pi_freq', ['D', 'W', 'Q', 'H'])
@pytest.mark.parametrize('dti_freq', [None, 'D'])
def test_dti_sub_pi(self, dti_freq, pi_freq, op, box_df_broadcast_failure):
def test_dti_sub_pi(self, dti_freq, pi_freq, op, box):
# GH#20049 subtracting PeriodIndex should raise TypeError
box = box_df_broadcast_failure

dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq=dti_freq)
pi = dti.to_period(pi_freq)

Expand Down Expand Up @@ -1782,6 +1787,8 @@ def test_dti_with_offset_series(self, tz_naive_fixture, names):

def test_dti_add_offset_tzaware(self, tz_aware_fixture, box_with_datetime):
# GH#21610, GH#22163 ensure DataFrame doesn't return object-dtype
box = box_with_datetime

timezone = tz_aware_fixture
if timezone == 'US/Pacific':
dates = date_range('2012-11-01', periods=3, tz=timezone)
Expand All @@ -1793,8 +1800,9 @@ def test_dti_add_offset_tzaware(self, tz_aware_fixture, box_with_datetime):
expected = DatetimeIndex(['2010-11-01 05:00', '2010-11-01 06:00',
'2010-11-01 07:00'], freq='H', tz=timezone)

dates = tm.box_expected(dates, box_with_datetime)
expected = tm.box_expected(expected, box_with_datetime)
# FIXME: these raise ValueError with transpose=True
dates = tm.box_expected(dates, box, transpose=False)
expected = tm.box_expected(expected, box, transpose=False)

# TODO: parametrize over the scalar being added? radd? sub?
offset = dates + pd.offsets.Hour(5)
Expand Down
43 changes: 20 additions & 23 deletions pandas/tests/arithmetic/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ def test_parr_cmp_period_scalar(self, freq, box):
tm.assert_equal(per >= base, exp)

@pytest.mark.parametrize('freq', ['M', '2M', '3M'])
def test_parr_cmp_pi(self, freq, box_df_fail):
def test_parr_cmp_pi(self, freq, box):
# GH#13200
box = box_df_fail
xbox = np.ndarray if box is pd.Index else box

base = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'],
Expand Down Expand Up @@ -108,11 +107,9 @@ def test_parr_cmp_pi(self, freq, box_df_fail):
tm.assert_equal(base <= idx, exp)

@pytest.mark.parametrize('freq', ['M', '2M', '3M'])
def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box_df_fail):
def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box):
# GH#13200
# different base freq
box = box_df_fail

base = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'],
freq=freq)
base = tm.box_expected(base, box)
Expand Down Expand Up @@ -302,9 +299,7 @@ class TestPeriodIndexArithmetic(object):
# PeriodIndex - other is defined for integers, timedelta-like others,
# and PeriodIndex (with matching freq)

def test_parr_add_iadd_parr_raises(self, box_df_broadcast_failure):
box = box_df_broadcast_failure

def test_parr_add_iadd_parr_raises(self, box):
rng = pd.period_range('1/1/2000', freq='D', periods=5)
other = pd.period_range('1/6/2000', freq='D', periods=5)
# TODO: parametrize over boxes for other?
Expand Down Expand Up @@ -346,9 +341,7 @@ def test_pi_sub_pi_with_nat(self):
expected = pd.Index([pd.NaT, 0 * off, 0 * off, 0 * off, 0 * off])
tm.assert_index_equal(result, expected)

def test_parr_sub_pi_mismatched_freq(self, box_df_broadcast_failure):
box = box_df_broadcast_failure

def test_parr_sub_pi_mismatched_freq(self, box):
rng = pd.period_range('1/1/2000', freq='D', periods=5)
other = pd.period_range('1/6/2000', freq='H', periods=5)
# TODO: parametrize over boxes for other?
Expand All @@ -364,9 +357,6 @@ def test_parr_sub_pi_mismatched_freq(self, box_df_broadcast_failure):
@pytest.mark.parametrize('op', [operator.add, ops.radd,
operator.sub, ops.rsub])
def test_pi_add_sub_float(self, op, other, box):
if box is pd.DataFrame and isinstance(other, np.ndarray):
pytest.xfail(reason="Tries to broadcast incorrectly")

dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
pi = dti.to_period('D')
pi = tm.box_expected(pi, box)
Expand Down Expand Up @@ -563,15 +553,18 @@ def test_pi_sub_isub_offset(self):
rng -= pd.offsets.MonthEnd(5)
tm.assert_index_equal(rng, expected)

def test_pi_add_offset_n_gt1(self, box):
def test_pi_add_offset_n_gt1(self, box_transpose_fail):
# GH#23215
# add offset to PeriodIndex with freq.n > 1
box, transpose = box_transpose_fail

per = pd.Period('2016-01', freq='2M')
pi = pd.PeriodIndex([per])

expected = pd.PeriodIndex(['2016-03'], freq='2M')
pi = tm.box_expected(pi, box)
expected = tm.box_expected(expected, box)

pi = tm.box_expected(pi, box, transpose=transpose)
expected = tm.box_expected(expected, box, transpose=transpose)

result = pi + per.freq
tm.assert_equal(result, expected)
Expand All @@ -582,12 +575,14 @@ def test_pi_add_offset_n_gt1(self, box):
def test_pi_add_offset_n_gt1_not_divisible(self, box_with_period):
# GH#23215
# PeriodIndex with freq.n > 1 add offset with offset.n % freq.n != 0
box = box_with_period

pi = pd.PeriodIndex(['2016-01'], freq='2M')
pi = tm.box_expected(pi, box_with_period)

expected = pd.PeriodIndex(['2016-04'], freq='2M')
expected = tm.box_expected(expected, box_with_period)

# FIXME: with transposing these tests fail
pi = tm.box_expected(pi, box, transpose=False)
expected = tm.box_expected(expected, box, transpose=False)

result = pi + to_offset('3M')
tm.assert_equal(result, expected)
Expand Down Expand Up @@ -801,14 +796,16 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_monthly(self,
with pytest.raises(period.IncompatibleFrequency, match=msg):
rng -= other

def test_parr_add_sub_td64_nat(self, box):
def test_parr_add_sub_td64_nat(self, box_transpose_fail):
# GH#23320 special handling for timedelta64("NaT")
box, transpose = box_transpose_fail

pi = pd.period_range("1994-04-01", periods=9, freq="19D")
other = np.timedelta64("NaT")
expected = pd.PeriodIndex(["NaT"] * 9, freq="19D")

obj = tm.box_expected(pi, box)
expected = tm.box_expected(expected, box)
obj = tm.box_expected(pi, box, transpose=transpose)
expected = tm.box_expected(expected, box, transpose=transpose)

result = obj + other
tm.assert_equal(result, expected)
Expand Down
Loading

0 comments on commit bb32564

Please sign in to comment.