Skip to content

CLN: Remove unnecessary uses of pd. in tests #18277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pandas/tests/indexing/test_chaining_and_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ def random_text(nobs=100):
def test_setting_with_copy_bug(self):

# operating on a copy
df = pd.DataFrame({'a': list(range(4)),
'b': list('ab..'),
'c': ['a', 'b', np.nan, 'd']})
df = DataFrame({'a': list(range(4)),
'b': list('ab..'),
'c': ['a', 'b', np.nan, 'd']})
mask = pd.isna(df.c)

def f():
Expand Down
28 changes: 13 additions & 15 deletions pandas/tests/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def test_setitem_with_datetime_tz(self):
# support .loc with alignment and tz-aware DatetimeIndex
mask = np.array([True, False, True, False])

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

result = df.copy()
result.loc[mask, :] = df.loc[mask, :]
Expand All @@ -22,8 +22,8 @@ def test_setitem_with_datetime_tz(self):
result.loc[mask] = df.loc[mask]
tm.assert_frame_equal(result, df)

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

result = df.copy()
result.loc[mask, :] = df.loc[mask, :]
Expand Down Expand Up @@ -127,10 +127,9 @@ def test_indexing_with_datetimeindex_tz(self):

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

ser = pd.Series(range(2), index=index,
dtype='int64')
ser = Series(range(2), index=index, dtype='int64')

# list-like indexing

Expand All @@ -141,7 +140,7 @@ def test_indexing_with_datetimeindex_tz(self):
# setitem
result = ser.copy()
result[sel] = 1
expected = pd.Series(1, index=index)
expected = Series(1, index=index)
tm.assert_series_equal(result, expected)

# .loc getitem
Expand All @@ -150,7 +149,7 @@ def test_indexing_with_datetimeindex_tz(self):
# .loc setitem
result = ser.copy()
result.loc[sel] = 1
expected = pd.Series(1, index=index)
expected = Series(1, index=index)
tm.assert_series_equal(result, expected)

# single element indexing
Expand All @@ -161,7 +160,7 @@ def test_indexing_with_datetimeindex_tz(self):
# setitem
result = ser.copy()
result[index[1]] = 5
expected = pd.Series([0, 5], index=index)
expected = Series([0, 5], index=index)
tm.assert_series_equal(result, expected)

# .loc getitem
Expand All @@ -170,16 +169,15 @@ def test_indexing_with_datetimeindex_tz(self):
# .loc setitem
result = ser.copy()
result.loc[index[1]] = 5
expected = pd.Series([0, 5], index=index)
expected = Series([0, 5], index=index)
tm.assert_series_equal(result, expected)

def test_partial_setting_with_datetimelike_dtype(self):

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

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

df = pd.DataFrame()
df = DataFrame()
df.loc[conv(dt1), 'one'] = 100
df.loc[conv(dt2), 'one'] = 200

Expand Down
13 changes: 5 additions & 8 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def test_iloc_getitem_neg_int_can_reach_first_index(self):
tm.assert_series_equal(result, expected)

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

Expand Down Expand Up @@ -285,9 +285,7 @@ def test_iloc_setitem(self):
def test_iloc_setitem_int_multiindex_series(
self, data, indexes, values, expected_k):
# GH17148
df = pd.DataFrame(
data=data,
columns=['i', 'j', 'k'])
df = DataFrame(data=data, columns=['i', 'j', 'k'])
df = df.set_index(['i', 'j'])

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

df3 = pd.concat([df, 2 * df, 3 * df])
df3 = concat([df, 2 * df, 3 * df])
result = df3.iloc[idx]

tm.assert_frame_equal(result, expected)

df2 = DataFrame({'A': [0.1] * 1000, 'B': [1] * 1000})
df2 = pd.concat([df2, 2 * df2, 3 * df2])
df2 = concat([df2, 2 * df2, 3 * df2])

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

expected = DataFrame(new_list)
expected = pd.concat([expected, DataFrame(index=idx[idx > sidx.max()])
])
expected = concat([expected, DataFrame(index=idx[idx > sidx.max()])])
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
result = df2.loc[idx]
tm.assert_frame_equal(result, expected, check_index_type=False)
Expand Down
80 changes: 39 additions & 41 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_inf_upcast(self):
# np.inf should cause an index to convert to float

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

# Test with np.inf in columns
df = pd.DataFrame()
df = DataFrame()
df.loc[0, 0] = 1
df.loc[1, 1] = 2
df.loc[0, np.inf] = 3
Expand Down Expand Up @@ -274,8 +274,8 @@ def test_indexing_mixed_frame_bug(self):

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

with pytest.raises(KeyError):
df[[22, 26, -8]]
Expand Down Expand Up @@ -469,16 +469,15 @@ def test_string_slice(self):
# GH 14424
# string indexing against datetimelike with object
# dtype should properly raises KeyError
df = pd.DataFrame([1], pd.Index([pd.Timestamp('2011-01-01')],
dtype=object))
df = DataFrame([1], Index([pd.Timestamp('2011-01-01')], dtype=object))
assert df.index.is_all_dates
with pytest.raises(KeyError):
df['2011']

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

df = pd.DataFrame()
df = DataFrame()
assert not df.index.is_all_dates
with pytest.raises(KeyError):
df['2011']
Expand Down Expand Up @@ -571,7 +570,7 @@ def test_astype_assignment_with_dups(self):

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

@pytest.mark.parametrize("index,val", [
(pd.Index([0, 1, 2]), 2),
(pd.Index([0, 1, '2']), '2'),
(pd.Index([0, 1, 2, np.inf, 4]), 4),
(pd.Index([0, 1, 2, np.nan, 4]), 4),
(pd.Index([0, 1, 2, np.inf]), np.inf),
(pd.Index([0, 1, 2, np.nan]), np.nan),
(Index([0, 1, 2]), 2),
(Index([0, 1, '2']), '2'),
(Index([0, 1, 2, np.inf, 4]), 4),
(Index([0, 1, 2, np.nan, 4]), 4),
(Index([0, 1, 2, np.inf]), np.inf),
(Index([0, 1, 2, np.nan]), np.nan),
])
def test_index_contains(self, index, val):
assert val in index

@pytest.mark.parametrize("index,val", [
(pd.Index([0, 1, 2]), '2'),
(pd.Index([0, 1, '2']), 2),
(pd.Index([0, 1, 2, np.inf]), 4),
(pd.Index([0, 1, 2, np.nan]), 4),
(pd.Index([0, 1, 2, np.inf]), np.nan),
(pd.Index([0, 1, 2, np.nan]), np.inf),
(Index([0, 1, 2]), '2'),
(Index([0, 1, '2']), 2),
(Index([0, 1, 2, np.inf]), 4),
(Index([0, 1, 2, np.nan]), 4),
(Index([0, 1, 2, np.inf]), np.nan),
(Index([0, 1, 2, np.nan]), np.inf),
# Checking if np.inf in Int64Index should not cause an OverflowError
# Related to GH 16957
(pd.Int64Index([0, 1, 2]), np.inf),
Expand Down Expand Up @@ -705,7 +704,7 @@ def test_float_index_non_scalar_assignment(self):
tm.assert_frame_equal(df, df2)

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

xs = np.arange(20).reshape(5, 4)
cols = ['jim', 'joe', 'jolie', 'joline']
df = pd.DataFrame(xs, columns=cols, index=list('abcde'))
df = DataFrame(xs, columns=cols, index=list('abcde'))

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

def test_indexing_assignment_dict_already_exists(self):
df = pd.DataFrame({'x': [1, 2, 6],
'y': [2, 2, 8],
'z': [-5, 0, 5]}).set_index('z')
df = DataFrame({'x': [1, 2, 6],
'y': [2, 2, 8],
'z': [-5, 0, 5]}).set_index('z')
expected = df.copy()
rhs = dict(x=9, y=99)
df.loc[5] = rhs
Expand All @@ -819,15 +818,15 @@ def test_range_in_series_indexing(self):
# range can cause an indexing error
# GH 11652
for x in [5, 999999, 1000000]:
s = pd.Series(index=range(x))
s = Series(index=range(x))
s.loc[range(1)] = 42
tm.assert_series_equal(s.loc[range(1)], Series(42.0, index=[0]))

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

def test_non_reducing_slice(self):
df = pd.DataFrame([[0, 1], [2, 3]])
df = DataFrame([[0, 1], [2, 3]])

slices = [
# pd.IndexSlice[:, :],
Expand All @@ -841,23 +840,23 @@ def test_non_reducing_slice(self):
slice(None, None, None),
[0, 1],
np.array([0, 1]),
pd.Series([0, 1])
Series([0, 1])
]
for slice_ in slices:
tslice_ = _non_reducing_slice(slice_)
assert isinstance(df.loc[tslice_], DataFrame)

def test_list_slice(self):
# like dataframe getitem
slices = [['A'], pd.Series(['A']), np.array(['A'])]
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}, index=['A', 'B'])
slices = [['A'], Series(['A']), np.array(['A'])]
df = DataFrame({'A': [1, 2], 'B': [3, 4]}, index=['A', 'B'])
expected = pd.IndexSlice[:, ['A']]
for subset in slices:
result = _non_reducing_slice(subset)
tm.assert_frame_equal(df.loc[result], df.loc[expected])

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

def test_partial_boolean_frame_indexing(self):
# GH 17170
df = pd.DataFrame(np.arange(9.).reshape(3, 3),
index=list('abc'),
columns=list('ABC'))
index_df = pd.DataFrame(1, index=list('ab'), columns=list('AB'))
df = DataFrame(np.arange(9.).reshape(3, 3),
index=list('abc'), columns=list('ABC'))
index_df = DataFrame(1, index=list('ab'), columns=list('AB'))
result = df[index_df.notnull()]
expected = pd.DataFrame(np.array([[0., 1., np.nan],
[3., 4., np.nan],
[np.nan] * 3]),
index=list('abc'),
columns=list('ABC'))
expected = DataFrame(np.array([[0., 1., np.nan],
[3., 4., np.nan],
[np.nan] * 3]),
index=list('abc'),
columns=list('ABC'))
tm.assert_frame_equal(result, expected)

def test_no_reference_cycle(self):
df = pd.DataFrame({'a': [0, 1], 'b': [2, 3]})
df = DataFrame({'a': [0, 1], 'b': [2, 3]})
for name in ('loc', 'iloc', 'at', 'iat'):
getattr(df, name)
with catch_warnings(record=True):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_indexing_slow.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def loop(mi, df, keys):
keys += list(map(lambda t: t[:-1], vals[::n // m]))

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

for frame in a, b:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def test_non_unique(self):

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

s = pd.Series(range(len(idx)), index=idx)
s = Series(range(len(idx)), index=idx)

result = s.loc[Interval(1, 3)]
assert result == 0
Expand Down
Loading