|
1 | 1 | # -*- coding: utf-8 -*-
|
2 |
| - |
3 | 2 | import pytest
|
4 | 3 | import numpy as np
|
5 | 4 |
|
| 5 | +from pandas.compat import range |
| 6 | + |
6 | 7 | import pandas as pd
|
7 | 8 | import pandas.util.testing as tm
|
8 | 9 |
|
@@ -58,10 +59,129 @@ def test_df_flex_cmp_constant_return_types_empty(self, opname):
|
58 | 59 | result = getattr(empty, opname)(const).get_dtype_counts()
|
59 | 60 | tm.assert_series_equal(result, pd.Series([2], ['bool']))
|
60 | 61 |
|
| 62 | + @pytest.mark.parametrize('timestamps', [ |
| 63 | + [pd.Timestamp('2012-01-01 13:00:00+00:00')] * 2, |
| 64 | + [pd.Timestamp('2012-01-01 13:00:00')] * 2]) |
| 65 | + def test_tz_aware_scalar_comparison(self, timestamps): |
| 66 | + # Test for issue #15966 |
| 67 | + df = pd.DataFrame({'test': timestamps}) |
| 68 | + expected = pd.DataFrame({'test': [False, False]}) |
| 69 | + tm.assert_frame_equal(df == -1, expected) |
| 70 | + |
61 | 71 |
|
62 | 72 | # -------------------------------------------------------------------
|
63 | 73 | # Arithmetic
|
64 | 74 |
|
| 75 | +class TestFrameMulDiv(object): |
| 76 | + """Tests for DataFrame multiplication and division""" |
| 77 | + # ------------------------------------------------------------------ |
| 78 | + # Mod By Zero |
| 79 | + |
| 80 | + def test_df_mod_zero_df(self): |
| 81 | + # GH#3590, modulo as ints |
| 82 | + df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}) |
| 83 | + |
| 84 | + # this is technically wrong, as the integer portion is coerced to float |
| 85 | + # ### |
| 86 | + first = pd.Series([0, 0, 0, 0], dtype='float64') |
| 87 | + second = pd.Series([np.nan, np.nan, np.nan, 0]) |
| 88 | + expected = pd.DataFrame({'first': first, 'second': second}) |
| 89 | + result = df % df |
| 90 | + tm.assert_frame_equal(result, expected) |
| 91 | + |
| 92 | + def test_df_mod_zero_array(self): |
| 93 | + # GH#3590, modulo as ints |
| 94 | + df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}) |
| 95 | + |
| 96 | + # this is technically wrong, as the integer portion is coerced to float |
| 97 | + # ### |
| 98 | + first = pd.Series([0, 0, 0, 0], dtype='float64') |
| 99 | + second = pd.Series([np.nan, np.nan, np.nan, 0]) |
| 100 | + expected = pd.DataFrame({'first': first, 'second': second}) |
| 101 | + |
| 102 | + # numpy has a slightly different (wrong) treatment |
| 103 | + with np.errstate(all='ignore'): |
| 104 | + arr = df.values % df.values |
| 105 | + result2 = pd.DataFrame(arr, index=df.index, |
| 106 | + columns=df.columns, dtype='float64') |
| 107 | + result2.iloc[0:3, 1] = np.nan |
| 108 | + tm.assert_frame_equal(result2, expected) |
| 109 | + |
| 110 | + def test_df_mod_zero_int(self): |
| 111 | + # GH#3590, modulo as ints |
| 112 | + df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}) |
| 113 | + |
| 114 | + result = df % 0 |
| 115 | + expected = pd.DataFrame(np.nan, index=df.index, columns=df.columns) |
| 116 | + tm.assert_frame_equal(result, expected) |
| 117 | + |
| 118 | + # numpy has a slightly different (wrong) treatment |
| 119 | + with np.errstate(all='ignore'): |
| 120 | + arr = df.values.astype('float64') % 0 |
| 121 | + result2 = pd.DataFrame(arr, index=df.index, columns=df.columns) |
| 122 | + tm.assert_frame_equal(result2, expected) |
| 123 | + |
| 124 | + def test_df_mod_zero_series_does_not_commute(self): |
| 125 | + # GH#3590, modulo as ints |
| 126 | + # not commutative with series |
| 127 | + df = pd.DataFrame(np.random.randn(10, 5)) |
| 128 | + ser = df[0] |
| 129 | + res = ser % df |
| 130 | + res2 = df % ser |
| 131 | + assert not res.fillna(0).equals(res2.fillna(0)) |
| 132 | + |
| 133 | + # ------------------------------------------------------------------ |
| 134 | + # Division By Zero |
| 135 | + |
| 136 | + def test_df_div_zero_df(self): |
| 137 | + # integer div, but deal with the 0's (GH#9144) |
| 138 | + df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}) |
| 139 | + result = df / df |
| 140 | + |
| 141 | + first = pd.Series([1.0, 1.0, 1.0, 1.0]) |
| 142 | + second = pd.Series([np.nan, np.nan, np.nan, 1]) |
| 143 | + expected = pd.DataFrame({'first': first, 'second': second}) |
| 144 | + tm.assert_frame_equal(result, expected) |
| 145 | + |
| 146 | + def test_df_div_zero_array(self): |
| 147 | + # integer div, but deal with the 0's (GH#9144) |
| 148 | + df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}) |
| 149 | + |
| 150 | + first = pd.Series([1.0, 1.0, 1.0, 1.0]) |
| 151 | + second = pd.Series([np.nan, np.nan, np.nan, 1]) |
| 152 | + expected = pd.DataFrame({'first': first, 'second': second}) |
| 153 | + |
| 154 | + with np.errstate(all='ignore'): |
| 155 | + arr = df.values.astype('float') / df.values |
| 156 | + result = pd.DataFrame(arr, index=df.index, |
| 157 | + columns=df.columns) |
| 158 | + tm.assert_frame_equal(result, expected) |
| 159 | + |
| 160 | + def test_df_div_zero_int(self): |
| 161 | + # integer div, but deal with the 0's (GH#9144) |
| 162 | + df = pd.DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]}) |
| 163 | + |
| 164 | + result = df / 0 |
| 165 | + expected = pd.DataFrame(np.inf, index=df.index, columns=df.columns) |
| 166 | + expected.iloc[0:3, 1] = np.nan |
| 167 | + tm.assert_frame_equal(result, expected) |
| 168 | + |
| 169 | + # numpy has a slightly different (wrong) treatment |
| 170 | + with np.errstate(all='ignore'): |
| 171 | + arr = df.values.astype('float64') / 0 |
| 172 | + result2 = pd.DataFrame(arr, index=df.index, |
| 173 | + columns=df.columns) |
| 174 | + tm.assert_frame_equal(result2, expected) |
| 175 | + |
| 176 | + def test_df_div_zero_series_does_not_commute(self): |
| 177 | + # integer div, but deal with the 0's (GH#9144) |
| 178 | + df = pd.DataFrame(np.random.randn(10, 5)) |
| 179 | + ser = df[0] |
| 180 | + res = ser / df |
| 181 | + res2 = df / ser |
| 182 | + assert not res.fillna(0).equals(res2.fillna(0)) |
| 183 | + |
| 184 | + |
65 | 185 | class TestFrameArithmetic(object):
|
66 | 186 |
|
67 | 187 | @pytest.mark.xfail(reason='GH#7996 datetime64 units not converted to nano')
|
|
0 commit comments