Skip to content

Commit ddaeb6c

Browse files
committed
got test_frame coverage to 100%
1 parent ea37dcc commit ddaeb6c

File tree

5 files changed

+40
-38
lines changed

5 files changed

+40
-38
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pandas 0.7.0
122122
- Fix outer join between two DateRanges with different offsets that returned
123123
an invalid DateRange
124124
- Cleanup DataFrame.from_records failure where index argument is an integer
125+
- Fix Data.from_records failure when passed a dictionary
125126

126127
Thanks
127128
------

pandas/core/frame.py

+3-23
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,9 @@ def from_records(cls, data, index=None, exclude=None, columns=None,
642642
result_index = MultiIndex.from_arrays(arrays)
643643
except Exception:
644644
result_index = index
645+
elif isinstance(data, dict) and len(data) > 0:
646+
# utilize first element of sdict to get length
647+
result_index = np.arange(len(data.values()[0]))
645648
else:
646649
result_index = np.arange(len(data))
647650

@@ -2681,13 +2684,6 @@ def _apply_broadcast(self, func, axis):
26812684

26822685
return result
26832686

2684-
def _apply_level(self, f, level=0, broadcast=False):
2685-
grouped = self.groupby(level=level)
2686-
if broadcast:
2687-
return grouped.transform(f)
2688-
else:
2689-
return grouped.agg(f)
2690-
26912687
def applymap(self, func):
26922688
"""
26932689
Apply a function to a DataFrame that is intended to operate
@@ -3033,10 +3029,6 @@ def count(self, axis=0, level=None, numeric_only=False):
30333029
else:
30343030
result = DataFrame.apply(frame, Series.count, axis=axis)
30353031

3036-
# what happens with empty DataFrame
3037-
if isinstance(result, DataFrame):
3038-
result = Series({})
3039-
30403032
return result
30413033

30423034
def _count_level(self, level, axis=0, numeric_only=False):
@@ -3823,18 +3815,6 @@ def _homogenize(data, index, columns, dtype=None):
38233815

38243816
return homogenized
38253817

3826-
def _get_join_data(left_index, right_index, how, level):
3827-
# defaults
3828-
join_index = left_index
3829-
lidx, ridx = None, None
3830-
3831-
if not left_index.equals(right_index):
3832-
join_index, lidx, ridx = \
3833-
left_index.join(right_index, how=how, level=level,
3834-
return_indexers=True)
3835-
3836-
return join_index, lidx, ridx
3837-
38383818
def _put_str(s, space):
38393819
return ('%s' % s)[:space].ljust(space)
38403820

pandas/tests/test_frame.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -1453,19 +1453,23 @@ def test_from_records_to_records(self):
14531453
self.assertEqual(len(records.dtype.names), 2)
14541454
self.assert_('index' not in records.dtype.names)
14551455

1456-
def test_from_records_tuples(self):
1456+
def test_from_records_sequencelike(self):
14571457
df = DataFrame({'A' : np.random.randn(6),
14581458
'B' : np.arange(6),
14591459
'C' : ['foo'] * 6,
14601460
'D' : np.array([True, False] * 3, dtype=bool)})
14611461

14621462
tuples = [tuple(x) for x in df.values]
14631463
lists = [list(x) for x in tuples]
1464+
asdict = dict((x,y) for x, y in df.iteritems())
14641465

14651466
result = DataFrame.from_records(tuples, columns=df.columns)
14661467
result2 = DataFrame.from_records(lists, columns=df.columns)
1468+
result3 = DataFrame.from_records(asdict, columns=df.columns)
1469+
14671470
assert_frame_equal(result, df)
14681471
assert_frame_equal(result2, df)
1472+
assert_frame_equal(result3, df)
14691473

14701474
result = DataFrame.from_records(tuples)
14711475
self.assert_(np.array_equal(result.columns, range(4)))
@@ -3474,6 +3478,11 @@ def test_count(self):
34743478
expected = Series(0, index=df.columns)
34753479
assert_series_equal(result, expected)
34763480

3481+
df = DataFrame()
3482+
result = df.count()
3483+
expected = Series(0, index=[])
3484+
assert_series_equal(result, expected)
3485+
34773486
def test_sum(self):
34783487
self._check_stat_op('sum', np.sum, has_numeric_only=True)
34793488

@@ -3663,6 +3672,10 @@ def test_quantile(self):
36633672
q = self.intframe.quantile(0.1)
36643673
self.assertEqual(q['A'], scoreatpercentile(self.intframe['A'], 10))
36653674

3675+
# test degenerate case
3676+
q = DataFrame({'x':[],'y':[]}).quantile(0.1, axis=0)
3677+
assert(np.isnan(q['x']) and np.isnan(q['y']))
3678+
36663679
def test_cumsum(self):
36673680
self.tsframe.ix[5:10, 0] = nan
36683681
self.tsframe.ix[10:15, 1] = nan
@@ -4095,11 +4108,11 @@ def test_idxmin(self):
40954108
for skipna in [True, False]:
40964109
for axis in [0, 1]:
40974110
for df in [frame, self.intframe]:
4098-
result = df.idxmax(axis=axis, skipna=skipna)
4099-
expected = df.apply(Series.idxmax, axis=axis, skipna=skipna)
4111+
result = df.idxmin(axis=axis, skipna=skipna)
4112+
expected = df.apply(Series.idxmin, axis=axis, skipna=skipna)
41004113
assert_series_equal(result, expected)
41014114

4102-
self.assertRaises(Exception, frame.idxmax, axis=2)
4115+
self.assertRaises(Exception, frame.idxmin, axis=2)
41034116

41044117
def test_idxmax(self):
41054118
frame = self.frame

pandas/tests/test_graphics.py

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ def test_plot(self):
6767
_check_plot_works(df.plot, subplots=True)
6868
_check_plot_works(df.plot, subplots=True, use_index=False)
6969

70+
df = DataFrame({'x':[1,2], 'y':[3,4]})
71+
self._check_plot_fails(df.plot, kind='line', blarg=True)
72+
7073
@slow
7174
def test_plot_bar(self):
7275
df = DataFrame(np.random.randn(6, 4),
@@ -107,6 +110,8 @@ def test_plot_int_columns(self):
107110
df = DataFrame(np.random.randn(100, 4)).cumsum()
108111
_check_plot_works(df.plot, legend=True)
109112

113+
def _check_plot_fails(self, f, *args, **kwargs):
114+
self.assertRaises(Exception, f, *args, **kwargs)
110115

111116
PNG_PATH = 'tmp.png'
112117

pandas/tests/test_panel.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,20 @@ def test_rename(self):
960960
renamed_nocopy['foo'] = 3.
961961
self.assert_((self.panel['ItemA'].values == 3).all())
962962

963+
def test_group_agg(self):
964+
values = np.ones((10, 2)) * np.arange(10).reshape((10, 1))
965+
bounds = np.arange(5) * 2
966+
f = lambda x: x.mean(axis=0)
967+
968+
agged = group_agg(values, bounds, f)
969+
970+
assert(agged[1][0] == 2.5)
971+
assert(agged[2][0] == 4.5)
972+
973+
# test a function that doesn't aggregate
974+
f2 = lambda x: np.zeros((2,2))
975+
self.assertRaises(Exception, group_agg, values, bounds, f2)
976+
963977
class TestLongPanel(unittest.TestCase):
964978
"""
965979
LongPanel no longer exists, but...
@@ -1172,17 +1186,6 @@ def test_pivot(self):
11721186
# corner case, empty
11731187
df = pivot(np.array([]), np.array([]), np.array([]))
11741188

1175-
1176-
def test_group_agg():
1177-
values = np.ones((10, 2)) * np.arange(10).reshape((10, 1))
1178-
bounds = np.arange(5) * 2
1179-
f = lambda x: x.mean(axis=0)
1180-
1181-
agged = group_agg(values, bounds, f)
1182-
1183-
assert(agged[1][0] == 2.5)
1184-
assert(agged[2][0] == 4.5)
1185-
11861189
def test_monotonic():
11871190
pos = np.array([1, 2, 3, 5])
11881191

0 commit comments

Comments
 (0)