-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
centralize and split frame division tests #19527
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import pytest | ||
import numpy as np | ||
|
||
from pandas.compat import range | ||
|
||
import pandas as pd | ||
import pandas.util.testing as tm | ||
|
||
|
@@ -58,10 +59,129 @@ def test_df_flex_cmp_constant_return_types_empty(self, opname): | |
result = getattr(empty, opname)(const).get_dtype_counts() | ||
tm.assert_series_equal(result, pd.Series([2], ['bool'])) | ||
|
||
@pytest.mark.parametrize('timestamps', [ | ||
[pd.Timestamp('2012-01-01 13:00:00+00:00')] * 2, | ||
[pd.Timestamp('2012-01-01 13:00:00')] * 2]) | ||
def test_tz_aware_scalar_comparison(self, timestamps): | ||
# Test for issue #15966 | ||
df = pd.DataFrame({'test': timestamps}) | ||
expected = pd.DataFrame({'test': [False, False]}) | ||
tm.assert_frame_equal(df == -1, expected) | ||
|
||
|
||
# ------------------------------------------------------------------- | ||
# Arithmetic | ||
|
||
class TestFrameMulDiv(object): | ||
"""Tests for DataFrame multiplication and division""" | ||
# ------------------------------------------------------------------ | ||
# Mod By Zero | ||
|
||
def test_df_mod_zero_df(self): | ||
# GH#3590, modulo as ints | ||
df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}) | ||
|
||
# this is technically wrong as the integer portion is coerced to float | ||
# ### | ||
first = pd.Series([0, 0, 0, 0], dtype='float64') | ||
second = pd.Series([np.nan, np.nan, np.nan, 0]) | ||
expected = pd.DataFrame({'first': first, 'second': second}) | ||
result = df % df | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_df_mod_zero_array(self): | ||
# GH#3590, modulo as ints | ||
df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}) | ||
|
||
# this is technically wrong as the integer portion is coerced to float | ||
# ### | ||
first = pd.Series([0, 0, 0, 0], dtype='float64') | ||
second = pd.Series([np.nan, np.nan, np.nan, 0]) | ||
expected = pd.DataFrame({'first': first, 'second': second}) | ||
|
||
# numpy has a slightly different (wrong) treatement | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. treatement --> treatment (fix this everywhere) |
||
with np.errstate(all='ignore'): | ||
arr = df.values % df.values | ||
result2 = pd.DataFrame(arr, index=df.index, | ||
columns=df.columns, dtype='float64') | ||
result2.iloc[0:3, 1] = np.nan | ||
tm.assert_frame_equal(result2, expected) | ||
|
||
def test_df_mod_zero_int(self): | ||
# GH#3590, modulo as ints | ||
df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}) | ||
|
||
result = df % 0 | ||
expected = pd.DataFrame(np.nan, index=df.index, columns=df.columns) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# numpy has a slightly different (wrong) treatement | ||
with np.errstate(all='ignore'): | ||
arr = df.values.astype('float64') % 0 | ||
result2 = pd.DataFrame(arr, index=df.index, columns=df.columns) | ||
tm.assert_frame_equal(result2, expected) | ||
|
||
def test_df_mod_zero_series_does_not_commute(self): | ||
# GH#3590, modulo as ints | ||
# not commutative with series | ||
df = pd.DataFrame(np.random.randn(10, 5)) | ||
ser = df[0] | ||
res = ser % df | ||
res2 = df % ser | ||
assert not res.fillna(0).equals(res2.fillna(0)) | ||
|
||
# ------------------------------------------------------------------ | ||
# Division By Zero | ||
|
||
def test_df_div_zero_df(self): | ||
# integer div, but deal with the 0's (GH#9144) | ||
df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}) | ||
result = df / df | ||
|
||
first = pd.Series([1.0, 1.0, 1.0, 1.0]) | ||
second = pd.Series([np.nan, np.nan, np.nan, 1]) | ||
expected = pd.DataFrame({'first': first, 'second': second}) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_df_div_zero_array(self): | ||
# integer div, but deal with the 0's (GH#9144) | ||
df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}) | ||
|
||
first = pd.Series([1.0, 1.0, 1.0, 1.0]) | ||
second = pd.Series([np.nan, np.nan, np.nan, 1]) | ||
expected = pd.DataFrame({'first': first, 'second': second}) | ||
|
||
with np.errstate(all='ignore'): | ||
arr = df.values.astype('float') / df.values | ||
result = pd.DataFrame(arr, index=df.index, | ||
columns=df.columns) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_df_div_zero_int(self): | ||
# integer div, but deal with the 0's (GH#9144) | ||
df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}) | ||
|
||
result = df / 0 | ||
expected = pd.DataFrame(np.inf, index=df.index, columns=df.columns) | ||
expected.iloc[0:3, 1] = np.nan | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# numpy has a slightly different (wrong) treatement | ||
with np.errstate(all='ignore'): | ||
arr = df.values.astype('float64') / 0 | ||
result2 = pd.DataFrame(arr, index=df.index, | ||
columns=df.columns) | ||
tm.assert_frame_equal(result2, expected) | ||
|
||
def test_df_div_zero_series_does_not_commute(self): | ||
# integer div, but deal with the 0's (GH#9144) | ||
df = pd.DataFrame(np.random.randn(10, 5)) | ||
ser = df[0] | ||
res = ser / df | ||
res2 = df / ser | ||
assert not res.fillna(0).equals(res2.fillna(0)) | ||
|
||
|
||
class TestFrameArithmetic(object): | ||
|
||
@pytest.mark.xfail(reason='GH#7996 datetime64 units not converted to nano') | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comma between "wrong" and "as"