39
39
from pandas .util import testing as tm
40
40
41
41
42
- @pytest .fixture (params = [True , False ], ids = lambda val : str ( val ) )
42
+ @pytest .fixture (params = [True , False ], ids = str )
43
43
def coerce (request ):
44
44
return request .param
45
45
@@ -60,25 +60,29 @@ def __getitem__(self):
60
60
assert (not is_seq (A ()))
61
61
62
62
63
- def test_is_list_like ():
64
- passes = ([], [1 ], (1 , ), (1 , 2 ), {'a' : 1 }, set ([1 , 'a' ]), Series ([1 ]),
65
- Series ([]), Series (['a' ]).str )
66
- fails = (1 , '2' , object (), str )
63
+ @pytest .mark .parametrize (
64
+ "ll" ,
65
+ [
66
+ [], [1 ], (1 , ), (1 , 2 ), {'a' : 1 },
67
+ set ([1 , 'a' ]), Series ([1 ]),
68
+ Series ([]), Series (['a' ]).str ])
69
+ def test_is_list_like_passes (ll ):
70
+ assert inference .is_list_like (ll )
67
71
68
- for p in passes :
69
- assert inference .is_list_like (p )
70
72
71
- for f in fails :
72
- assert not inference .is_list_like (f )
73
+ @pytest .mark .parametrize (
74
+ "ll" , [1 , '2' , object (), str ])
75
+ def test_is_list_like_fails (ll ):
76
+ assert not inference .is_list_like (ll )
73
77
74
78
75
79
@pytest .mark .parametrize ('inner' , [
76
80
[], [1 ], (1 , ), (1 , 2 ), {'a' : 1 }, set ([1 , 'a' ]), Series ([1 ]),
77
81
Series ([]), Series (['a' ]).str , (x for x in range (5 ))
78
- ])
82
+ ], ids = str )
79
83
@pytest .mark .parametrize ('outer' , [
80
84
list , Series , np .array , tuple
81
- ])
85
+ ], ids = str )
82
86
def test_is_nested_list_like_passes (inner , outer ):
83
87
result = outer ([inner for _ in range (5 )])
84
88
assert inference .is_list_like (result )
@@ -93,15 +97,16 @@ def test_is_nested_list_like_fails(obj):
93
97
assert not inference .is_nested_list_like (obj )
94
98
95
99
96
- def test_is_dict_like ():
97
- passes = [{}, {'A' : 1 }, Series ([1 ])]
98
- fails = ['1' , 1 , [1 , 2 ], (1 , 2 ), range (2 ), Index ([1 ])]
100
+ @pytest .mark .parametrize (
101
+ "ll" , [{}, {'A' : 1 }, Series ([1 ])])
102
+ def test_is_dict_like_passes (ll ):
103
+ assert inference .is_dict_like (ll )
99
104
100
- for p in passes :
101
- assert inference .is_dict_like (p )
102
105
103
- for f in fails :
104
- assert not inference .is_dict_like (f )
106
+ @pytest .mark .parametrize (
107
+ "ll" , ['1' , 1 , [1 , 2 ], (1 , 2 ), range (2 ), Index ([1 ])])
108
+ def test_is_dict_like_fails (ll ):
109
+ assert not inference .is_dict_like (ll )
105
110
106
111
107
112
def test_is_file_like ():
@@ -148,15 +153,16 @@ class MockFile(object):
148
153
assert not is_file (mock .Mock ())
149
154
150
155
151
- def test_is_named_tuple ():
152
- passes = (collections .namedtuple ('Test' , list ('abc' ))(1 , 2 , 3 ), )
153
- fails = ((1 , 2 , 3 ), 'a' , Series ({'pi' : 3.14 }))
156
+ @pytest .mark .parametrize (
157
+ "ll" , [collections .namedtuple ('Test' , list ('abc' ))(1 , 2 , 3 )])
158
+ def test_is_names_tuple_passes (ll ):
159
+ assert inference .is_named_tuple (ll )
154
160
155
- for p in passes :
156
- assert inference .is_named_tuple (p )
157
161
158
- for f in fails :
159
- assert not inference .is_named_tuple (f )
162
+ @pytest .mark .parametrize (
163
+ "ll" , [(1 , 2 , 3 ), 'a' , Series ({'pi' : 3.14 })])
164
+ def test_is_names_tuple_fails (ll ):
165
+ assert not inference .is_named_tuple (ll )
160
166
161
167
162
168
def test_is_hashable ():
@@ -208,27 +214,32 @@ class OldStyleClass():
208
214
hash (c ) # this will not raise
209
215
210
216
211
- def test_is_re ():
212
- passes = re .compile ('ad' ),
213
- fails = 'x' , 2 , 3 , object ()
217
+ @pytest .mark .parametrize (
218
+ "ll" , [re .compile ('ad' )])
219
+ def test_is_re_passes (ll ):
220
+ assert inference .is_re (ll )
214
221
215
- for p in passes :
216
- assert inference .is_re (p )
217
222
218
- for f in fails :
219
- assert not inference .is_re (f )
223
+ @pytest .mark .parametrize (
224
+ "ll" , ['x' , 2 , 3 , object ()])
225
+ def test_is_re_fails (ll ):
226
+ assert not inference .is_re (ll )
220
227
221
228
222
- def test_is_recompilable ():
223
- passes = (r'a' , u ('x' ), r'asdf' , re .compile ('adsf' ), u (r'\u2233\s*' ),
224
- re .compile (r'' ))
225
- fails = 1 , [], object ()
229
+ @pytest .mark .parametrize (
230
+ "ll" , [r'a' , u ('x' ),
231
+ r'asdf' ,
232
+ re .compile ('adsf' ),
233
+ u (r'\u2233\s*' ),
234
+ re .compile (r'' )])
235
+ def test_is_recompilable_passes (ll ):
236
+ assert inference .is_re_compilable (ll )
226
237
227
- for p in passes :
228
- assert inference .is_re_compilable (p )
229
238
230
- for f in fails :
231
- assert not inference .is_re_compilable (f )
239
+ @pytest .mark .parametrize (
240
+ "ll" , [1 , [], object ()])
241
+ def test_is_recompilable_fails (ll ):
242
+ assert not inference .is_re_compilable (ll )
232
243
233
244
234
245
class TestInference (object ):
@@ -300,15 +311,14 @@ def test_maybe_convert_numeric_infinities(self):
300
311
np .array (['foo_' + infinity ], dtype = object ),
301
312
na_values , maybe_int )
302
313
303
- def test_maybe_convert_numeric_post_floatify_nan (self ):
314
+ def test_maybe_convert_numeric_post_floatify_nan (self , coerce ):
304
315
# see gh-13314
305
316
data = np .array (['1.200' , '-999.000' , '4.500' ], dtype = object )
306
317
expected = np .array ([1.2 , np .nan , 4.5 ], dtype = np .float64 )
307
318
nan_values = set ([- 999 , - 999.0 ])
308
319
309
- for coerce_type in (True , False ):
310
- out = lib .maybe_convert_numeric (data , nan_values , coerce_type )
311
- tm .assert_numpy_array_equal (out , expected )
320
+ out = lib .maybe_convert_numeric (data , nan_values , coerce )
321
+ tm .assert_numpy_array_equal (out , expected )
312
322
313
323
def test_convert_infs (self ):
314
324
arr = np .array (['inf' , 'inf' , 'inf' ], dtype = 'O' )
@@ -739,6 +749,36 @@ def test_is_datetimelike_array_all_nan_nat_like(self):
739
749
assert not lib .is_timedelta64_array (arr )
740
750
assert not lib .is_timedelta_or_timedelta64_array (arr )
741
751
752
+ assert lib .is_datetime_with_singletz_array (
753
+ np .array ([pd .Timestamp ('20130101' , tz = 'US/Eastern' ),
754
+ pd .Timestamp ('20130102' , tz = 'US/Eastern' )],
755
+ dtype = object ))
756
+ assert not lib .is_datetime_with_singletz_array (
757
+ np .array ([pd .Timestamp ('20130101' , tz = 'US/Eastern' ),
758
+ pd .Timestamp ('20130102' , tz = 'CET' )],
759
+ dtype = object ))
760
+
761
+ @pytest .mark .parametrize (
762
+ "func" ,
763
+ [
764
+ 'is_datetime_array' ,
765
+ 'is_datetime64_array' ,
766
+ 'is_bool_array' ,
767
+ 'is_timedelta_array' ,
768
+ 'is_timedelta64_array' ,
769
+ 'is_timedelta_or_timedelta64_array' ,
770
+ 'is_date_array' ,
771
+ 'is_time_array' ,
772
+ 'is_interval_array' ,
773
+ 'is_period_array' ])
774
+ def test_other_dtypes_for_array (self , func ):
775
+ func = getattr (lib , func )
776
+ arr = np .array (['foo' , 'bar' ])
777
+ assert not func (arr )
778
+
779
+ arr = np .array ([1 , 2 ])
780
+ assert not func (arr )
781
+
742
782
def test_date (self ):
743
783
744
784
dates = [date (2012 , 1 , day ) for day in range (1 , 20 )]
@@ -752,6 +792,24 @@ def test_date(self):
752
792
result = lib .infer_dtype (dates , skipna = True )
753
793
assert result == 'date'
754
794
795
+ def test_is_numeric_array (self ):
796
+
797
+ assert lib .is_float_array (np .array ([1 , 2.0 ]))
798
+ assert lib .is_float_array (np .array ([1 , 2.0 , np .nan ]))
799
+ assert not lib .is_float_array (np .array ([1 , 2 ]))
800
+
801
+ assert lib .is_integer_array (np .array ([1 , 2 ]))
802
+ assert not lib .is_integer_array (np .array ([1 , 2.0 ]))
803
+
804
+ def test_is_string_array (self ):
805
+
806
+ assert lib .is_string_array (np .array (['foo' , 'bar' ]))
807
+ assert not lib .is_string_array (
808
+ np .array (['foo' , 'bar' , np .nan ], dtype = object ), skipna = False )
809
+ assert lib .is_string_array (
810
+ np .array (['foo' , 'bar' , np .nan ], dtype = object ), skipna = True )
811
+ assert not lib .is_string_array (np .array ([1 , 2 ]))
812
+
755
813
def test_to_object_array_tuples (self ):
756
814
r = (5 , 6 )
757
815
values = [r ]
0 commit comments