6
6
7
7
from pandas .compat import range
8
8
9
- import pandas as pd
9
+ from pandas import (DataFrame , Series , Timedelta ,
10
+ date_range , timedelta_range , NaT )
11
+
10
12
import pandas .util .testing as tm
11
13
12
14
from pandas .tests .frame .common import _check_mixed_float , _check_mixed_int
17
19
18
20
class TestFrameComparisons (object ):
19
21
def test_flex_comparison_nat (self ):
20
- # GH#15697, GH#22163 df.eq(pd. NaT) should behave like df == pd. NaT,
22
+ # GH#15697, GH#22163 df.eq(NaT) should behave like df == NaT,
21
23
# and _definitely_ not be NaN
22
- df = pd . DataFrame ([pd . NaT ])
24
+ df = DataFrame ([NaT ])
23
25
24
- result = df == pd . NaT
26
+ result = df == NaT
25
27
# result.iloc[0, 0] is a np.bool_ object
26
28
assert result .iloc [0 , 0 ].item () is False
27
29
28
- result = df .eq (pd . NaT )
30
+ result = df .eq (NaT )
29
31
assert result .iloc [0 , 0 ].item () is False
30
32
31
- result = df != pd . NaT
33
+ result = df != NaT
32
34
assert result .iloc [0 , 0 ].item () is True
33
35
34
- result = df .ne (pd . NaT )
36
+ result = df .ne (NaT )
35
37
assert result .iloc [0 , 0 ].item () is True
36
38
37
39
def test_mixed_comparison (self ):
38
40
# GH#13128, GH#22163 != datetime64 vs non-dt64 should be False,
39
41
# not raise TypeError
40
42
# (this appears to be fixed before #22163, not sure when)
41
- df = pd . DataFrame ([['1989-08-01' , 1 ], ['1989-08-01' , 2 ]])
42
- other = pd . DataFrame ([['a' , 'b' ], ['c' , 'd' ]])
43
+ df = DataFrame ([['1989-08-01' , 1 ], ['1989-08-01' , 2 ]])
44
+ other = DataFrame ([['a' , 'b' ], ['c' , 'd' ]])
43
45
44
46
result = df == other
45
47
assert not result .any ().any ()
@@ -50,7 +52,7 @@ def test_mixed_comparison(self):
50
52
def test_df_boolean_comparison_error (self ):
51
53
# GH#4576
52
54
# boolean comparisons with a tuple/list give unexpected results
53
- df = pd . DataFrame (np .arange (6 ).reshape ((3 , 2 )))
55
+ df = DataFrame (np .arange (6 ).reshape ((3 , 2 )))
54
56
55
57
# not shape compatible
56
58
with pytest .raises (ValueError ):
@@ -59,14 +61,14 @@ def test_df_boolean_comparison_error(self):
59
61
df == [2 , 2 ]
60
62
61
63
def test_df_float_none_comparison (self ):
62
- df = pd . DataFrame (np .random .randn (8 , 3 ), index = range (8 ),
63
- columns = ['A' , 'B' , 'C' ])
64
+ df = DataFrame (np .random .randn (8 , 3 ), index = range (8 ),
65
+ columns = ['A' , 'B' , 'C' ])
64
66
65
67
result = df .__eq__ (None )
66
68
assert not result .any ().any ()
67
69
68
70
def test_df_string_comparison (self ):
69
- df = pd . DataFrame ([{"a" : 1 , "b" : "foo" }, {"a" : 2 , "b" : "bar" }])
71
+ df = DataFrame ([{"a" : 1 , "b" : "foo" }, {"a" : 2 , "b" : "bar" }])
70
72
mask_a = df .a > 1
71
73
tm .assert_frame_equal (df [mask_a ], df .loc [1 :1 , :])
72
74
tm .assert_frame_equal (df [- mask_a ], df .loc [0 :0 , :])
@@ -78,21 +80,21 @@ def test_df_string_comparison(self):
78
80
@pytest .mark .parametrize ('opname' , ['eq' , 'ne' , 'gt' , 'lt' , 'ge' , 'le' ])
79
81
def test_df_flex_cmp_constant_return_types (self , opname ):
80
82
# GH#15077, non-empty DataFrame
81
- df = pd . DataFrame ({'x' : [1 , 2 , 3 ], 'y' : [1. , 2. , 3. ]})
83
+ df = DataFrame ({'x' : [1 , 2 , 3 ], 'y' : [1. , 2. , 3. ]})
82
84
const = 2
83
85
84
86
result = getattr (df , opname )(const ).get_dtype_counts ()
85
- tm .assert_series_equal (result , pd . Series ([2 ], ['bool' ]))
87
+ tm .assert_series_equal (result , Series ([2 ], ['bool' ]))
86
88
87
89
@pytest .mark .parametrize ('opname' , ['eq' , 'ne' , 'gt' , 'lt' , 'ge' , 'le' ])
88
90
def test_df_flex_cmp_constant_return_types_empty (self , opname ):
89
91
# GH#15077 empty DataFrame
90
- df = pd . DataFrame ({'x' : [1 , 2 , 3 ], 'y' : [1. , 2. , 3. ]})
92
+ df = DataFrame ({'x' : [1 , 2 , 3 ], 'y' : [1. , 2. , 3. ]})
91
93
const = 2
92
94
93
95
empty = df .iloc [:0 ]
94
96
result = getattr (empty , opname )(const ).get_dtype_counts ()
95
- tm .assert_series_equal (result , pd . Series ([2 ], ['bool' ]))
97
+ tm .assert_series_equal (result , Series ([2 ], ['bool' ]))
96
98
97
99
98
100
# -------------------------------------------------------------------
@@ -101,28 +103,27 @@ def test_df_flex_cmp_constant_return_types_empty(self, opname):
101
103
class TestFrameFlexArithmetic (object ):
102
104
def test_df_add_td64_columnwise (self ):
103
105
# GH#22534 Check that column-wise addition broadcasts correctly
104
- dti = pd . date_range ('2016-01-01' , periods = 10 )
105
- tdi = pd . timedelta_range ('1' , periods = 10 )
106
- tser = pd . Series (tdi )
107
- df = pd . DataFrame ({0 : dti , 1 : tdi })
106
+ dti = date_range ('2016-01-01' , periods = 10 )
107
+ tdi = timedelta_range ('1' , periods = 10 )
108
+ tser = Series (tdi )
109
+ df = DataFrame ({0 : dti , 1 : tdi })
108
110
109
111
result = df .add (tser , axis = 0 )
110
- expected = pd .DataFrame ({0 : dti + tdi ,
111
- 1 : tdi + tdi })
112
+ expected = DataFrame ({0 : dti + tdi , 1 : tdi + tdi })
112
113
tm .assert_frame_equal (result , expected )
113
114
114
115
def test_df_add_flex_filled_mixed_dtypes (self ):
115
116
# GH#19611
116
- dti = pd . date_range ('2016-01-01' , periods = 3 )
117
- ser = pd . Series (['1 Day' , 'NaT' , '2 Days' ], dtype = 'timedelta64[ns]' )
118
- df = pd . DataFrame ({'A' : dti , 'B' : ser })
119
- other = pd . DataFrame ({'A' : ser , 'B' : ser })
120
- fill = pd . Timedelta (days = 1 ).to_timedelta64 ()
117
+ dti = date_range ('2016-01-01' , periods = 3 )
118
+ ser = Series (['1 Day' , 'NaT' , '2 Days' ], dtype = 'timedelta64[ns]' )
119
+ df = DataFrame ({'A' : dti , 'B' : ser })
120
+ other = DataFrame ({'A' : ser , 'B' : ser })
121
+ fill = Timedelta (days = 1 ).to_timedelta64 ()
121
122
result = df .add (other , fill_value = fill )
122
123
123
- expected = pd . DataFrame (
124
- {'A' : pd . Series (['2016-01-02' , '2016-01-03' , '2016-01-05' ],
125
- dtype = 'datetime64[ns]' ),
124
+ expected = DataFrame (
125
+ {'A' : Series (['2016-01-02' , '2016-01-03' , '2016-01-05' ],
126
+ dtype = 'datetime64[ns]' ),
126
127
'B' : ser * 2 })
127
128
tm .assert_frame_equal (result , expected )
128
129
@@ -225,22 +226,22 @@ def test_arith_flex_series(self, simple_frame):
225
226
tm .assert_frame_equal (df .div (col , axis = 0 ), (df .T / col ).T )
226
227
227
228
# broadcasting issue in GH#7325
228
- df = pd . DataFrame (np .arange (3 * 2 ).reshape ((3 , 2 )), dtype = 'int64' )
229
- expected = pd . DataFrame ([[np .nan , np .inf ], [1.0 , 1.5 ], [1.0 , 1.25 ]])
229
+ df = DataFrame (np .arange (3 * 2 ).reshape ((3 , 2 )), dtype = 'int64' )
230
+ expected = DataFrame ([[np .nan , np .inf ], [1.0 , 1.5 ], [1.0 , 1.25 ]])
230
231
result = df .div (df [0 ], axis = 'index' )
231
232
tm .assert_frame_equal (result , expected )
232
233
233
- df = pd . DataFrame (np .arange (3 * 2 ).reshape ((3 , 2 )), dtype = 'float64' )
234
- expected = pd . DataFrame ([[np .nan , np .inf ], [1.0 , 1.5 ], [1.0 , 1.25 ]])
234
+ df = DataFrame (np .arange (3 * 2 ).reshape ((3 , 2 )), dtype = 'float64' )
235
+ expected = DataFrame ([[np .nan , np .inf ], [1.0 , 1.5 ], [1.0 , 1.25 ]])
235
236
result = df .div (df [0 ], axis = 'index' )
236
237
tm .assert_frame_equal (result , expected )
237
238
238
239
def test_arith_flex_zero_len_raises (self ):
239
240
# GH#19522 passing fill_value to frame flex arith methods should
240
241
# raise even in the zero-length special cases
241
- ser_len0 = pd . Series ([])
242
- df_len0 = pd . DataFrame ([], columns = ['A' , 'B' ])
243
- df = pd . DataFrame ([[1 , 2 ], [3 , 4 ]], columns = ['A' , 'B' ])
242
+ ser_len0 = Series ([])
243
+ df_len0 = DataFrame ([], columns = ['A' , 'B' ])
244
+ df = DataFrame ([[1 , 2 ], [3 , 4 ]], columns = ['A' , 'B' ])
244
245
245
246
with tm .assert_raises_regex (NotImplementedError , 'fill_value' ):
246
247
df .add (ser_len0 , fill_value = 'E' )
@@ -253,7 +254,7 @@ class TestFrameArithmetic(object):
253
254
def test_df_bool_mul_int (self ):
254
255
# GH#22047, GH#22163 multiplication by 1 should result in int dtype,
255
256
# not object dtype
256
- df = pd . DataFrame ([[False , True ], [False , False ]])
257
+ df = DataFrame ([[False , True ], [False , False ]])
257
258
result = df * 1
258
259
259
260
# On appveyor this comes back as np.int32 instead of np.int64,
0 commit comments