Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: Continue collecting arithmetic tests #22559

Merged
merged 2 commits into from
Sep 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't these be in test_datetime64?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is specifically testing that the numeric-dtypes are correctly handling the presence of datetime64-dtype, not vice-versa.

ts < df
with pytest.raises(TypeError):
ts > df

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add for tz as well (use the fixture)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure; follow-up. This PR is pretty specifically only-moving.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there timedelta tests that should be moved (ok for followup, just indicate)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those should all be in test_arithmetic, which this PR moves to arithmetic.test_timedelta64

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)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would move these to test_datetime64 (maybe rename that to test_datetime)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are mixed-type; it's pretty specifically the object-dtyped Index/Series/Column that these tests are addressing.

# 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)
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