17
17
from pandas import (
18
18
Categorical , DataFrame , Index , MultiIndex , RangeIndex , Series , Timedelta ,
19
19
Timestamp , date_range , isna )
20
- from pandas .tests .frame .common import TestData
21
20
import pandas .util .testing as tm
22
21
23
22
MIXED_FLOAT_DTYPES = ['float16' , 'float32' , 'float64' ]
24
23
MIXED_INT_DTYPES = ['uint8' , 'uint16' , 'uint32' , 'uint64' , 'int8' , 'int16' ,
25
24
'int32' , 'int64' ]
26
25
27
26
28
- class TestDataFrameConstructors ( TestData ) :
27
+ class TestDataFrameConstructors :
29
28
30
29
@pytest .mark .parametrize ('constructor' , [
31
30
lambda : DataFrame (),
@@ -60,14 +59,14 @@ def test_emptylike_constructor(
60
59
result = DataFrame (emptylike )
61
60
tm .assert_frame_equal (result , expected )
62
61
63
- def test_constructor_mixed (self ):
62
+ def test_constructor_mixed (self , float_string_frame ):
64
63
index , data = tm .getMixedTypeDict ()
65
64
66
65
# TODO(wesm), incomplete test?
67
66
indexed_frame = DataFrame (data , index = index ) # noqa
68
67
unindexed_frame = DataFrame (data ) # noqa
69
68
70
- assert self . mixed_frame ['foo' ].dtype == np .object_
69
+ assert float_string_frame ['foo' ].dtype == np .object_
71
70
72
71
def test_constructor_cast_failure (self ):
73
72
foo = DataFrame ({'a' : ['a' , 'b' , 'c' ]}, dtype = np .float64 )
@@ -181,11 +180,11 @@ def test_constructor_dtype_str_na_values(self, string_dtype):
181
180
df = DataFrame ({'A' : ['x' , np .nan ]}, dtype = string_dtype )
182
181
assert np .isnan (df .iloc [1 , 0 ])
183
182
184
- def test_constructor_rec (self ):
185
- rec = self . frame .to_records (index = False )
183
+ def test_constructor_rec (self , float_frame ):
184
+ rec = float_frame .to_records (index = False )
186
185
rec .dtype .names = list (rec .dtype .names )[::- 1 ]
187
186
188
- index = self . frame .index
187
+ index = float_frame .index
189
188
190
189
df = DataFrame (rec )
191
190
tm .assert_index_equal (df .columns , pd .Index (rec .dtype .names ))
@@ -244,24 +243,29 @@ def test_constructor_ordereddict(self):
244
243
assert expected == list (df .columns )
245
244
246
245
def test_constructor_dict (self ):
247
- frame = DataFrame ({'col1' : self .ts1 ,
248
- 'col2' : self .ts2 })
246
+ datetime_series = tm .makeTimeSeries (nper = 30 )
247
+ # test expects index shifted by 5
248
+ datetime_series_short = tm .makeTimeSeries (nper = 30 )[5 :]
249
+
250
+ frame = DataFrame ({'col1' : datetime_series ,
251
+ 'col2' : datetime_series_short })
249
252
250
253
# col2 is padded with NaN
251
- assert len (self . ts1 ) == 30
252
- assert len (self . ts2 ) == 25
254
+ assert len (datetime_series ) == 30
255
+ assert len (datetime_series_short ) == 25
253
256
254
- tm .assert_series_equal (self . ts1 , frame ['col1' ], check_names = False )
257
+ tm .assert_series_equal (frame ['col1' ], datetime_series . rename ( 'col1' ) )
255
258
256
- exp = pd .Series (np .concatenate ([[np .nan ] * 5 , self .ts2 .values ]),
257
- index = self .ts1 .index , name = 'col2' )
259
+ exp = pd .Series (np .concatenate ([[np .nan ] * 5 ,
260
+ datetime_series_short .values ]),
261
+ index = datetime_series .index , name = 'col2' )
258
262
tm .assert_series_equal (exp , frame ['col2' ])
259
263
260
- frame = DataFrame ({'col1' : self . ts1 ,
261
- 'col2' : self . ts2 },
264
+ frame = DataFrame ({'col1' : datetime_series ,
265
+ 'col2' : datetime_series_short },
262
266
columns = ['col2' , 'col3' , 'col4' ])
263
267
264
- assert len (frame ) == len (self . ts2 )
268
+ assert len (frame ) == len (datetime_series_short )
265
269
assert 'col1' not in frame
266
270
assert isna (frame ['col3' ]).all ()
267
271
@@ -361,18 +365,24 @@ def test_constructor_dict_nan_tuple_key(self, value):
361
365
362
366
@pytest .mark .skipif (not PY36 , reason = 'Insertion order for Python>=3.6' )
363
367
def test_constructor_dict_order_insertion (self ):
368
+ datetime_series = tm .makeTimeSeries (nper = 30 )
369
+ datetime_series_short = tm .makeTimeSeries (nper = 25 )
370
+
364
371
# GH19018
365
372
# initialization ordering: by insertion order if python>= 3.6
366
- d = {'b' : self . ts2 , 'a' : self . ts1 }
373
+ d = {'b' : datetime_series_short , 'a' : datetime_series }
367
374
frame = DataFrame (data = d )
368
375
expected = DataFrame (data = d , columns = list ('ba' ))
369
376
tm .assert_frame_equal (frame , expected )
370
377
371
378
@pytest .mark .skipif (PY36 , reason = 'order by value for Python<3.6' )
372
379
def test_constructor_dict_order_by_values (self ):
380
+ datetime_series = tm .makeTimeSeries (nper = 30 )
381
+ datetime_series_short = tm .makeTimeSeries (nper = 25 )
382
+
373
383
# GH19018
374
384
# initialization ordering: by value if python<3.6
375
- d = {'b' : self . ts2 , 'a' : self . ts1 }
385
+ d = {'b' : datetime_series_short , 'a' : datetime_series }
376
386
frame = DataFrame (data = d )
377
387
expected = DataFrame (data = d , columns = list ('ab' ))
378
388
tm .assert_frame_equal (frame , expected )
@@ -462,7 +472,7 @@ def test_constructor_with_embedded_frames(self):
462
472
result = df2 .loc [1 , 0 ]
463
473
tm .assert_frame_equal (result , df1 + 10 )
464
474
465
- def test_constructor_subclass_dict (self ):
475
+ def test_constructor_subclass_dict (self , float_frame ):
466
476
# Test for passing dict subclass to constructor
467
477
data = {'col1' : tm .TestSubDict ((x , 10.0 * x ) for x in range (10 )),
468
478
'col2' : tm .TestSubDict ((x , 20.0 * x ) for x in range (10 ))}
@@ -478,13 +488,13 @@ def test_constructor_subclass_dict(self):
478
488
# try with defaultdict
479
489
from collections import defaultdict
480
490
data = {}
481
- self . frame ['B' ][:10 ] = np .nan
482
- for k , v in self . frame .items ():
491
+ float_frame ['B' ][:10 ] = np .nan
492
+ for k , v in float_frame .items ():
483
493
dct = defaultdict (dict )
484
494
dct .update (v .to_dict ())
485
495
data [k ] = dct
486
496
frame = DataFrame (data )
487
- tm .assert_frame_equal (self . frame .sort_index (), frame )
497
+ tm .assert_frame_equal (float_frame .sort_index (), frame )
488
498
489
499
def test_constructor_dict_block (self ):
490
500
expected = np .array ([[4. , 3. , 2. , 1. ]])
@@ -923,14 +933,14 @@ def test_constructor_arrays_and_scalars(self):
923
933
with pytest .raises (ValueError , match = 'must pass an index' ):
924
934
DataFrame ({'a' : False , 'b' : True })
925
935
926
- def test_constructor_DataFrame (self ):
927
- df = DataFrame (self . frame )
928
- tm .assert_frame_equal (df , self . frame )
936
+ def test_constructor_DataFrame (self , float_frame ):
937
+ df = DataFrame (float_frame )
938
+ tm .assert_frame_equal (df , float_frame )
929
939
930
- df_casted = DataFrame (self . frame , dtype = np .int64 )
940
+ df_casted = DataFrame (float_frame , dtype = np .int64 )
931
941
assert df_casted .values .dtype == np .int64
932
942
933
- def test_constructor_more (self ):
943
+ def test_constructor_more (self , float_frame ):
934
944
# used to be in test_matrix.py
935
945
arr = np .random .randn (10 )
936
946
dm = DataFrame (arr , columns = ['A' ], index = np .arange (10 ))
@@ -956,8 +966,8 @@ def test_constructor_more(self):
956
966
with pytest .raises (ValueError , match = 'cast' ):
957
967
DataFrame (mat , index = [0 , 1 ], columns = [0 ], dtype = float )
958
968
959
- dm = DataFrame (DataFrame (self . frame ._series ))
960
- tm .assert_frame_equal (dm , self . frame )
969
+ dm = DataFrame (DataFrame (float_frame ._series ))
970
+ tm .assert_frame_equal (dm , float_frame )
961
971
962
972
# int cast
963
973
dm = DataFrame ({'A' : np .ones (10 , dtype = int ),
@@ -1223,8 +1233,9 @@ def test_constructor_scalar(self):
1223
1233
expected = DataFrame ({"a" : [0 , 0 , 0 ]}, index = idx )
1224
1234
tm .assert_frame_equal (df , expected , check_dtype = False )
1225
1235
1226
- def test_constructor_Series_copy_bug (self ):
1227
- df = DataFrame (self .frame ['A' ], index = self .frame .index , columns = ['A' ])
1236
+ def test_constructor_Series_copy_bug (self , float_frame ):
1237
+ df = DataFrame (float_frame ['A' ], index = float_frame .index ,
1238
+ columns = ['A' ])
1228
1239
df .copy ()
1229
1240
1230
1241
def test_constructor_mixed_dict_and_Series (self ):
@@ -1286,10 +1297,10 @@ def test_constructor_list_of_namedtuples(self):
1286
1297
result = DataFrame (tuples , columns = ['y' , 'z' ])
1287
1298
tm .assert_frame_equal (result , expected )
1288
1299
1289
- def test_constructor_orient (self ):
1290
- data_dict = self . mixed_frame .T ._series
1300
+ def test_constructor_orient (self , float_string_frame ):
1301
+ data_dict = float_string_frame .T ._series
1291
1302
recons = DataFrame .from_dict (data_dict , orient = 'index' )
1292
- expected = self . mixed_frame .sort_index ()
1303
+ expected = float_string_frame .sort_index ()
1293
1304
tm .assert_frame_equal (recons , expected )
1294
1305
1295
1306
# dict of sequence
@@ -1393,38 +1404,38 @@ def test_constructor_Series_differently_indexed(self):
1393
1404
tm .assert_index_equal (df2 .index , other_index )
1394
1405
tm .assert_frame_equal (df2 , exp2 )
1395
1406
1396
- def test_constructor_manager_resize (self ):
1397
- index = list (self . frame .index [:5 ])
1398
- columns = list (self . frame .columns [:3 ])
1407
+ def test_constructor_manager_resize (self , float_frame ):
1408
+ index = list (float_frame .index [:5 ])
1409
+ columns = list (float_frame .columns [:3 ])
1399
1410
1400
- result = DataFrame (self . frame ._data , index = index ,
1411
+ result = DataFrame (float_frame ._data , index = index ,
1401
1412
columns = columns )
1402
1413
tm .assert_index_equal (result .index , Index (index ))
1403
1414
tm .assert_index_equal (result .columns , Index (columns ))
1404
1415
1405
- def test_constructor_from_items (self ):
1406
- items = [(c , self . frame [c ]) for c in self . frame .columns ]
1416
+ def test_constructor_from_items (self , float_frame , float_string_frame ):
1417
+ items = [(c , float_frame [c ]) for c in float_frame .columns ]
1407
1418
with tm .assert_produces_warning (FutureWarning ,
1408
1419
check_stacklevel = False ):
1409
1420
recons = DataFrame .from_items (items )
1410
- tm .assert_frame_equal (recons , self . frame )
1421
+ tm .assert_frame_equal (recons , float_frame )
1411
1422
1412
1423
# pass some columns
1413
1424
with tm .assert_produces_warning (FutureWarning ,
1414
1425
check_stacklevel = False ):
1415
1426
recons = DataFrame .from_items (items , columns = ['C' , 'B' , 'A' ])
1416
- tm .assert_frame_equal (recons , self . frame .loc [:, ['C' , 'B' , 'A' ]])
1427
+ tm .assert_frame_equal (recons , float_frame .loc [:, ['C' , 'B' , 'A' ]])
1417
1428
1418
1429
# orient='index'
1419
1430
1420
- row_items = [(idx , self . mixed_frame .xs (idx ))
1421
- for idx in self . mixed_frame .index ]
1431
+ row_items = [(idx , float_string_frame .xs (idx ))
1432
+ for idx in float_string_frame .index ]
1422
1433
with tm .assert_produces_warning (FutureWarning ,
1423
1434
check_stacklevel = False ):
1424
1435
recons = DataFrame .from_items (row_items ,
1425
- columns = self . mixed_frame .columns ,
1436
+ columns = float_string_frame .columns ,
1426
1437
orient = 'index' )
1427
- tm .assert_frame_equal (recons , self . mixed_frame )
1438
+ tm .assert_frame_equal (recons , float_string_frame )
1428
1439
assert recons ['A' ].dtype == np .float64
1429
1440
1430
1441
msg = "Must pass columns with orient='index'"
@@ -1435,16 +1446,16 @@ def test_constructor_from_items(self):
1435
1446
1436
1447
# orient='index', but thar be tuples
1437
1448
arr = construct_1d_object_array_from_listlike (
1438
- [('bar' , 'baz' )] * len (self . mixed_frame ))
1439
- self . mixed_frame ['foo' ] = arr
1440
- row_items = [(idx , list (self . mixed_frame .xs (idx )))
1441
- for idx in self . mixed_frame .index ]
1449
+ [('bar' , 'baz' )] * len (float_string_frame ))
1450
+ float_string_frame ['foo' ] = arr
1451
+ row_items = [(idx , list (float_string_frame .xs (idx )))
1452
+ for idx in float_string_frame .index ]
1442
1453
with tm .assert_produces_warning (FutureWarning ,
1443
1454
check_stacklevel = False ):
1444
1455
recons = DataFrame .from_items (row_items ,
1445
- columns = self . mixed_frame .columns ,
1456
+ columns = float_string_frame .columns ,
1446
1457
orient = 'index' )
1447
- tm .assert_frame_equal (recons , self . mixed_frame )
1458
+ tm .assert_frame_equal (recons , float_string_frame )
1448
1459
assert isinstance (recons ['foo' ][0 ], tuple )
1449
1460
1450
1461
with tm .assert_produces_warning (FutureWarning ,
@@ -1485,14 +1496,15 @@ def test_from_items_deprecation(self):
1485
1496
columns = ['col1' , 'col2' , 'col3' ],
1486
1497
orient = 'index' )
1487
1498
1488
- def test_constructor_mix_series_nonseries (self ):
1489
- df = DataFrame ({'A' : self . frame ['A' ],
1490
- 'B' : list (self . frame ['B' ])}, columns = ['A' , 'B' ])
1491
- tm .assert_frame_equal (df , self . frame .loc [:, ['A' , 'B' ]])
1499
+ def test_constructor_mix_series_nonseries (self , float_frame ):
1500
+ df = DataFrame ({'A' : float_frame ['A' ],
1501
+ 'B' : list (float_frame ['B' ])}, columns = ['A' , 'B' ])
1502
+ tm .assert_frame_equal (df , float_frame .loc [:, ['A' , 'B' ]])
1492
1503
1493
1504
msg = 'does not match index length'
1494
1505
with pytest .raises (ValueError , match = msg ):
1495
- DataFrame ({'A' : self .frame ['A' ], 'B' : list (self .frame ['B' ])[:- 2 ]})
1506
+ DataFrame ({'A' : float_frame ['A' ],
1507
+ 'B' : list (float_frame ['B' ])[:- 2 ]})
1496
1508
1497
1509
def test_constructor_miscast_na_int_dtype (self ):
1498
1510
df = DataFrame ([[np .nan , 1 ], [1 , 0 ]], dtype = np .int64 )
@@ -1752,24 +1764,24 @@ def test_constructor_for_list_with_dtypes(self):
1752
1764
expected = expected .sort_index ()
1753
1765
tm .assert_series_equal (result , expected )
1754
1766
1755
- def test_constructor_frame_copy (self ):
1756
- cop = DataFrame (self . frame , copy = True )
1767
+ def test_constructor_frame_copy (self , float_frame ):
1768
+ cop = DataFrame (float_frame , copy = True )
1757
1769
cop ['A' ] = 5
1758
1770
assert (cop ['A' ] == 5 ).all ()
1759
- assert not (self . frame ['A' ] == 5 ).all ()
1771
+ assert not (float_frame ['A' ] == 5 ).all ()
1760
1772
1761
- def test_constructor_ndarray_copy (self ):
1762
- df = DataFrame (self . frame .values )
1773
+ def test_constructor_ndarray_copy (self , float_frame ):
1774
+ df = DataFrame (float_frame .values )
1763
1775
1764
- self . frame .values [5 ] = 5
1776
+ float_frame .values [5 ] = 5
1765
1777
assert (df .values [5 ] == 5 ).all ()
1766
1778
1767
- df = DataFrame (self . frame .values , copy = True )
1768
- self . frame .values [6 ] = 6
1779
+ df = DataFrame (float_frame .values , copy = True )
1780
+ float_frame .values [6 ] = 6
1769
1781
assert not (df .values [6 ] == 6 ).all ()
1770
1782
1771
- def test_constructor_series_copy (self ):
1772
- series = self . frame ._series
1783
+ def test_constructor_series_copy (self , float_frame ):
1784
+ series = float_frame ._series
1773
1785
1774
1786
df = DataFrame ({'A' : series ['A' ]})
1775
1787
df ['A' ][:] = 5
@@ -2318,7 +2330,7 @@ class List(list):
2318
2330
tm .assert_frame_equal (result , expected )
2319
2331
2320
2332
2321
- class TestDataFrameConstructorWithDatetimeTZ ( TestData ) :
2333
+ class TestDataFrameConstructorWithDatetimeTZ :
2322
2334
2323
2335
def test_from_dict (self ):
2324
2336
0 commit comments