Skip to content

Commit a1f06d4

Browse files
jbrockmendelPingviinituutti
authored andcommitted
TST: Fix xfailing DataFrame arithmetic tests by transposing (pandas-dev#23620)
1 parent a125a12 commit a1f06d4

File tree

5 files changed

+113
-117
lines changed

5 files changed

+113
-117
lines changed

pandas/tests/arithmetic/conftest.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,18 @@ def box_df_fail(request):
158158
return request.param
159159

160160

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

176175

pandas/tests/arithmetic/test_datetime64.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -1155,14 +1155,18 @@ def test_dti_add_intarray_no_freq(self, box):
11551155
def test_dti_add_timedeltalike(self, tz_naive_fixture, two_hours,
11561156
box_with_datetime):
11571157
# GH#22005, GH#22163 check DataFrame doesn't raise TypeError
1158+
box = box_with_datetime
1159+
11581160
tz = tz_naive_fixture
11591161
rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz)
1160-
rng = tm.box_expected(rng, box_with_datetime)
1162+
1163+
# FIXME: calling with transpose=True raises ValueError
1164+
rng = tm.box_expected(rng, box, transpose=False)
11611165

11621166
result = rng + two_hours
11631167
expected = pd.date_range('2000-01-01 02:00',
11641168
'2000-02-01 02:00', tz=tz)
1165-
expected = tm.box_expected(expected, box_with_datetime)
1169+
expected = tm.box_expected(expected, box, transpose=False)
11661170
tm.assert_equal(result, expected)
11671171

11681172
def test_dti_iadd_timedeltalike(self, tz_naive_fixture, two_hours):
@@ -1192,12 +1196,15 @@ def test_dti_isub_timedeltalike(self, tz_naive_fixture, two_hours):
11921196
def test_dt64arr_add_sub_td64_nat(self, box, tz_naive_fixture):
11931197
# GH#23320 special handling for timedelta64("NaT")
11941198
tz = tz_naive_fixture
1199+
11951200
dti = pd.date_range("1994-04-01", periods=9, tz=tz, freq="QS")
11961201
other = np.timedelta64("NaT")
11971202
expected = pd.DatetimeIndex(["NaT"] * 9, tz=tz)
11981203

1199-
obj = tm.box_expected(dti, box)
1200-
expected = tm.box_expected(expected, box)
1204+
# FIXME: fails with transpose=True due to tz-aware DataFrame
1205+
# transpose bug
1206+
obj = tm.box_expected(dti, box, transpose=False)
1207+
expected = tm.box_expected(expected, box, transpose=False)
12011208

12021209
result = obj + other
12031210
tm.assert_equal(result, expected)
@@ -1450,10 +1457,8 @@ def test_sub_period(self, freq, box_with_datetime):
14501457
operator.sub, ops.rsub])
14511458
@pytest.mark.parametrize('pi_freq', ['D', 'W', 'Q', 'H'])
14521459
@pytest.mark.parametrize('dti_freq', [None, 'D'])
1453-
def test_dti_sub_pi(self, dti_freq, pi_freq, op, box_df_broadcast_failure):
1460+
def test_dti_sub_pi(self, dti_freq, pi_freq, op, box):
14541461
# GH#20049 subtracting PeriodIndex should raise TypeError
1455-
box = box_df_broadcast_failure
1456-
14571462
dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq=dti_freq)
14581463
pi = dti.to_period(pi_freq)
14591464

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

17831788
def test_dti_add_offset_tzaware(self, tz_aware_fixture, box_with_datetime):
17841789
# GH#21610, GH#22163 ensure DataFrame doesn't return object-dtype
1790+
box = box_with_datetime
1791+
17851792
timezone = tz_aware_fixture
17861793
if timezone == 'US/Pacific':
17871794
dates = date_range('2012-11-01', periods=3, tz=timezone)
@@ -1793,8 +1800,9 @@ def test_dti_add_offset_tzaware(self, tz_aware_fixture, box_with_datetime):
17931800
expected = DatetimeIndex(['2010-11-01 05:00', '2010-11-01 06:00',
17941801
'2010-11-01 07:00'], freq='H', tz=timezone)
17951802

1796-
dates = tm.box_expected(dates, box_with_datetime)
1797-
expected = tm.box_expected(expected, box_with_datetime)
1803+
# FIXME: these raise ValueError with transpose=True
1804+
dates = tm.box_expected(dates, box, transpose=False)
1805+
expected = tm.box_expected(expected, box, transpose=False)
17981806

17991807
# TODO: parametrize over the scalar being added? radd? sub?
18001808
offset = dates + pd.offsets.Hour(5)

pandas/tests/arithmetic/test_period.py

+20-23
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ def test_parr_cmp_period_scalar(self, freq, box):
7070
tm.assert_equal(per >= base, exp)
7171

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

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

110109
@pytest.mark.parametrize('freq', ['M', '2M', '3M'])
111-
def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box_df_fail):
110+
def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box):
112111
# GH#13200
113112
# different base freq
114-
box = box_df_fail
115-
116113
base = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'],
117114
freq=freq)
118115
base = tm.box_expected(base, box)
@@ -302,9 +299,7 @@ class TestPeriodIndexArithmetic(object):
302299
# PeriodIndex - other is defined for integers, timedelta-like others,
303300
# and PeriodIndex (with matching freq)
304301

305-
def test_parr_add_iadd_parr_raises(self, box_df_broadcast_failure):
306-
box = box_df_broadcast_failure
307-
302+
def test_parr_add_iadd_parr_raises(self, box):
308303
rng = pd.period_range('1/1/2000', freq='D', periods=5)
309304
other = pd.period_range('1/6/2000', freq='D', periods=5)
310305
# TODO: parametrize over boxes for other?
@@ -346,9 +341,7 @@ def test_pi_sub_pi_with_nat(self):
346341
expected = pd.Index([pd.NaT, 0 * off, 0 * off, 0 * off, 0 * off])
347342
tm.assert_index_equal(result, expected)
348343

349-
def test_parr_sub_pi_mismatched_freq(self, box_df_broadcast_failure):
350-
box = box_df_broadcast_failure
351-
344+
def test_parr_sub_pi_mismatched_freq(self, box):
352345
rng = pd.period_range('1/1/2000', freq='D', periods=5)
353346
other = pd.period_range('1/6/2000', freq='H', periods=5)
354347
# TODO: parametrize over boxes for other?
@@ -364,9 +357,6 @@ def test_parr_sub_pi_mismatched_freq(self, box_df_broadcast_failure):
364357
@pytest.mark.parametrize('op', [operator.add, ops.radd,
365358
operator.sub, ops.rsub])
366359
def test_pi_add_sub_float(self, op, other, box):
367-
if box is pd.DataFrame and isinstance(other, np.ndarray):
368-
pytest.xfail(reason="Tries to broadcast incorrectly")
369-
370360
dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
371361
pi = dti.to_period('D')
372362
pi = tm.box_expected(pi, box)
@@ -563,15 +553,18 @@ def test_pi_sub_isub_offset(self):
563553
rng -= pd.offsets.MonthEnd(5)
564554
tm.assert_index_equal(rng, expected)
565555

566-
def test_pi_add_offset_n_gt1(self, box):
556+
def test_pi_add_offset_n_gt1(self, box_transpose_fail):
567557
# GH#23215
568558
# add offset to PeriodIndex with freq.n > 1
559+
box, transpose = box_transpose_fail
560+
569561
per = pd.Period('2016-01', freq='2M')
570562
pi = pd.PeriodIndex([per])
571563

572564
expected = pd.PeriodIndex(['2016-03'], freq='2M')
573-
pi = tm.box_expected(pi, box)
574-
expected = tm.box_expected(expected, box)
565+
566+
pi = tm.box_expected(pi, box, transpose=transpose)
567+
expected = tm.box_expected(expected, box, transpose=transpose)
575568

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

586580
pi = pd.PeriodIndex(['2016-01'], freq='2M')
587-
pi = tm.box_expected(pi, box_with_period)
588-
589581
expected = pd.PeriodIndex(['2016-04'], freq='2M')
590-
expected = tm.box_expected(expected, box_with_period)
582+
583+
# FIXME: with transposing these tests fail
584+
pi = tm.box_expected(pi, box, transpose=False)
585+
expected = tm.box_expected(expected, box, transpose=False)
591586

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

804-
def test_parr_add_sub_td64_nat(self, box):
799+
def test_parr_add_sub_td64_nat(self, box_transpose_fail):
805800
# GH#23320 special handling for timedelta64("NaT")
801+
box, transpose = box_transpose_fail
802+
806803
pi = pd.period_range("1994-04-01", periods=9, freq="19D")
807804
other = np.timedelta64("NaT")
808805
expected = pd.PeriodIndex(["NaT"] * 9, freq="19D")
809806

810-
obj = tm.box_expected(pi, box)
811-
expected = tm.box_expected(expected, box)
807+
obj = tm.box_expected(pi, box, transpose=transpose)
808+
expected = tm.box_expected(expected, box, transpose=transpose)
812809

813810
result = obj + other
814811
tm.assert_equal(result, expected)

0 commit comments

Comments
 (0)