Skip to content

Commit 8b82bd6

Browse files
committed
CLN: split test_indexing.py
split to: test_ix, test_scalar, test_loc, test_iloc, test_partial
1 parent 23e82eb commit 8b82bd6

File tree

8 files changed

+2672
-2593
lines changed

8 files changed

+2672
-2593
lines changed

pandas/tests/indexing/common.py

+257
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,262 @@
11
""" common utilities """
22

3+
import itertools
4+
from warnings import catch_warnings
5+
import numpy as np
6+
7+
from pandas.compat import lrange
8+
from pandas.types.common import is_scalar
9+
from pandas import Series, DataFrame, Panel, date_range, UInt64Index
10+
from pandas.util import testing as tm
11+
from pandas.formats.printing import pprint_thing
12+
13+
_verbose = False
14+
315

416
def _mklbl(prefix, n):
517
return ["%s%s" % (prefix, i) for i in range(n)]
18+
19+
20+
def _axify(obj, key, axis):
21+
# create a tuple accessor
22+
axes = [slice(None)] * obj.ndim
23+
axes[axis] = key
24+
return tuple(axes)
25+
26+
27+
class Base(object):
28+
""" indexing comprehensive base class """
29+
30+
_objs = set(['series', 'frame', 'panel'])
31+
_typs = set(['ints', 'uints', 'labels', 'mixed',
32+
'ts', 'floats', 'empty', 'ts_rev'])
33+
34+
def setUp(self):
35+
36+
self.series_ints = Series(np.random.rand(4), index=lrange(0, 8, 2))
37+
self.frame_ints = DataFrame(np.random.randn(4, 4),
38+
index=lrange(0, 8, 2),
39+
columns=lrange(0, 12, 3))
40+
self.panel_ints = Panel(np.random.rand(4, 4, 4),
41+
items=lrange(0, 8, 2),
42+
major_axis=lrange(0, 12, 3),
43+
minor_axis=lrange(0, 16, 4))
44+
45+
self.series_uints = Series(np.random.rand(4),
46+
index=UInt64Index(lrange(0, 8, 2)))
47+
self.frame_uints = DataFrame(np.random.randn(4, 4),
48+
index=UInt64Index(lrange(0, 8, 2)),
49+
columns=UInt64Index(lrange(0, 12, 3)))
50+
self.panel_uints = Panel(np.random.rand(4, 4, 4),
51+
items=UInt64Index(lrange(0, 8, 2)),
52+
major_axis=UInt64Index(lrange(0, 12, 3)),
53+
minor_axis=UInt64Index(lrange(0, 16, 4)))
54+
55+
self.series_labels = Series(np.random.randn(4), index=list('abcd'))
56+
self.frame_labels = DataFrame(np.random.randn(4, 4),
57+
index=list('abcd'), columns=list('ABCD'))
58+
self.panel_labels = Panel(np.random.randn(4, 4, 4),
59+
items=list('abcd'),
60+
major_axis=list('ABCD'),
61+
minor_axis=list('ZYXW'))
62+
63+
self.series_mixed = Series(np.random.randn(4), index=[2, 4, 'null', 8])
64+
self.frame_mixed = DataFrame(np.random.randn(4, 4),
65+
index=[2, 4, 'null', 8])
66+
self.panel_mixed = Panel(np.random.randn(4, 4, 4),
67+
items=[2, 4, 'null', 8])
68+
69+
self.series_ts = Series(np.random.randn(4),
70+
index=date_range('20130101', periods=4))
71+
self.frame_ts = DataFrame(np.random.randn(4, 4),
72+
index=date_range('20130101', periods=4))
73+
self.panel_ts = Panel(np.random.randn(4, 4, 4),
74+
items=date_range('20130101', periods=4))
75+
76+
dates_rev = (date_range('20130101', periods=4)
77+
.sort_values(ascending=False))
78+
self.series_ts_rev = Series(np.random.randn(4),
79+
index=dates_rev)
80+
self.frame_ts_rev = DataFrame(np.random.randn(4, 4),
81+
index=dates_rev)
82+
self.panel_ts_rev = Panel(np.random.randn(4, 4, 4),
83+
items=dates_rev)
84+
85+
self.frame_empty = DataFrame({})
86+
self.series_empty = Series({})
87+
self.panel_empty = Panel({})
88+
89+
# form agglomerates
90+
for o in self._objs:
91+
92+
d = dict()
93+
for t in self._typs:
94+
d[t] = getattr(self, '%s_%s' % (o, t), None)
95+
96+
setattr(self, o, d)
97+
98+
def generate_indices(self, f, values=False):
99+
""" generate the indicies
100+
if values is True , use the axis values
101+
is False, use the range
102+
"""
103+
104+
axes = f.axes
105+
if values:
106+
axes = [lrange(len(a)) for a in axes]
107+
108+
return itertools.product(*axes)
109+
110+
def get_result(self, obj, method, key, axis):
111+
""" return the result for this obj with this key and this axis """
112+
113+
if isinstance(key, dict):
114+
key = key[axis]
115+
116+
# use an artifical conversion to map the key as integers to the labels
117+
# so ix can work for comparisions
118+
if method == 'indexer':
119+
method = 'ix'
120+
key = obj._get_axis(axis)[key]
121+
122+
# in case we actually want 0 index slicing
123+
try:
124+
with catch_warnings(record=True):
125+
xp = getattr(obj, method).__getitem__(_axify(obj, key, axis))
126+
except:
127+
xp = getattr(obj, method).__getitem__(key)
128+
129+
return xp
130+
131+
def get_value(self, f, i, values=False):
132+
""" return the value for the location i """
133+
134+
# check agains values
135+
if values:
136+
return f.values[i]
137+
138+
# this is equiv of f[col][row].....
139+
# v = f
140+
# for a in reversed(i):
141+
# v = v.__getitem__(a)
142+
# return v
143+
with catch_warnings(record=True):
144+
return f.ix[i]
145+
146+
def check_values(self, f, func, values=False):
147+
148+
if f is None:
149+
return
150+
axes = f.axes
151+
indicies = itertools.product(*axes)
152+
153+
for i in indicies:
154+
result = getattr(f, func)[i]
155+
156+
# check agains values
157+
if values:
158+
expected = f.values[i]
159+
else:
160+
expected = f
161+
for a in reversed(i):
162+
expected = expected.__getitem__(a)
163+
164+
tm.assert_almost_equal(result, expected)
165+
166+
def check_result(self, name, method1, key1, method2, key2, typs=None,
167+
objs=None, axes=None, fails=None):
168+
def _eq(t, o, a, obj, k1, k2):
169+
""" compare equal for these 2 keys """
170+
171+
if a is not None and a > obj.ndim - 1:
172+
return
173+
174+
def _print(result, error=None):
175+
if error is not None:
176+
error = str(error)
177+
v = ("%-16.16s [%-16.16s]: [typ->%-8.8s,obj->%-8.8s,"
178+
"key1->(%-4.4s),key2->(%-4.4s),axis->%s] %s" %
179+
(name, result, t, o, method1, method2, a, error or ''))
180+
if _verbose:
181+
pprint_thing(v)
182+
183+
try:
184+
rs = getattr(obj, method1).__getitem__(_axify(obj, k1, a))
185+
186+
try:
187+
xp = self.get_result(obj, method2, k2, a)
188+
except:
189+
result = 'no comp'
190+
_print(result)
191+
return
192+
193+
detail = None
194+
195+
try:
196+
if is_scalar(rs) and is_scalar(xp):
197+
self.assertEqual(rs, xp)
198+
elif xp.ndim == 1:
199+
tm.assert_series_equal(rs, xp)
200+
elif xp.ndim == 2:
201+
tm.assert_frame_equal(rs, xp)
202+
elif xp.ndim == 3:
203+
tm.assert_panel_equal(rs, xp)
204+
result = 'ok'
205+
except AssertionError as e:
206+
detail = str(e)
207+
result = 'fail'
208+
209+
# reverse the checks
210+
if fails is True:
211+
if result == 'fail':
212+
result = 'ok (fail)'
213+
214+
_print(result)
215+
if not result.startswith('ok'):
216+
raise AssertionError(detail)
217+
218+
except AssertionError:
219+
raise
220+
except Exception as detail:
221+
222+
# if we are in fails, the ok, otherwise raise it
223+
if fails is not None:
224+
if isinstance(detail, fails):
225+
result = 'ok (%s)' % type(detail).__name__
226+
_print(result)
227+
return
228+
229+
result = type(detail).__name__
230+
raise AssertionError(_print(result, error=detail))
231+
232+
if typs is None:
233+
typs = self._typs
234+
235+
if objs is None:
236+
objs = self._objs
237+
238+
if axes is not None:
239+
if not isinstance(axes, (tuple, list)):
240+
axes = [axes]
241+
else:
242+
axes = list(axes)
243+
else:
244+
axes = [0, 1, 2]
245+
246+
# check
247+
for o in objs:
248+
if o not in self._objs:
249+
continue
250+
251+
d = getattr(self, o)
252+
for a in axes:
253+
for t in typs:
254+
if t not in self._typs:
255+
continue
256+
257+
obj = d[t]
258+
if obj is not None:
259+
obj = obj.copy()
260+
261+
k2 = key2
262+
_eq(t, o, a, obj, key1, k2)

pandas/tests/indexing/test_chaining_and_caching.py

+75
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from warnings import catch_warnings
2+
13
import numpy as np
24
import pandas as pd
35
from pandas.core import common as com
@@ -357,3 +359,76 @@ def test_detect_chained_assignment_warnings(self):
357359
with tm.assert_produces_warning(
358360
expected_warning=com.SettingWithCopyWarning):
359361
df.loc[0]['A'] = 111
362+
363+
def test_chained_getitem_with_lists(self):
364+
365+
# GH6394
366+
# Regression in chained getitem indexing with embedded list-like from
367+
# 0.12
368+
def check(result, expected):
369+
tm.assert_numpy_array_equal(result, expected)
370+
tm.assertIsInstance(result, np.ndarray)
371+
372+
df = DataFrame({'A': 5 * [np.zeros(3)], 'B': 5 * [np.ones(3)]})
373+
expected = df['A'].iloc[2]
374+
result = df.loc[2, 'A']
375+
check(result, expected)
376+
result2 = df.iloc[2]['A']
377+
check(result2, expected)
378+
result3 = df['A'].loc[2]
379+
check(result3, expected)
380+
result4 = df['A'].iloc[2]
381+
check(result4, expected)
382+
383+
def test_cache_updating(self):
384+
# GH 4939, make sure to update the cache on setitem
385+
386+
df = tm.makeDataFrame()
387+
df['A'] # cache series
388+
with catch_warnings(record=True):
389+
df.ix["Hello Friend"] = df.ix[0]
390+
self.assertIn("Hello Friend", df['A'].index)
391+
self.assertIn("Hello Friend", df['B'].index)
392+
393+
with catch_warnings(record=True):
394+
panel = tm.makePanel()
395+
panel.ix[0] # get first item into cache
396+
panel.ix[:, :, 'A+1'] = panel.ix[:, :, 'A'] + 1
397+
self.assertIn("A+1", panel.ix[0].columns)
398+
self.assertIn("A+1", panel.ix[1].columns)
399+
400+
# 5216
401+
# make sure that we don't try to set a dead cache
402+
a = np.random.rand(10, 3)
403+
df = DataFrame(a, columns=['x', 'y', 'z'])
404+
tuples = [(i, j) for i in range(5) for j in range(2)]
405+
index = MultiIndex.from_tuples(tuples)
406+
df.index = index
407+
408+
# setting via chained assignment
409+
# but actually works, since everything is a view
410+
df.loc[0]['z'].iloc[0] = 1.
411+
result = df.loc[(0, 0), 'z']
412+
self.assertEqual(result, 1)
413+
414+
# correct setting
415+
df.loc[(0, 0), 'z'] = 2
416+
result = df.loc[(0, 0), 'z']
417+
self.assertEqual(result, 2)
418+
419+
# 10264
420+
df = DataFrame(np.zeros((5, 5), dtype='int64'), columns=[
421+
'a', 'b', 'c', 'd', 'e'], index=range(5))
422+
df['f'] = 0
423+
df.f.values[3] = 1
424+
425+
# TODO(wesm): unused?
426+
# y = df.iloc[np.arange(2, len(df))]
427+
428+
df.f.values[3] = 2
429+
expected = DataFrame(np.zeros((5, 6), dtype='int64'), columns=[
430+
'a', 'b', 'c', 'd', 'e', 'f'], index=range(5))
431+
expected.at[3, 'f'] = 2
432+
tm.assert_frame_equal(df, expected)
433+
expected = Series([0, 0, 0, 2, 0], name='f')
434+
tm.assert_series_equal(df.f, expected)

0 commit comments

Comments
 (0)