Skip to content

Commit

Permalink
TST: Continue collecting arithmetic tests (#22559)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Sep 8, 2018
1 parent 8078500 commit 9b2e6db
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 90 deletions.
69 changes: 69 additions & 0 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ def test_operator_series_comparison_zerorank(self):
expected = 0.0 > pd.Series([1, 2, 3])
tm.assert_series_equal(result, expected)

def test_df_numeric_cmp_dt64_raises(self):
# GH#8932, GH#22163
ts = pd.Timestamp.now()
df = pd.DataFrame({'x': range(5)})
with pytest.raises(TypeError):
df > ts
with pytest.raises(TypeError):
df < ts
with pytest.raises(TypeError):
ts < df
with pytest.raises(TypeError):
ts > df

assert not (df == ts).any().any()
assert (df != ts).all().all()

def test_compare_invalid(self):
# GH#8058
# ops testing
a = pd.Series(np.random.randn(5), name=0)
b = pd.Series(np.random.randn(5))
b.name = pd.Timestamp('2000-01-01')
tm.assert_series_equal(a / b, 1 / (b / a))


# ------------------------------------------------------------------
# Numeric dtypes Arithmetic with Timedelta Scalar
Expand Down Expand Up @@ -754,6 +778,51 @@ def check(series, other):
check(tser, 5)


class TestUFuncCompat(object):
@pytest.mark.parametrize('holder', [pd.Int64Index, pd.UInt64Index,
pd.Float64Index, pd.Series])
def test_ufunc_coercions(self, holder):
idx = holder([1, 2, 3, 4, 5], name='x')
box = pd.Series if holder is pd.Series else pd.Index

result = np.sqrt(idx)
assert result.dtype == 'f8' and isinstance(result, box)
exp = pd.Float64Index(np.sqrt(np.array([1, 2, 3, 4, 5])), name='x')
exp = tm.box_expected(exp, box)
tm.assert_equal(result, exp)

result = np.divide(idx, 2.)
assert result.dtype == 'f8' and isinstance(result, box)
exp = pd.Float64Index([0.5, 1., 1.5, 2., 2.5], name='x')
exp = tm.box_expected(exp, box)
tm.assert_equal(result, exp)

# _evaluate_numeric_binop
result = idx + 2.
assert result.dtype == 'f8' and isinstance(result, box)
exp = pd.Float64Index([3., 4., 5., 6., 7.], name='x')
exp = tm.box_expected(exp, box)
tm.assert_equal(result, exp)

result = idx - 2.
assert result.dtype == 'f8' and isinstance(result, box)
exp = pd.Float64Index([-1., 0., 1., 2., 3.], name='x')
exp = tm.box_expected(exp, box)
tm.assert_equal(result, exp)

result = idx * 1.
assert result.dtype == 'f8' and isinstance(result, box)
exp = pd.Float64Index([1., 2., 3., 4., 5.], name='x')
exp = tm.box_expected(exp, box)
tm.assert_equal(result, exp)

result = idx / 2.
assert result.dtype == 'f8' and isinstance(result, box)
exp = pd.Float64Index([0.5, 1., 1.5, 2., 2.5], name='x')
exp = tm.box_expected(exp, box)
tm.assert_equal(result, exp)


class TestObjectDtypeEquivalence(object):
# Tests that arithmetic operations match operations executed elementwise

Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/arithmetic/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,36 @@ def test_series_with_dtype_radd_timedelta(self, dtype):

result = ser + pd.Timedelta('3 days')
tm.assert_series_equal(result, expected)

# TODO: cleanup & parametrize over box
def test_mixed_timezone_series_ops_object(self):
# GH#13043
ser = pd.Series([pd.Timestamp('2015-01-01', tz='US/Eastern'),
pd.Timestamp('2015-01-01', tz='Asia/Tokyo')],
name='xxx')
assert ser.dtype == object

exp = pd.Series([pd.Timestamp('2015-01-02', tz='US/Eastern'),
pd.Timestamp('2015-01-02', tz='Asia/Tokyo')],
name='xxx')
tm.assert_series_equal(ser + pd.Timedelta('1 days'), exp)
tm.assert_series_equal(pd.Timedelta('1 days') + ser, exp)

# object series & object series
ser2 = pd.Series([pd.Timestamp('2015-01-03', tz='US/Eastern'),
pd.Timestamp('2015-01-05', tz='Asia/Tokyo')],
name='xxx')
assert ser2.dtype == object
exp = pd.Series([pd.Timedelta('2 days'), pd.Timedelta('4 days')],
name='xxx')
tm.assert_series_equal(ser2 - ser, exp)
tm.assert_series_equal(ser - ser2, -exp)

ser = pd.Series([pd.Timedelta('01:00:00'), pd.Timedelta('02:00:00')],
name='xxx', dtype=object)
assert ser.dtype == object

exp = pd.Series([pd.Timedelta('01:30:00'), pd.Timedelta('02:30:00')],
name='xxx')
tm.assert_series_equal(ser + pd.Timedelta('00:30:00'), exp)
tm.assert_series_equal(pd.Timedelta('00:30:00') + ser, exp)
File renamed without changes.
16 changes: 0 additions & 16 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,6 @@ def test_mixed_comparison(self):
result = df != other
assert result.all().all()

def test_df_numeric_cmp_dt64_raises(self):
# GH#8932, GH#22163
ts = pd.Timestamp.now()
df = pd.DataFrame({'x': range(5)})
with pytest.raises(TypeError):
df > ts
with pytest.raises(TypeError):
df < ts
with pytest.raises(TypeError):
ts < df
with pytest.raises(TypeError):
ts > df

assert not (df == ts).any().any()
assert (df != ts).all().all()

def test_df_boolean_comparison_error(self):
# GH#4576
# boolean comparisons with a tuple/list give unexpected results
Expand Down
34 changes: 0 additions & 34 deletions pandas/tests/indexes/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,40 +565,6 @@ def test_slice_keep_name(self):
idx = self._holder([1, 2], name='asdf')
assert idx.name == idx[1:].name

def test_ufunc_coercions(self):
idx = self._holder([1, 2, 3, 4, 5], name='x')

result = np.sqrt(idx)
assert isinstance(result, Float64Index)
exp = Float64Index(np.sqrt(np.array([1, 2, 3, 4, 5])), name='x')
tm.assert_index_equal(result, exp)

result = np.divide(idx, 2.)
assert isinstance(result, Float64Index)
exp = Float64Index([0.5, 1., 1.5, 2., 2.5], name='x')
tm.assert_index_equal(result, exp)

# _evaluate_numeric_binop
result = idx + 2.
assert isinstance(result, Float64Index)
exp = Float64Index([3., 4., 5., 6., 7.], name='x')
tm.assert_index_equal(result, exp)

result = idx - 2.
assert isinstance(result, Float64Index)
exp = Float64Index([-1., 0., 1., 2., 3.], name='x')
tm.assert_index_equal(result, exp)

result = idx * 1.
assert isinstance(result, Float64Index)
exp = Float64Index([1., 2., 3., 4., 5.], name='x')
tm.assert_index_equal(result, exp)

result = idx / 2.
assert isinstance(result, Float64Index)
exp = Float64Index([0.5, 1., 1.5, 2., 2.5], name='x')
tm.assert_index_equal(result, exp)


class TestInt64Index(NumericInt):
_dtype = 'int64'
Expand Down
32 changes: 0 additions & 32 deletions pandas/tests/indexes/timedeltas/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,38 +430,6 @@ def test_ops_ndarray(self):
if LooseVersion(np.__version__) >= LooseVersion('1.8'):
tm.assert_numpy_array_equal(other - td, expected)

def test_ops_series_object(self):
# GH 13043
s = pd.Series([pd.Timestamp('2015-01-01', tz='US/Eastern'),
pd.Timestamp('2015-01-01', tz='Asia/Tokyo')],
name='xxx')
assert s.dtype == object

exp = pd.Series([pd.Timestamp('2015-01-02', tz='US/Eastern'),
pd.Timestamp('2015-01-02', tz='Asia/Tokyo')],
name='xxx')
tm.assert_series_equal(s + pd.Timedelta('1 days'), exp)
tm.assert_series_equal(pd.Timedelta('1 days') + s, exp)

# object series & object series
s2 = pd.Series([pd.Timestamp('2015-01-03', tz='US/Eastern'),
pd.Timestamp('2015-01-05', tz='Asia/Tokyo')],
name='xxx')
assert s2.dtype == object
exp = pd.Series([pd.Timedelta('2 days'), pd.Timedelta('4 days')],
name='xxx')
tm.assert_series_equal(s2 - s, exp)
tm.assert_series_equal(s - s2, -exp)

s = pd.Series([pd.Timedelta('01:00:00'), pd.Timedelta('02:00:00')],
name='xxx', dtype=object)
assert s.dtype == object

exp = pd.Series([pd.Timedelta('01:30:00'), pd.Timedelta('02:30:00')],
name='xxx')
tm.assert_series_equal(s + pd.Timedelta('00:30:00'), exp)
tm.assert_series_equal(pd.Timedelta('00:30:00') + s, exp)

def test_timedelta_ops_with_missing_values(self):
# setup
s1 = pd.to_timedelta(Series(['00:00:01']))
Expand Down
8 changes: 0 additions & 8 deletions pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
import operator

import numpy as np
import pytest

from pandas import Series
Expand All @@ -14,13 +13,6 @@
# Comparisons

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

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

0 comments on commit 9b2e6db

Please sign in to comment.