Skip to content

Commit 9b2e6db

Browse files
jbrockmendeljreback
authored andcommitted
TST: Continue collecting arithmetic tests (#22559)
1 parent 8078500 commit 9b2e6db

File tree

7 files changed

+102
-90
lines changed

7 files changed

+102
-90
lines changed

pandas/tests/arithmetic/test_numeric.py

+69
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ def test_operator_series_comparison_zerorank(self):
4242
expected = 0.0 > pd.Series([1, 2, 3])
4343
tm.assert_series_equal(result, expected)
4444

45+
def test_df_numeric_cmp_dt64_raises(self):
46+
# GH#8932, GH#22163
47+
ts = pd.Timestamp.now()
48+
df = pd.DataFrame({'x': range(5)})
49+
with pytest.raises(TypeError):
50+
df > ts
51+
with pytest.raises(TypeError):
52+
df < ts
53+
with pytest.raises(TypeError):
54+
ts < df
55+
with pytest.raises(TypeError):
56+
ts > df
57+
58+
assert not (df == ts).any().any()
59+
assert (df != ts).all().all()
60+
61+
def test_compare_invalid(self):
62+
# GH#8058
63+
# ops testing
64+
a = pd.Series(np.random.randn(5), name=0)
65+
b = pd.Series(np.random.randn(5))
66+
b.name = pd.Timestamp('2000-01-01')
67+
tm.assert_series_equal(a / b, 1 / (b / a))
68+
4569

4670
# ------------------------------------------------------------------
4771
# Numeric dtypes Arithmetic with Timedelta Scalar
@@ -754,6 +778,51 @@ def check(series, other):
754778
check(tser, 5)
755779

756780

781+
class TestUFuncCompat(object):
782+
@pytest.mark.parametrize('holder', [pd.Int64Index, pd.UInt64Index,
783+
pd.Float64Index, pd.Series])
784+
def test_ufunc_coercions(self, holder):
785+
idx = holder([1, 2, 3, 4, 5], name='x')
786+
box = pd.Series if holder is pd.Series else pd.Index
787+
788+
result = np.sqrt(idx)
789+
assert result.dtype == 'f8' and isinstance(result, box)
790+
exp = pd.Float64Index(np.sqrt(np.array([1, 2, 3, 4, 5])), name='x')
791+
exp = tm.box_expected(exp, box)
792+
tm.assert_equal(result, exp)
793+
794+
result = np.divide(idx, 2.)
795+
assert result.dtype == 'f8' and isinstance(result, box)
796+
exp = pd.Float64Index([0.5, 1., 1.5, 2., 2.5], name='x')
797+
exp = tm.box_expected(exp, box)
798+
tm.assert_equal(result, exp)
799+
800+
# _evaluate_numeric_binop
801+
result = idx + 2.
802+
assert result.dtype == 'f8' and isinstance(result, box)
803+
exp = pd.Float64Index([3., 4., 5., 6., 7.], name='x')
804+
exp = tm.box_expected(exp, box)
805+
tm.assert_equal(result, exp)
806+
807+
result = idx - 2.
808+
assert result.dtype == 'f8' and isinstance(result, box)
809+
exp = pd.Float64Index([-1., 0., 1., 2., 3.], name='x')
810+
exp = tm.box_expected(exp, box)
811+
tm.assert_equal(result, exp)
812+
813+
result = idx * 1.
814+
assert result.dtype == 'f8' and isinstance(result, box)
815+
exp = pd.Float64Index([1., 2., 3., 4., 5.], name='x')
816+
exp = tm.box_expected(exp, box)
817+
tm.assert_equal(result, exp)
818+
819+
result = idx / 2.
820+
assert result.dtype == 'f8' and isinstance(result, box)
821+
exp = pd.Float64Index([0.5, 1., 1.5, 2., 2.5], name='x')
822+
exp = tm.box_expected(exp, box)
823+
tm.assert_equal(result, exp)
824+
825+
757826
class TestObjectDtypeEquivalence(object):
758827
# Tests that arithmetic operations match operations executed elementwise
759828

pandas/tests/arithmetic/test_object.py

+33
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,36 @@ def test_series_with_dtype_radd_timedelta(self, dtype):
180180

181181
result = ser + pd.Timedelta('3 days')
182182
tm.assert_series_equal(result, expected)
183+
184+
# TODO: cleanup & parametrize over box
185+
def test_mixed_timezone_series_ops_object(self):
186+
# GH#13043
187+
ser = pd.Series([pd.Timestamp('2015-01-01', tz='US/Eastern'),
188+
pd.Timestamp('2015-01-01', tz='Asia/Tokyo')],
189+
name='xxx')
190+
assert ser.dtype == object
191+
192+
exp = pd.Series([pd.Timestamp('2015-01-02', tz='US/Eastern'),
193+
pd.Timestamp('2015-01-02', tz='Asia/Tokyo')],
194+
name='xxx')
195+
tm.assert_series_equal(ser + pd.Timedelta('1 days'), exp)
196+
tm.assert_series_equal(pd.Timedelta('1 days') + ser, exp)
197+
198+
# object series & object series
199+
ser2 = pd.Series([pd.Timestamp('2015-01-03', tz='US/Eastern'),
200+
pd.Timestamp('2015-01-05', tz='Asia/Tokyo')],
201+
name='xxx')
202+
assert ser2.dtype == object
203+
exp = pd.Series([pd.Timedelta('2 days'), pd.Timedelta('4 days')],
204+
name='xxx')
205+
tm.assert_series_equal(ser2 - ser, exp)
206+
tm.assert_series_equal(ser - ser2, -exp)
207+
208+
ser = pd.Series([pd.Timedelta('01:00:00'), pd.Timedelta('02:00:00')],
209+
name='xxx', dtype=object)
210+
assert ser.dtype == object
211+
212+
exp = pd.Series([pd.Timedelta('01:30:00'), pd.Timedelta('02:30:00')],
213+
name='xxx')
214+
tm.assert_series_equal(ser + pd.Timedelta('00:30:00'), exp)
215+
tm.assert_series_equal(pd.Timedelta('00:30:00') + ser, exp)

pandas/tests/frame/test_arithmetic.py

-16
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,6 @@ def test_mixed_comparison(self):
4848
result = df != other
4949
assert result.all().all()
5050

51-
def test_df_numeric_cmp_dt64_raises(self):
52-
# GH#8932, GH#22163
53-
ts = pd.Timestamp.now()
54-
df = pd.DataFrame({'x': range(5)})
55-
with pytest.raises(TypeError):
56-
df > ts
57-
with pytest.raises(TypeError):
58-
df < ts
59-
with pytest.raises(TypeError):
60-
ts < df
61-
with pytest.raises(TypeError):
62-
ts > df
63-
64-
assert not (df == ts).any().any()
65-
assert (df != ts).all().all()
66-
6751
def test_df_boolean_comparison_error(self):
6852
# GH#4576
6953
# boolean comparisons with a tuple/list give unexpected results

pandas/tests/indexes/test_numeric.py

-34
Original file line numberDiff line numberDiff line change
@@ -565,40 +565,6 @@ def test_slice_keep_name(self):
565565
idx = self._holder([1, 2], name='asdf')
566566
assert idx.name == idx[1:].name
567567

568-
def test_ufunc_coercions(self):
569-
idx = self._holder([1, 2, 3, 4, 5], name='x')
570-
571-
result = np.sqrt(idx)
572-
assert isinstance(result, Float64Index)
573-
exp = Float64Index(np.sqrt(np.array([1, 2, 3, 4, 5])), name='x')
574-
tm.assert_index_equal(result, exp)
575-
576-
result = np.divide(idx, 2.)
577-
assert isinstance(result, Float64Index)
578-
exp = Float64Index([0.5, 1., 1.5, 2., 2.5], name='x')
579-
tm.assert_index_equal(result, exp)
580-
581-
# _evaluate_numeric_binop
582-
result = idx + 2.
583-
assert isinstance(result, Float64Index)
584-
exp = Float64Index([3., 4., 5., 6., 7.], name='x')
585-
tm.assert_index_equal(result, exp)
586-
587-
result = idx - 2.
588-
assert isinstance(result, Float64Index)
589-
exp = Float64Index([-1., 0., 1., 2., 3.], name='x')
590-
tm.assert_index_equal(result, exp)
591-
592-
result = idx * 1.
593-
assert isinstance(result, Float64Index)
594-
exp = Float64Index([1., 2., 3., 4., 5.], name='x')
595-
tm.assert_index_equal(result, exp)
596-
597-
result = idx / 2.
598-
assert isinstance(result, Float64Index)
599-
exp = Float64Index([0.5, 1., 1.5, 2., 2.5], name='x')
600-
tm.assert_index_equal(result, exp)
601-
602568

603569
class TestInt64Index(NumericInt):
604570
_dtype = 'int64'

pandas/tests/indexes/timedeltas/test_arithmetic.py

-32
Original file line numberDiff line numberDiff line change
@@ -430,38 +430,6 @@ def test_ops_ndarray(self):
430430
if LooseVersion(np.__version__) >= LooseVersion('1.8'):
431431
tm.assert_numpy_array_equal(other - td, expected)
432432

433-
def test_ops_series_object(self):
434-
# GH 13043
435-
s = pd.Series([pd.Timestamp('2015-01-01', tz='US/Eastern'),
436-
pd.Timestamp('2015-01-01', tz='Asia/Tokyo')],
437-
name='xxx')
438-
assert s.dtype == object
439-
440-
exp = pd.Series([pd.Timestamp('2015-01-02', tz='US/Eastern'),
441-
pd.Timestamp('2015-01-02', tz='Asia/Tokyo')],
442-
name='xxx')
443-
tm.assert_series_equal(s + pd.Timedelta('1 days'), exp)
444-
tm.assert_series_equal(pd.Timedelta('1 days') + s, exp)
445-
446-
# object series & object series
447-
s2 = pd.Series([pd.Timestamp('2015-01-03', tz='US/Eastern'),
448-
pd.Timestamp('2015-01-05', tz='Asia/Tokyo')],
449-
name='xxx')
450-
assert s2.dtype == object
451-
exp = pd.Series([pd.Timedelta('2 days'), pd.Timedelta('4 days')],
452-
name='xxx')
453-
tm.assert_series_equal(s2 - s, exp)
454-
tm.assert_series_equal(s - s2, -exp)
455-
456-
s = pd.Series([pd.Timedelta('01:00:00'), pd.Timedelta('02:00:00')],
457-
name='xxx', dtype=object)
458-
assert s.dtype == object
459-
460-
exp = pd.Series([pd.Timedelta('01:30:00'), pd.Timedelta('02:30:00')],
461-
name='xxx')
462-
tm.assert_series_equal(s + pd.Timedelta('00:30:00'), exp)
463-
tm.assert_series_equal(pd.Timedelta('00:30:00') + s, exp)
464-
465433
def test_timedelta_ops_with_missing_values(self):
466434
# setup
467435
s1 = pd.to_timedelta(Series(['00:00:01']))

pandas/tests/series/test_arithmetic.py

-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import operator
33

4-
import numpy as np
54
import pytest
65

76
from pandas import Series
@@ -14,13 +13,6 @@
1413
# Comparisons
1514

1615
class TestSeriesComparison(object):
17-
def test_compare_invalid(self):
18-
# GH#8058
19-
# ops testing
20-
a = pd.Series(np.random.randn(5), name=0)
21-
b = pd.Series(np.random.randn(5))
22-
b.name = pd.Timestamp('2000-01-01')
23-
tm.assert_series_equal(a / b, 1 / (b / a))
2416

2517
@pytest.mark.parametrize('opname', ['eq', 'ne', 'gt', 'lt', 'ge', 'le'])
2618
def test_ser_flex_cmp_return_dtypes(self, opname):

0 commit comments

Comments
 (0)