6
6
import pytest
7
7
8
8
import pandas as pd
9
- from pandas import (
10
- Categorical ,
11
- DataFrame ,
12
- Series ,
13
- SparseDtype ,
14
- compat ,
15
- date_range ,
16
- timedelta_range ,
17
- )
9
+ from pandas import Categorical , DataFrame , Series , compat , date_range , timedelta_range
18
10
import pandas .util .testing as tm
19
11
20
12
21
- class SharedWithSparse :
22
- """
23
- A collection of tests DataFrame and SparseDataFrame can share.
24
-
25
- In generic tests on this class, use ``self._assert_frame_equal()`` and
26
- ``self._assert_series_equal()`` which are implemented in sub-classes
27
- and dispatch correctly.
28
- """
29
-
30
- def _assert_frame_equal (self , left , right ):
31
- """Dispatch to frame class dependent assertion"""
32
- raise NotImplementedError
33
-
34
- def _assert_series_equal (self , left , right ):
35
- """Dispatch to series class dependent assertion"""
36
- raise NotImplementedError
37
-
13
+ class TestDataFrameMisc :
38
14
def test_copy_index_name_checking (self , float_frame ):
39
15
# don't want to be able to modify the index stored elsewhere after
40
16
# making a copy
@@ -141,16 +117,16 @@ def test_tab_completion(self):
141
117
def test_not_hashable (self ):
142
118
empty_frame = DataFrame ()
143
119
144
- df = self . klass ([1 ])
145
- msg = "'(Sparse)? DataFrame' objects are mutable, thus they cannot be hashed"
120
+ df = DataFrame ([1 ])
121
+ msg = "'DataFrame' objects are mutable, thus they cannot be hashed"
146
122
with pytest .raises (TypeError , match = msg ):
147
123
hash (df )
148
124
with pytest .raises (TypeError , match = msg ):
149
125
hash (empty_frame )
150
126
151
127
def test_new_empty_index (self ):
152
- df1 = self . klass (np .random .randn (0 , 3 ))
153
- df2 = self . klass (np .random .randn (0 , 3 ))
128
+ df1 = DataFrame (np .random .randn (0 , 3 ))
129
+ df2 = DataFrame (np .random .randn (0 , 3 ))
154
130
df1 .index .name = "foo"
155
131
assert df2 .index .name is None
156
132
@@ -161,7 +137,7 @@ def test_array_interface(self, float_frame):
161
137
assert result .index is float_frame .index
162
138
assert result .columns is float_frame .columns
163
139
164
- self . _assert_frame_equal (result , float_frame .apply (np .sqrt ))
140
+ tm . assert_frame_equal (result , float_frame .apply (np .sqrt ))
165
141
166
142
def test_get_agg_axis (self , float_frame ):
167
143
cols = float_frame ._get_agg_axis (0 )
@@ -187,9 +163,9 @@ def test_nonzero(self, float_frame, float_string_frame):
187
163
assert not df .empty
188
164
189
165
def test_iteritems (self ):
190
- df = self . klass ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = ["a" , "a" , "b" ])
166
+ df = DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = ["a" , "a" , "b" ])
191
167
for k , v in df .items ():
192
- assert isinstance (v , self . klass ._constructor_sliced )
168
+ assert isinstance (v , DataFrame ._constructor_sliced )
193
169
194
170
def test_items (self ):
195
171
# GH 17213, GH 13918
@@ -206,23 +182,23 @@ def test_iter(self, float_frame):
206
182
def test_iterrows (self , float_frame , float_string_frame ):
207
183
for k , v in float_frame .iterrows ():
208
184
exp = float_frame .loc [k ]
209
- self . _assert_series_equal (v , exp )
185
+ tm . assert_series_equal (v , exp )
210
186
211
187
for k , v in float_string_frame .iterrows ():
212
188
exp = float_string_frame .loc [k ]
213
- self . _assert_series_equal (v , exp )
189
+ tm . assert_series_equal (v , exp )
214
190
215
191
def test_iterrows_iso8601 (self ):
216
192
# GH 19671
217
- s = self . klass (
193
+ s = DataFrame (
218
194
{
219
195
"non_iso8601" : ["M1701" , "M1802" , "M1903" , "M2004" ],
220
196
"iso8601" : date_range ("2000-01-01" , periods = 4 , freq = "M" ),
221
197
}
222
198
)
223
199
for k , v in s .iterrows ():
224
200
exp = s .loc [k ]
225
- self . _assert_series_equal (v , exp )
201
+ tm . assert_series_equal (v , exp )
226
202
227
203
def test_iterrows_corner (self ):
228
204
# gh-12222
@@ -248,19 +224,19 @@ def test_iterrows_corner(self):
248
224
249
225
def test_itertuples (self , float_frame ):
250
226
for i , tup in enumerate (float_frame .itertuples ()):
251
- s = self . klass ._constructor_sliced (tup [1 :])
227
+ s = DataFrame ._constructor_sliced (tup [1 :])
252
228
s .name = tup [0 ]
253
229
expected = float_frame .iloc [i , :].reset_index (drop = True )
254
- self . _assert_series_equal (s , expected )
230
+ tm . assert_series_equal (s , expected )
255
231
256
- df = self . klass (
232
+ df = DataFrame (
257
233
{"floats" : np .random .randn (5 ), "ints" : range (5 )}, columns = ["floats" , "ints" ]
258
234
)
259
235
260
236
for tup in df .itertuples (index = False ):
261
237
assert isinstance (tup [1 ], int )
262
238
263
- df = self . klass (data = {"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ]})
239
+ df = DataFrame (data = {"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ]})
264
240
dfaa = df [["a" , "a" ]]
265
241
266
242
assert list (dfaa .itertuples ()) == [(0 , 1 , 1 ), (1 , 2 , 2 ), (2 , 3 , 3 )]
@@ -315,7 +291,7 @@ def test_sequence_like_with_categorical(self):
315
291
def test_len (self , float_frame ):
316
292
assert len (float_frame ) == len (float_frame .index )
317
293
318
- def test_values (self , float_frame , float_string_frame ):
294
+ def test_values_mixed_dtypes (self , float_frame , float_string_frame ):
319
295
frame = float_frame
320
296
arr = frame .values
321
297
@@ -332,7 +308,7 @@ def test_values(self, float_frame, float_string_frame):
332
308
arr = float_string_frame [["foo" , "A" ]].values
333
309
assert arr [0 , 0 ] == "bar"
334
310
335
- df = self . klass ({"complex" : [1j , 2j , 3j ], "real" : [1 , 2 , 3 ]})
311
+ df = DataFrame ({"complex" : [1j , 2j , 3j ], "real" : [1 , 2 , 3 ]})
336
312
arr = df .values
337
313
assert arr [0 , 0 ] == 1j
338
314
@@ -372,17 +348,17 @@ def test_transpose(self, float_frame):
372
348
373
349
# mixed type
374
350
index , data = tm .getMixedTypeDict ()
375
- mixed = self . klass (data , index = index )
351
+ mixed = DataFrame (data , index = index )
376
352
377
353
mixed_T = mixed .T
378
354
for col , s in mixed_T .items ():
379
355
assert s .dtype == np .object_
380
356
381
357
def test_swapaxes (self ):
382
- df = self . klass (np .random .randn (10 , 5 ))
383
- self . _assert_frame_equal (df .T , df .swapaxes (0 , 1 ))
384
- self . _assert_frame_equal (df .T , df .swapaxes (1 , 0 ))
385
- self . _assert_frame_equal (df , df .swapaxes (0 , 0 ))
358
+ df = DataFrame (np .random .randn (10 , 5 ))
359
+ tm . assert_frame_equal (df .T , df .swapaxes (0 , 1 ))
360
+ tm . assert_frame_equal (df .T , df .swapaxes (1 , 0 ))
361
+ tm . assert_frame_equal (df , df .swapaxes (0 , 0 ))
386
362
msg = (
387
363
"No axis named 2 for object type"
388
364
r" <class 'pandas.core(.sparse)?.frame.(Sparse)?DataFrame'>"
@@ -413,7 +389,7 @@ def test_more_values(self, float_string_frame):
413
389
assert values .shape [1 ] == len (float_string_frame .columns )
414
390
415
391
def test_repr_with_mi_nat (self , float_string_frame ):
416
- df = self . klass (
392
+ df = DataFrame (
417
393
{"X" : [1 , 2 ]}, index = [[pd .NaT , pd .Timestamp ("20130101" )], ["a" , "b" ]]
418
394
)
419
395
result = repr (df )
@@ -430,26 +406,26 @@ def test_series_put_names(self, float_string_frame):
430
406
assert v .name == k
431
407
432
408
def test_empty_nonzero (self ):
433
- df = self . klass ([1 , 2 , 3 ])
409
+ df = DataFrame ([1 , 2 , 3 ])
434
410
assert not df .empty
435
- df = self . klass (index = [1 ], columns = [1 ])
411
+ df = DataFrame (index = [1 ], columns = [1 ])
436
412
assert not df .empty
437
- df = self . klass (index = ["a" , "b" ], columns = ["c" , "d" ]).dropna ()
413
+ df = DataFrame (index = ["a" , "b" ], columns = ["c" , "d" ]).dropna ()
438
414
assert df .empty
439
415
assert df .T .empty
440
416
empty_frames = [
441
- self . klass (),
442
- self . klass (index = [1 ]),
443
- self . klass (columns = [1 ]),
444
- self . klass ({1 : []}),
417
+ DataFrame (),
418
+ DataFrame (index = [1 ]),
419
+ DataFrame (columns = [1 ]),
420
+ DataFrame ({1 : []}),
445
421
]
446
422
for df in empty_frames :
447
423
assert df .empty
448
424
assert df .T .empty
449
425
450
426
def test_with_datetimelikes (self ):
451
427
452
- df = self . klass (
428
+ df = DataFrame (
453
429
{
454
430
"A" : date_range ("20130101" , periods = 10 ),
455
431
"B" : timedelta_range ("1 day" , periods = 10 ),
@@ -458,20 +434,9 @@ def test_with_datetimelikes(self):
458
434
t = df .T
459
435
460
436
result = t .dtypes .value_counts ()
461
- if self .klass is DataFrame :
462
- expected = Series ({np .dtype ("object" ): 10 })
463
- else :
464
- expected = Series ({SparseDtype (dtype = object ): 10 })
437
+ expected = Series ({np .dtype ("object" ): 10 })
465
438
tm .assert_series_equal (result , expected )
466
439
467
-
468
- class TestDataFrameMisc (SharedWithSparse ):
469
-
470
- klass = DataFrame
471
- # SharedWithSparse tests use generic, klass-agnostic assertion
472
- _assert_frame_equal = staticmethod (tm .assert_frame_equal )
473
- _assert_series_equal = staticmethod (tm .assert_series_equal )
474
-
475
440
def test_values (self , float_frame ):
476
441
float_frame .values [:, 0 ] = 5.0
477
442
assert (float_frame .values [:, 0 ] == 5 ).all ()
0 commit comments