Skip to content

Commit 1ce5646

Browse files
dsaxtonSeeminSyed
authored andcommitted
CLN: Clean frame/test_constructors.py (pandas-dev#32610)
1 parent aca6e4c commit 1ce5646

File tree

1 file changed

+52
-54
lines changed

1 file changed

+52
-54
lines changed

pandas/tests/frame/test_constructors.py

+52-54
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@
4747
class TestDataFrameConstructors:
4848
def test_series_with_name_not_matching_column(self):
4949
# 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)
5252

53-
result = pd.DataFrame(x, columns=[0])
54-
expected = pd.DataFrame([], columns=[0])
53+
result = DataFrame(x, columns=[0])
54+
expected = DataFrame([], columns=[0])
5555
tm.assert_frame_equal(result, expected)
5656

57-
result = pd.DataFrame(y, columns=[1])
58-
expected = pd.DataFrame([], columns=[1])
57+
result = DataFrame(y, columns=[1])
58+
expected = DataFrame([], columns=[1])
5959
tm.assert_frame_equal(result, expected)
6060

6161
@pytest.mark.parametrize(
@@ -126,7 +126,7 @@ def test_constructor_cast_failure(self):
126126
def test_constructor_dtype_copy(self):
127127
orig_df = DataFrame({"col1": [1.0], "col2": [2.0], "col3": [3.0]})
128128

129-
new_df = pd.DataFrame(orig_df, dtype=float, copy=True)
129+
new_df = DataFrame(orig_df, dtype=float, copy=True)
130130

131131
new_df["col1"] = 200.0
132132
assert orig_df["col1"][0] == 1.0
@@ -220,10 +220,10 @@ def test_constructor_rec(self, float_frame):
220220
index = float_frame.index
221221

222222
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))
224224

225225
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))
227227
tm.assert_index_equal(df2.index, index)
228228

229229
rng = np.arange(len(rec))[::-1]
@@ -298,7 +298,7 @@ def test_constructor_dict(self):
298298

299299
tm.assert_series_equal(frame["col1"], datetime_series.rename("col1"))
300300

301-
exp = pd.Series(
301+
exp = Series(
302302
np.concatenate([[np.nan] * 5, datetime_series_short.values]),
303303
index=datetime_series.index,
304304
name="col2",
@@ -325,7 +325,7 @@ def test_constructor_dict(self):
325325

326326
# Length-one dict micro-optimization
327327
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"]))
329329

330330
# empty dict plus index
331331
idx = Index([0, 1, 2])
@@ -418,8 +418,8 @@ def test_constructor_dict_order_insertion(self):
418418

419419
def test_constructor_dict_nan_key_and_columns(self):
420420
# 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])
423423
tm.assert_frame_equal(result, expected)
424424

425425
def test_constructor_multi_index(self):
@@ -428,29 +428,29 @@ def test_constructor_multi_index(self):
428428
tuples = [(2, 3), (3, 3), (3, 3)]
429429
mi = MultiIndex.from_tuples(tuples)
430430
df = DataFrame(index=mi, columns=mi)
431-
assert pd.isna(df).values.ravel().all()
431+
assert isna(df).values.ravel().all()
432432

433433
tuples = [(3, 3), (2, 3), (3, 3)]
434434
mi = MultiIndex.from_tuples(tuples)
435435
df = DataFrame(index=mi, columns=mi)
436-
assert pd.isna(df).values.ravel().all()
436+
assert isna(df).values.ravel().all()
437437

438438
def test_constructor_2d_index(self):
439439
# GH 25416
440440
# 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(
443443
[1, 1],
444444
index=pd.Int64Index([1, 2], dtype="int64"),
445-
columns=pd.MultiIndex(levels=[[1]], codes=[[0]]),
445+
columns=MultiIndex(levels=[[1]], codes=[[0]]),
446446
)
447447
tm.assert_frame_equal(df, expected)
448448

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(
451451
[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]]),
454454
)
455455
tm.assert_frame_equal(df, expected)
456456

@@ -471,7 +471,7 @@ def test_constructor_error_msgs(self):
471471
DataFrame(
472472
np.arange(12).reshape((4, 3)),
473473
columns=["foo", "bar", "baz"],
474-
index=pd.date_range("2000-01-01", periods=3),
474+
index=date_range("2000-01-01", periods=3),
475475
)
476476

477477
arr = np.array([[4, 5, 6]])
@@ -713,14 +713,12 @@ def test_constructor_period(self):
713713
# PeriodIndex
714714
a = pd.PeriodIndex(["2012-01", "NaT", "2012-04"], freq="M")
715715
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})
717717
assert df["a"].dtype == a.dtype
718718
assert df["b"].dtype == b.dtype
719719

720720
# 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()})
724722
assert df["a"].dtype == a.dtype
725723
assert df["b"].dtype == b.dtype
726724

@@ -882,8 +880,8 @@ def test_constructor_maskedarray_nonfloat(self):
882880
def test_constructor_maskedarray_hardened(self):
883881
# Check numpy masked arrays with hard masks -- from GH24574
884882
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(
887885
{"A": [np.nan, np.nan], "B": [np.nan, np.nan]},
888886
columns=["A", "B"],
889887
index=[1, 2],
@@ -892,8 +890,8 @@ def test_constructor_maskedarray_hardened(self):
892890
tm.assert_frame_equal(result, expected)
893891
# Check case where mask is hard but no data are masked
894892
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(
897895
{"A": [1.0, 1.0], "B": [1.0, 1.0]},
898896
columns=["A", "B"],
899897
index=[1, 2],
@@ -907,8 +905,8 @@ def test_constructor_maskedrecarray_dtype(self):
907905
np.ma.zeros(5, dtype=[("date", "<f8"), ("price", "<f8")]), mask=[False] * 5
908906
)
909907
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"])
912910
tm.assert_frame_equal(result, expected)
913911

914912
def test_constructor_mrecarray(self):
@@ -1268,9 +1266,9 @@ def test_constructor_list_of_series(self):
12681266
tm.assert_frame_equal(result, expected)
12691267

12701268
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(
12741272
{"b": [0, 1, 2], "a": [0, 1, 2], "c": [0, 1, 2]},
12751273
columns=["b", "a", "c"],
12761274
index=["0", "1", "2"],
@@ -1497,12 +1495,12 @@ def test_constructor_Series_named_and_columns(self):
14971495
s1 = Series(range(5), name=1)
14981496

14991497
# 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())
15021500

15031501
# 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
15061504

15071505
def test_constructor_Series_differently_indexed(self):
15081506
# name
@@ -1981,7 +1979,7 @@ def test_from_records_to_records(self):
19811979
# TODO(wesm): unused
19821980
frame = DataFrame.from_records(arr) # noqa
19831981

1984-
index = pd.Index(np.arange(len(arr))[::-1])
1982+
index = Index(np.arange(len(arr))[::-1])
19851983
indexed_frame = DataFrame.from_records(arr, index=index)
19861984
tm.assert_index_equal(indexed_frame.index, index)
19871985

@@ -2280,7 +2278,7 @@ def test_from_records_sequencelike(self):
22802278
# empty case
22812279
result = DataFrame.from_records([], columns=["foo", "bar", "baz"])
22822280
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"]))
22842282

22852283
result = DataFrame.from_records([])
22862284
assert len(result) == 0
@@ -2439,20 +2437,20 @@ def test_datetime_date_tuple_columns_from_dict(self):
24392437
v = date.today()
24402438
tup = v, v
24412439
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])))
24432441
tm.assert_frame_equal(result, expected)
24442442

24452443
def test_construct_with_two_categoricalindex_series(self):
24462444
# GH 14600
2447-
s1 = pd.Series(
2445+
s1 = Series(
24482446
[39, 6, 4], index=pd.CategoricalIndex(["female", "male", "unknown"])
24492447
)
2450-
s2 = pd.Series(
2448+
s2 = Series(
24512449
[2, 152, 2, 242, 150],
24522450
index=pd.CategoricalIndex(["f", "female", "m", "male", "unknown"]),
24532451
)
2454-
result = pd.DataFrame([s1, s2])
2455-
expected = pd.DataFrame(
2452+
result = DataFrame([s1, s2])
2453+
expected = DataFrame(
24562454
np.array(
24572455
[[np.nan, 39.0, np.nan, 6.0, 4.0], [2.0, 152.0, 2.0, 242.0, 150.0]]
24582456
),
@@ -2551,19 +2549,19 @@ def test_nested_dict_construction(self):
25512549
"Nevada": {2001: 2.4, 2002: 2.9},
25522550
"Ohio": {2000: 1.5, 2001: 1.7, 2002: 3.6},
25532551
}
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(
25562554
[(2.4, 1.7), (2.9, 3.6), (np.nan, np.nan)],
25572555
columns=columns,
2558-
index=pd.Index([2001, 2002, 2003]),
2556+
index=Index([2001, 2002, 2003]),
25592557
)
25602558
tm.assert_frame_equal(result, expected)
25612559

25622560
def test_from_tzaware_object_array(self):
25632561
# 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")
25652563
data = dti._data.astype(object).reshape(1, -1)
2566-
df = pd.DataFrame(data)
2564+
df = DataFrame(data)
25672565
assert df.shape == (1, 3)
25682566
assert (df.dtypes == dti.dtype).all()
25692567
assert (df == dti).all().all()
@@ -2602,7 +2600,7 @@ def test_from_tzaware_mixed_object_array(self):
26022600
def test_from_2d_ndarray_with_dtype(self):
26032601
# GH#12513
26042602
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]")
26062604

2607-
expected = pd.DataFrame(array_dim2).astype("datetime64[ns, UTC]")
2605+
expected = DataFrame(array_dim2).astype("datetime64[ns, UTC]")
26082606
tm.assert_frame_equal(df, expected)

0 commit comments

Comments
 (0)