Skip to content

Commit fa4ebd4

Browse files
jschendeljreback
authored andcommitted
CLN: Remove unnecessary uses of pd. in tests (#18277)
1 parent 9aaf50a commit fa4ebd4

18 files changed

+851
-954
lines changed

pandas/tests/indexing/test_chaining_and_caching.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,9 @@ def random_text(nobs=100):
318318
def test_setting_with_copy_bug(self):
319319

320320
# operating on a copy
321-
df = pd.DataFrame({'a': list(range(4)),
322-
'b': list('ab..'),
323-
'c': ['a', 'b', np.nan, 'd']})
321+
df = DataFrame({'a': list(range(4)),
322+
'b': list('ab..'),
323+
'c': ['a', 'b', np.nan, 'd']})
324324
mask = pd.isna(df.c)
325325

326326
def f():

pandas/tests/indexing/test_datetime.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def test_setitem_with_datetime_tz(self):
1111
# support .loc with alignment and tz-aware DatetimeIndex
1212
mask = np.array([True, False, True, False])
1313

14-
idx = pd.date_range('20010101', periods=4, tz='UTC')
15-
df = pd.DataFrame({'a': np.arange(4)}, index=idx).astype('float64')
14+
idx = date_range('20010101', periods=4, tz='UTC')
15+
df = DataFrame({'a': np.arange(4)}, index=idx).astype('float64')
1616

1717
result = df.copy()
1818
result.loc[mask, :] = df.loc[mask, :]
@@ -22,8 +22,8 @@ def test_setitem_with_datetime_tz(self):
2222
result.loc[mask] = df.loc[mask]
2323
tm.assert_frame_equal(result, df)
2424

25-
idx = pd.date_range('20010101', periods=4)
26-
df = pd.DataFrame({'a': np.arange(4)}, index=idx).astype('float64')
25+
idx = date_range('20010101', periods=4)
26+
df = DataFrame({'a': np.arange(4)}, index=idx).astype('float64')
2727

2828
result = df.copy()
2929
result.loc[mask, :] = df.loc[mask, :]
@@ -127,10 +127,9 @@ def test_indexing_with_datetimeindex_tz(self):
127127

128128
# GH 12050
129129
# indexing on a series with a datetimeindex with tz
130-
index = pd.date_range('2015-01-01', periods=2, tz='utc')
130+
index = date_range('2015-01-01', periods=2, tz='utc')
131131

132-
ser = pd.Series(range(2), index=index,
133-
dtype='int64')
132+
ser = Series(range(2), index=index, dtype='int64')
134133

135134
# list-like indexing
136135

@@ -141,7 +140,7 @@ def test_indexing_with_datetimeindex_tz(self):
141140
# setitem
142141
result = ser.copy()
143142
result[sel] = 1
144-
expected = pd.Series(1, index=index)
143+
expected = Series(1, index=index)
145144
tm.assert_series_equal(result, expected)
146145

147146
# .loc getitem
@@ -150,7 +149,7 @@ def test_indexing_with_datetimeindex_tz(self):
150149
# .loc setitem
151150
result = ser.copy()
152151
result.loc[sel] = 1
153-
expected = pd.Series(1, index=index)
152+
expected = Series(1, index=index)
154153
tm.assert_series_equal(result, expected)
155154

156155
# single element indexing
@@ -161,7 +160,7 @@ def test_indexing_with_datetimeindex_tz(self):
161160
# setitem
162161
result = ser.copy()
163162
result[index[1]] = 5
164-
expected = pd.Series([0, 5], index=index)
163+
expected = Series([0, 5], index=index)
165164
tm.assert_series_equal(result, expected)
166165

167166
# .loc getitem
@@ -170,16 +169,15 @@ def test_indexing_with_datetimeindex_tz(self):
170169
# .loc setitem
171170
result = ser.copy()
172171
result.loc[index[1]] = 5
173-
expected = pd.Series([0, 5], index=index)
172+
expected = Series([0, 5], index=index)
174173
tm.assert_series_equal(result, expected)
175174

176175
def test_partial_setting_with_datetimelike_dtype(self):
177176

178177
# GH9478
179178
# a datetimeindex alignment issue with partial setting
180-
df = pd.DataFrame(np.arange(6.).reshape(3, 2), columns=list('AB'),
181-
index=pd.date_range('1/1/2000', periods=3,
182-
freq='1H'))
179+
df = DataFrame(np.arange(6.).reshape(3, 2), columns=list('AB'),
180+
index=date_range('1/1/2000', periods=3, freq='1H'))
183181
expected = df.copy()
184182
expected['C'] = [expected.index[0]] + [pd.NaT, pd.NaT]
185183

@@ -196,7 +194,7 @@ def test_loc_setitem_datetime(self):
196194
for conv in [lambda x: x, lambda x: x.to_datetime64(),
197195
lambda x: x.to_pydatetime(), lambda x: np.datetime64(x)]:
198196

199-
df = pd.DataFrame()
197+
df = DataFrame()
200198
df.loc[conv(dt1), 'one'] = 100
201199
df.loc[conv(dt2), 'one'] = 200
202200

pandas/tests/indexing/test_iloc.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def test_iloc_getitem_neg_int_can_reach_first_index(self):
173173
tm.assert_series_equal(result, expected)
174174

175175
# check the length 1 Series case highlighted in GH10547
176-
expected = pd.Series(['a'], index=['A'])
176+
expected = Series(['a'], index=['A'])
177177
result = expected.iloc[[-1]]
178178
tm.assert_series_equal(result, expected)
179179

@@ -285,9 +285,7 @@ def test_iloc_setitem(self):
285285
def test_iloc_setitem_int_multiindex_series(
286286
self, data, indexes, values, expected_k):
287287
# GH17148
288-
df = pd.DataFrame(
289-
data=data,
290-
columns=['i', 'j', 'k'])
288+
df = DataFrame(data=data, columns=['i', 'j', 'k'])
291289
df = df.set_index(['i', 'j'])
292290

293291
series = df.k.copy()
@@ -597,13 +595,13 @@ def test_iloc_non_unique_indexing(self):
597595
idx = np.array(lrange(30)) * 99
598596
expected = df.iloc[idx]
599597

600-
df3 = pd.concat([df, 2 * df, 3 * df])
598+
df3 = concat([df, 2 * df, 3 * df])
601599
result = df3.iloc[idx]
602600

603601
tm.assert_frame_equal(result, expected)
604602

605603
df2 = DataFrame({'A': [0.1] * 1000, 'B': [1] * 1000})
606-
df2 = pd.concat([df2, 2 * df2, 3 * df2])
604+
df2 = concat([df2, 2 * df2, 3 * df2])
607605

608606
sidx = df2.index.to_series()
609607
expected = df2.iloc[idx[idx <= sidx.max()]]
@@ -615,8 +613,7 @@ def test_iloc_non_unique_indexing(self):
615613
new_list.append(s * 3)
616614

617615
expected = DataFrame(new_list)
618-
expected = pd.concat([expected, DataFrame(index=idx[idx > sidx.max()])
619-
])
616+
expected = concat([expected, DataFrame(index=idx[idx > sidx.max()])])
620617
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
621618
result = df2.loc[idx]
622619
tm.assert_frame_equal(result, expected, check_index_type=False)

pandas/tests/indexing/test_indexing.py

+39-41
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_inf_upcast(self):
7070
# np.inf should cause an index to convert to float
7171

7272
# Test with np.inf in rows
73-
df = pd.DataFrame(columns=[0])
73+
df = DataFrame(columns=[0])
7474
df.loc[1] = 1
7575
df.loc[2] = 2
7676
df.loc[np.inf] = 3
@@ -83,7 +83,7 @@ def test_inf_upcast(self):
8383
tm.assert_index_equal(result, expected)
8484

8585
# Test with np.inf in columns
86-
df = pd.DataFrame()
86+
df = DataFrame()
8787
df.loc[0, 0] = 1
8888
df.loc[1, 1] = 2
8989
df.loc[0, np.inf] = 3
@@ -274,8 +274,8 @@ def test_indexing_mixed_frame_bug(self):
274274

275275
def test_multitype_list_index_access(self):
276276
# GH 10610
277-
df = pd.DataFrame(np.random.random((10, 5)),
278-
columns=["a"] + [20, 21, 22, 23])
277+
df = DataFrame(np.random.random((10, 5)),
278+
columns=["a"] + [20, 21, 22, 23])
279279

280280
with pytest.raises(KeyError):
281281
df[[22, 26, -8]]
@@ -469,16 +469,15 @@ def test_string_slice(self):
469469
# GH 14424
470470
# string indexing against datetimelike with object
471471
# dtype should properly raises KeyError
472-
df = pd.DataFrame([1], pd.Index([pd.Timestamp('2011-01-01')],
473-
dtype=object))
472+
df = DataFrame([1], Index([pd.Timestamp('2011-01-01')], dtype=object))
474473
assert df.index.is_all_dates
475474
with pytest.raises(KeyError):
476475
df['2011']
477476

478477
with pytest.raises(KeyError):
479478
df.loc['2011', 0]
480479

481-
df = pd.DataFrame()
480+
df = DataFrame()
482481
assert not df.index.is_all_dates
483482
with pytest.raises(KeyError):
484483
df['2011']
@@ -571,7 +570,7 @@ def test_astype_assignment_with_dups(self):
571570

572571
# GH 4686
573572
# assignment with dups that has a dtype change
574-
cols = pd.MultiIndex.from_tuples([('A', '1'), ('B', '1'), ('A', '2')])
573+
cols = MultiIndex.from_tuples([('A', '1'), ('B', '1'), ('A', '2')])
575574
df = DataFrame(np.arange(3).reshape((1, 3)),
576575
columns=cols, dtype=object)
577576
index = df.index.copy()
@@ -584,23 +583,23 @@ def test_astype_assignment_with_dups(self):
584583
# expected = Series({'float64': 2, 'object': 1}).sort_index()
585584

586585
@pytest.mark.parametrize("index,val", [
587-
(pd.Index([0, 1, 2]), 2),
588-
(pd.Index([0, 1, '2']), '2'),
589-
(pd.Index([0, 1, 2, np.inf, 4]), 4),
590-
(pd.Index([0, 1, 2, np.nan, 4]), 4),
591-
(pd.Index([0, 1, 2, np.inf]), np.inf),
592-
(pd.Index([0, 1, 2, np.nan]), np.nan),
586+
(Index([0, 1, 2]), 2),
587+
(Index([0, 1, '2']), '2'),
588+
(Index([0, 1, 2, np.inf, 4]), 4),
589+
(Index([0, 1, 2, np.nan, 4]), 4),
590+
(Index([0, 1, 2, np.inf]), np.inf),
591+
(Index([0, 1, 2, np.nan]), np.nan),
593592
])
594593
def test_index_contains(self, index, val):
595594
assert val in index
596595

597596
@pytest.mark.parametrize("index,val", [
598-
(pd.Index([0, 1, 2]), '2'),
599-
(pd.Index([0, 1, '2']), 2),
600-
(pd.Index([0, 1, 2, np.inf]), 4),
601-
(pd.Index([0, 1, 2, np.nan]), 4),
602-
(pd.Index([0, 1, 2, np.inf]), np.nan),
603-
(pd.Index([0, 1, 2, np.nan]), np.inf),
597+
(Index([0, 1, 2]), '2'),
598+
(Index([0, 1, '2']), 2),
599+
(Index([0, 1, 2, np.inf]), 4),
600+
(Index([0, 1, 2, np.nan]), 4),
601+
(Index([0, 1, 2, np.inf]), np.nan),
602+
(Index([0, 1, 2, np.nan]), np.inf),
604603
# Checking if np.inf in Int64Index should not cause an OverflowError
605604
# Related to GH 16957
606605
(pd.Int64Index([0, 1, 2]), np.inf),
@@ -705,7 +704,7 @@ def test_float_index_non_scalar_assignment(self):
705704
tm.assert_frame_equal(df, df2)
706705

707706
def test_float_index_at_iat(self):
708-
s = pd.Series([1, 2, 3], index=[0.1, 0.2, 0.3])
707+
s = Series([1, 2, 3], index=[0.1, 0.2, 0.3])
709708
for el, item in s.iteritems():
710709
assert s.at[el] == item
711710
for i in range(len(s)):
@@ -744,7 +743,7 @@ def run_tests(df, rhs, right):
744743

745744
xs = np.arange(20).reshape(5, 4)
746745
cols = ['jim', 'joe', 'jolie', 'joline']
747-
df = pd.DataFrame(xs, columns=cols, index=list('abcde'))
746+
df = DataFrame(xs, columns=cols, index=list('abcde'))
748747

749748
# right hand side; permute the indices and multiplpy by -2
750749
rhs = -2 * df.iloc[3:0:-1, 2:0:-1]
@@ -795,9 +794,9 @@ def test_slice_with_zero_step_raises(self):
795794
lambda: s.ix[::0])
796795

797796
def test_indexing_assignment_dict_already_exists(self):
798-
df = pd.DataFrame({'x': [1, 2, 6],
799-
'y': [2, 2, 8],
800-
'z': [-5, 0, 5]}).set_index('z')
797+
df = DataFrame({'x': [1, 2, 6],
798+
'y': [2, 2, 8],
799+
'z': [-5, 0, 5]}).set_index('z')
801800
expected = df.copy()
802801
rhs = dict(x=9, y=99)
803802
df.loc[5] = rhs
@@ -819,15 +818,15 @@ def test_range_in_series_indexing(self):
819818
# range can cause an indexing error
820819
# GH 11652
821820
for x in [5, 999999, 1000000]:
822-
s = pd.Series(index=range(x))
821+
s = Series(index=range(x))
823822
s.loc[range(1)] = 42
824823
tm.assert_series_equal(s.loc[range(1)], Series(42.0, index=[0]))
825824

826825
s.loc[range(2)] = 43
827826
tm.assert_series_equal(s.loc[range(2)], Series(43.0, index=[0, 1]))
828827

829828
def test_non_reducing_slice(self):
830-
df = pd.DataFrame([[0, 1], [2, 3]])
829+
df = DataFrame([[0, 1], [2, 3]])
831830

832831
slices = [
833832
# pd.IndexSlice[:, :],
@@ -841,23 +840,23 @@ def test_non_reducing_slice(self):
841840
slice(None, None, None),
842841
[0, 1],
843842
np.array([0, 1]),
844-
pd.Series([0, 1])
843+
Series([0, 1])
845844
]
846845
for slice_ in slices:
847846
tslice_ = _non_reducing_slice(slice_)
848847
assert isinstance(df.loc[tslice_], DataFrame)
849848

850849
def test_list_slice(self):
851850
# like dataframe getitem
852-
slices = [['A'], pd.Series(['A']), np.array(['A'])]
853-
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}, index=['A', 'B'])
851+
slices = [['A'], Series(['A']), np.array(['A'])]
852+
df = DataFrame({'A': [1, 2], 'B': [3, 4]}, index=['A', 'B'])
854853
expected = pd.IndexSlice[:, ['A']]
855854
for subset in slices:
856855
result = _non_reducing_slice(subset)
857856
tm.assert_frame_equal(df.loc[result], df.loc[expected])
858857

859858
def test_maybe_numeric_slice(self):
860-
df = pd.DataFrame({'A': [1, 2], 'B': ['c', 'd'], 'C': [True, False]})
859+
df = DataFrame({'A': [1, 2], 'B': ['c', 'd'], 'C': [True, False]})
861860
result = _maybe_numeric_slice(df, slice_=None)
862861
expected = pd.IndexSlice[:, ['A']]
863862
assert result == expected
@@ -870,20 +869,19 @@ def test_maybe_numeric_slice(self):
870869

871870
def test_partial_boolean_frame_indexing(self):
872871
# GH 17170
873-
df = pd.DataFrame(np.arange(9.).reshape(3, 3),
874-
index=list('abc'),
875-
columns=list('ABC'))
876-
index_df = pd.DataFrame(1, index=list('ab'), columns=list('AB'))
872+
df = DataFrame(np.arange(9.).reshape(3, 3),
873+
index=list('abc'), columns=list('ABC'))
874+
index_df = DataFrame(1, index=list('ab'), columns=list('AB'))
877875
result = df[index_df.notnull()]
878-
expected = pd.DataFrame(np.array([[0., 1., np.nan],
879-
[3., 4., np.nan],
880-
[np.nan] * 3]),
881-
index=list('abc'),
882-
columns=list('ABC'))
876+
expected = DataFrame(np.array([[0., 1., np.nan],
877+
[3., 4., np.nan],
878+
[np.nan] * 3]),
879+
index=list('abc'),
880+
columns=list('ABC'))
883881
tm.assert_frame_equal(result, expected)
884882

885883
def test_no_reference_cycle(self):
886-
df = pd.DataFrame({'a': [0, 1], 'b': [2, 3]})
884+
df = DataFrame({'a': [0, 1], 'b': [2, 3]})
887885
for name in ('loc', 'iloc', 'at', 'iat'):
888886
getattr(df, name)
889887
with catch_warnings(record=True):

pandas/tests/indexing/test_indexing_slow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def loop(mi, df, keys):
7070
keys += list(map(lambda t: t[:-1], vals[::n // m]))
7171

7272
# covers both unique index and non-unique index
73-
df = pd.DataFrame(vals, columns=cols)
73+
df = DataFrame(vals, columns=cols)
7474
a, b = pd.concat([df, df]), df.drop_duplicates(subset=cols[:-1])
7575

7676
for frame in a, b:

pandas/tests/indexing/test_interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def test_non_unique(self):
179179

180180
idx = IntervalIndex.from_tuples([(1, 3), (3, 7)])
181181

182-
s = pd.Series(range(len(idx)), index=idx)
182+
s = Series(range(len(idx)), index=idx)
183183

184184
result = s.loc[Interval(1, 3)]
185185
assert result == 0

0 commit comments

Comments
 (0)