@@ -42,6 +42,30 @@ def test_operator_series_comparison_zerorank(self):
42
42
expected = 0.0 > pd .Series ([1 , 2 , 3 ])
43
43
tm .assert_series_equal (result , expected )
44
44
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
+
45
69
46
70
# ------------------------------------------------------------------
47
71
# Numeric dtypes Arithmetic with Timedelta Scalar
@@ -754,6 +778,51 @@ def check(series, other):
754
778
check (tser , 5 )
755
779
756
780
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
+
757
826
class TestObjectDtypeEquivalence (object ):
758
827
# Tests that arithmetic operations match operations executed elementwise
759
828
0 commit comments