47
47
class TestDataFrameConstructors :
48
48
def test_series_with_name_not_matching_column (self ):
49
49
# GH#9232
50
- x = pd . Series (range (5 ), name = 1 )
51
- y = pd . Series (range (5 ), name = 0 )
50
+ x = Series (range (5 ), name = 1 )
51
+ y = Series (range (5 ), name = 0 )
52
52
53
- result = pd . DataFrame (x , columns = [0 ])
54
- expected = pd . DataFrame ([], columns = [0 ])
53
+ result = DataFrame (x , columns = [0 ])
54
+ expected = DataFrame ([], columns = [0 ])
55
55
tm .assert_frame_equal (result , expected )
56
56
57
- result = pd . DataFrame (y , columns = [1 ])
58
- expected = pd . DataFrame ([], columns = [1 ])
57
+ result = DataFrame (y , columns = [1 ])
58
+ expected = DataFrame ([], columns = [1 ])
59
59
tm .assert_frame_equal (result , expected )
60
60
61
61
@pytest .mark .parametrize (
@@ -126,7 +126,7 @@ def test_constructor_cast_failure(self):
126
126
def test_constructor_dtype_copy (self ):
127
127
orig_df = DataFrame ({"col1" : [1.0 ], "col2" : [2.0 ], "col3" : [3.0 ]})
128
128
129
- new_df = pd . DataFrame (orig_df , dtype = float , copy = True )
129
+ new_df = DataFrame (orig_df , dtype = float , copy = True )
130
130
131
131
new_df ["col1" ] = 200.0
132
132
assert orig_df ["col1" ][0 ] == 1.0
@@ -220,10 +220,10 @@ def test_constructor_rec(self, float_frame):
220
220
index = float_frame .index
221
221
222
222
df = DataFrame (rec )
223
- tm .assert_index_equal (df .columns , pd . Index (rec .dtype .names ))
223
+ tm .assert_index_equal (df .columns , Index (rec .dtype .names ))
224
224
225
225
df2 = DataFrame (rec , index = index )
226
- tm .assert_index_equal (df2 .columns , pd . Index (rec .dtype .names ))
226
+ tm .assert_index_equal (df2 .columns , Index (rec .dtype .names ))
227
227
tm .assert_index_equal (df2 .index , index )
228
228
229
229
rng = np .arange (len (rec ))[::- 1 ]
@@ -298,7 +298,7 @@ def test_constructor_dict(self):
298
298
299
299
tm .assert_series_equal (frame ["col1" ], datetime_series .rename ("col1" ))
300
300
301
- exp = pd . Series (
301
+ exp = Series (
302
302
np .concatenate ([[np .nan ] * 5 , datetime_series_short .values ]),
303
303
index = datetime_series .index ,
304
304
name = "col2" ,
@@ -325,7 +325,7 @@ def test_constructor_dict(self):
325
325
326
326
# Length-one dict micro-optimization
327
327
frame = DataFrame ({"A" : {"1" : 1 , "2" : 2 }})
328
- tm .assert_index_equal (frame .index , pd . Index (["1" , "2" ]))
328
+ tm .assert_index_equal (frame .index , Index (["1" , "2" ]))
329
329
330
330
# empty dict plus index
331
331
idx = Index ([0 , 1 , 2 ])
@@ -418,8 +418,8 @@ def test_constructor_dict_order_insertion(self):
418
418
419
419
def test_constructor_dict_nan_key_and_columns (self ):
420
420
# GH 16894
421
- result = pd . DataFrame ({np .nan : [1 , 2 ], 2 : [2 , 3 ]}, columns = [np .nan , 2 ])
422
- expected = pd . DataFrame ([[1 , 2 ], [2 , 3 ]], columns = [np .nan , 2 ])
421
+ result = DataFrame ({np .nan : [1 , 2 ], 2 : [2 , 3 ]}, columns = [np .nan , 2 ])
422
+ expected = DataFrame ([[1 , 2 ], [2 , 3 ]], columns = [np .nan , 2 ])
423
423
tm .assert_frame_equal (result , expected )
424
424
425
425
def test_constructor_multi_index (self ):
@@ -428,29 +428,29 @@ def test_constructor_multi_index(self):
428
428
tuples = [(2 , 3 ), (3 , 3 ), (3 , 3 )]
429
429
mi = MultiIndex .from_tuples (tuples )
430
430
df = DataFrame (index = mi , columns = mi )
431
- assert pd . isna (df ).values .ravel ().all ()
431
+ assert isna (df ).values .ravel ().all ()
432
432
433
433
tuples = [(3 , 3 ), (2 , 3 ), (3 , 3 )]
434
434
mi = MultiIndex .from_tuples (tuples )
435
435
df = DataFrame (index = mi , columns = mi )
436
- assert pd . isna (df ).values .ravel ().all ()
436
+ assert isna (df ).values .ravel ().all ()
437
437
438
438
def test_constructor_2d_index (self ):
439
439
# GH 25416
440
440
# handling of 2d index in construction
441
- df = pd . DataFrame ([[1 ]], columns = [[1 ]], index = [1 , 2 ])
442
- expected = pd . DataFrame (
441
+ df = DataFrame ([[1 ]], columns = [[1 ]], index = [1 , 2 ])
442
+ expected = DataFrame (
443
443
[1 , 1 ],
444
444
index = pd .Int64Index ([1 , 2 ], dtype = "int64" ),
445
- columns = pd . MultiIndex (levels = [[1 ]], codes = [[0 ]]),
445
+ columns = MultiIndex (levels = [[1 ]], codes = [[0 ]]),
446
446
)
447
447
tm .assert_frame_equal (df , expected )
448
448
449
- df = pd . DataFrame ([[1 ]], columns = [[1 ]], index = [[1 , 2 ]])
450
- expected = pd . DataFrame (
449
+ df = DataFrame ([[1 ]], columns = [[1 ]], index = [[1 , 2 ]])
450
+ expected = DataFrame (
451
451
[1 , 1 ],
452
- index = pd . MultiIndex (levels = [[1 , 2 ]], codes = [[0 , 1 ]]),
453
- columns = pd . MultiIndex (levels = [[1 ]], codes = [[0 ]]),
452
+ index = MultiIndex (levels = [[1 , 2 ]], codes = [[0 , 1 ]]),
453
+ columns = MultiIndex (levels = [[1 ]], codes = [[0 ]]),
454
454
)
455
455
tm .assert_frame_equal (df , expected )
456
456
@@ -471,7 +471,7 @@ def test_constructor_error_msgs(self):
471
471
DataFrame (
472
472
np .arange (12 ).reshape ((4 , 3 )),
473
473
columns = ["foo" , "bar" , "baz" ],
474
- index = pd . date_range ("2000-01-01" , periods = 3 ),
474
+ index = date_range ("2000-01-01" , periods = 3 ),
475
475
)
476
476
477
477
arr = np .array ([[4 , 5 , 6 ]])
@@ -713,14 +713,12 @@ def test_constructor_period(self):
713
713
# PeriodIndex
714
714
a = pd .PeriodIndex (["2012-01" , "NaT" , "2012-04" ], freq = "M" )
715
715
b = pd .PeriodIndex (["2012-02-01" , "2012-03-01" , "NaT" ], freq = "D" )
716
- df = pd . DataFrame ({"a" : a , "b" : b })
716
+ df = DataFrame ({"a" : a , "b" : b })
717
717
assert df ["a" ].dtype == a .dtype
718
718
assert df ["b" ].dtype == b .dtype
719
719
720
720
# list of periods
721
- df = pd .DataFrame (
722
- {"a" : a .astype (object ).tolist (), "b" : b .astype (object ).tolist ()}
723
- )
721
+ df = DataFrame ({"a" : a .astype (object ).tolist (), "b" : b .astype (object ).tolist ()})
724
722
assert df ["a" ].dtype == a .dtype
725
723
assert df ["b" ].dtype == b .dtype
726
724
@@ -882,8 +880,8 @@ def test_constructor_maskedarray_nonfloat(self):
882
880
def test_constructor_maskedarray_hardened (self ):
883
881
# Check numpy masked arrays with hard masks -- from GH24574
884
882
mat_hard = ma .masked_all ((2 , 2 ), dtype = float ).harden_mask ()
885
- result = pd . DataFrame (mat_hard , columns = ["A" , "B" ], index = [1 , 2 ])
886
- expected = pd . DataFrame (
883
+ result = DataFrame (mat_hard , columns = ["A" , "B" ], index = [1 , 2 ])
884
+ expected = DataFrame (
887
885
{"A" : [np .nan , np .nan ], "B" : [np .nan , np .nan ]},
888
886
columns = ["A" , "B" ],
889
887
index = [1 , 2 ],
@@ -892,8 +890,8 @@ def test_constructor_maskedarray_hardened(self):
892
890
tm .assert_frame_equal (result , expected )
893
891
# Check case where mask is hard but no data are masked
894
892
mat_hard = ma .ones ((2 , 2 ), dtype = float ).harden_mask ()
895
- result = pd . DataFrame (mat_hard , columns = ["A" , "B" ], index = [1 , 2 ])
896
- expected = pd . DataFrame (
893
+ result = DataFrame (mat_hard , columns = ["A" , "B" ], index = [1 , 2 ])
894
+ expected = DataFrame (
897
895
{"A" : [1.0 , 1.0 ], "B" : [1.0 , 1.0 ]},
898
896
columns = ["A" , "B" ],
899
897
index = [1 , 2 ],
@@ -907,8 +905,8 @@ def test_constructor_maskedrecarray_dtype(self):
907
905
np .ma .zeros (5 , dtype = [("date" , "<f8" ), ("price" , "<f8" )]), mask = [False ] * 5
908
906
)
909
907
data = data .view (mrecords .mrecarray )
910
- result = pd . DataFrame (data , dtype = int )
911
- expected = pd . DataFrame (np .zeros ((5 , 2 ), dtype = int ), columns = ["date" , "price" ])
908
+ result = DataFrame (data , dtype = int )
909
+ expected = DataFrame (np .zeros ((5 , 2 ), dtype = int ), columns = ["date" , "price" ])
912
910
tm .assert_frame_equal (result , expected )
913
911
914
912
def test_constructor_mrecarray (self ):
@@ -1268,9 +1266,9 @@ def test_constructor_list_of_series(self):
1268
1266
tm .assert_frame_equal (result , expected )
1269
1267
1270
1268
def test_constructor_list_of_series_aligned_index (self ):
1271
- series = [pd . Series (i , index = ["b" , "a" , "c" ], name = str (i )) for i in range (3 )]
1272
- result = pd . DataFrame (series )
1273
- expected = pd . DataFrame (
1269
+ series = [Series (i , index = ["b" , "a" , "c" ], name = str (i )) for i in range (3 )]
1270
+ result = DataFrame (series )
1271
+ expected = DataFrame (
1274
1272
{"b" : [0 , 1 , 2 ], "a" : [0 , 1 , 2 ], "c" : [0 , 1 , 2 ]},
1275
1273
columns = ["b" , "a" , "c" ],
1276
1274
index = ["0" , "1" , "2" ],
@@ -1497,12 +1495,12 @@ def test_constructor_Series_named_and_columns(self):
1497
1495
s1 = Series (range (5 ), name = 1 )
1498
1496
1499
1497
# matching name and column gives standard frame
1500
- tm .assert_frame_equal (pd . DataFrame (s0 , columns = [0 ]), s0 .to_frame ())
1501
- tm .assert_frame_equal (pd . DataFrame (s1 , columns = [1 ]), s1 .to_frame ())
1498
+ tm .assert_frame_equal (DataFrame (s0 , columns = [0 ]), s0 .to_frame ())
1499
+ tm .assert_frame_equal (DataFrame (s1 , columns = [1 ]), s1 .to_frame ())
1502
1500
1503
1501
# non-matching produces empty frame
1504
- assert pd . DataFrame (s0 , columns = [1 ]).empty
1505
- assert pd . DataFrame (s1 , columns = [0 ]).empty
1502
+ assert DataFrame (s0 , columns = [1 ]).empty
1503
+ assert DataFrame (s1 , columns = [0 ]).empty
1506
1504
1507
1505
def test_constructor_Series_differently_indexed (self ):
1508
1506
# name
@@ -1981,7 +1979,7 @@ def test_from_records_to_records(self):
1981
1979
# TODO(wesm): unused
1982
1980
frame = DataFrame .from_records (arr ) # noqa
1983
1981
1984
- index = pd . Index (np .arange (len (arr ))[::- 1 ])
1982
+ index = Index (np .arange (len (arr ))[::- 1 ])
1985
1983
indexed_frame = DataFrame .from_records (arr , index = index )
1986
1984
tm .assert_index_equal (indexed_frame .index , index )
1987
1985
@@ -2280,7 +2278,7 @@ def test_from_records_sequencelike(self):
2280
2278
# empty case
2281
2279
result = DataFrame .from_records ([], columns = ["foo" , "bar" , "baz" ])
2282
2280
assert len (result ) == 0
2283
- tm .assert_index_equal (result .columns , pd . Index (["foo" , "bar" , "baz" ]))
2281
+ tm .assert_index_equal (result .columns , Index (["foo" , "bar" , "baz" ]))
2284
2282
2285
2283
result = DataFrame .from_records ([])
2286
2284
assert len (result ) == 0
@@ -2439,20 +2437,20 @@ def test_datetime_date_tuple_columns_from_dict(self):
2439
2437
v = date .today ()
2440
2438
tup = v , v
2441
2439
result = DataFrame ({tup : Series (range (3 ), index = range (3 ))}, columns = [tup ])
2442
- expected = DataFrame ([0 , 1 , 2 ], columns = pd . Index (pd . Series ([tup ])))
2440
+ expected = DataFrame ([0 , 1 , 2 ], columns = Index (Series ([tup ])))
2443
2441
tm .assert_frame_equal (result , expected )
2444
2442
2445
2443
def test_construct_with_two_categoricalindex_series (self ):
2446
2444
# GH 14600
2447
- s1 = pd . Series (
2445
+ s1 = Series (
2448
2446
[39 , 6 , 4 ], index = pd .CategoricalIndex (["female" , "male" , "unknown" ])
2449
2447
)
2450
- s2 = pd . Series (
2448
+ s2 = Series (
2451
2449
[2 , 152 , 2 , 242 , 150 ],
2452
2450
index = pd .CategoricalIndex (["f" , "female" , "m" , "male" , "unknown" ]),
2453
2451
)
2454
- result = pd . DataFrame ([s1 , s2 ])
2455
- expected = pd . DataFrame (
2452
+ result = DataFrame ([s1 , s2 ])
2453
+ expected = DataFrame (
2456
2454
np .array (
2457
2455
[[np .nan , 39.0 , np .nan , 6.0 , 4.0 ], [2.0 , 152.0 , 2.0 , 242.0 , 150.0 ]]
2458
2456
),
@@ -2551,19 +2549,19 @@ def test_nested_dict_construction(self):
2551
2549
"Nevada" : {2001 : 2.4 , 2002 : 2.9 },
2552
2550
"Ohio" : {2000 : 1.5 , 2001 : 1.7 , 2002 : 3.6 },
2553
2551
}
2554
- result = pd . DataFrame (pop , index = [2001 , 2002 , 2003 ], columns = columns )
2555
- expected = pd . DataFrame (
2552
+ result = DataFrame (pop , index = [2001 , 2002 , 2003 ], columns = columns )
2553
+ expected = DataFrame (
2556
2554
[(2.4 , 1.7 ), (2.9 , 3.6 ), (np .nan , np .nan )],
2557
2555
columns = columns ,
2558
- index = pd . Index ([2001 , 2002 , 2003 ]),
2556
+ index = Index ([2001 , 2002 , 2003 ]),
2559
2557
)
2560
2558
tm .assert_frame_equal (result , expected )
2561
2559
2562
2560
def test_from_tzaware_object_array (self ):
2563
2561
# GH#26825 2D object array of tzaware timestamps should not raise
2564
- dti = pd . date_range ("2016-04-05 04:30" , periods = 3 , tz = "UTC" )
2562
+ dti = date_range ("2016-04-05 04:30" , periods = 3 , tz = "UTC" )
2565
2563
data = dti ._data .astype (object ).reshape (1 , - 1 )
2566
- df = pd . DataFrame (data )
2564
+ df = DataFrame (data )
2567
2565
assert df .shape == (1 , 3 )
2568
2566
assert (df .dtypes == dti .dtype ).all ()
2569
2567
assert (df == dti ).all ().all ()
@@ -2602,7 +2600,7 @@ def test_from_tzaware_mixed_object_array(self):
2602
2600
def test_from_2d_ndarray_with_dtype (self ):
2603
2601
# GH#12513
2604
2602
array_dim2 = np .arange (10 ).reshape ((5 , 2 ))
2605
- df = pd . DataFrame (array_dim2 , dtype = "datetime64[ns, UTC]" )
2603
+ df = DataFrame (array_dim2 , dtype = "datetime64[ns, UTC]" )
2606
2604
2607
- expected = pd . DataFrame (array_dim2 ).astype ("datetime64[ns, UTC]" )
2605
+ expected = DataFrame (array_dim2 ).astype ("datetime64[ns, UTC]" )
2608
2606
tm .assert_frame_equal (df , expected )
0 commit comments