Skip to content

Commit d671492

Browse files
Chang Shewesm
Chang She
authored andcommitted
TST: additional test coverage for 0.8
1 parent 97cef6c commit d671492

File tree

8 files changed

+119
-48
lines changed

8 files changed

+119
-48
lines changed

pandas/core/common.py

+1-16
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,6 @@ def _unpickle_array(bytes):
140140
arr = read_array(BytesIO(bytes))
141141
return arr
142142

143-
def _take_1d_datetime(arr, indexer, out, fill_value=np.nan):
144-
view = arr.view(np.int64)
145-
outview = out.view(np.int64)
146-
_algos.take_1d_bool(view, indexer, outview, fill_value=fill_value)
147-
148-
def _take_2d_axis0_datetime(arr, indexer, out, fill_value=np.nan):
149-
view = arr.view(np.int64)
150-
outview = out.view(np.int64)
151-
_algos.take_1d_bool(view, indexer, outview, fill_value=fill_value)
152-
153-
def _take_2d_axis1_datetime(arr, indexer, out, fill_value=np.nan):
154-
view = arr.view(np.uint8)
155-
outview = out.view(np.uint8)
156-
_algos.take_1d_bool(view, indexer, outview, fill_value=fill_value)
157-
158143
def _view_wrapper(f, wrap_dtype, na_override=None):
159144
def wrapper(arr, indexer, out, fill_value=np.nan):
160145
if na_override is not None and np.isnan(fill_value):
@@ -212,7 +197,7 @@ def _get_take2d_function(dtype_str, axis=0):
212197
return _take2d_axis1_dict[dtype_str]
213198
elif axis == 'multi':
214199
return _take2d_multi_dict[dtype_str]
215-
else:
200+
else: # pragma: no cover
216201
raise ValueError('bad axis: %s' % axis)
217202

218203
def take_1d(arr, indexer, out=None, fill_value=np.nan):

pandas/core/index.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def wrapper(self, other):
2828
result = func(other)
2929
try:
3030
return result.view(np.ndarray)
31-
except:
31+
except: # pragma: no cover
3232
return result
3333
return wrapper
3434

@@ -525,7 +525,7 @@ def union(self, other):
525525
# contained in
526526
try:
527527
result = np.sort(self.values)
528-
except TypeError:
528+
except TypeError: # pragma: no cover
529529
result = self.values
530530

531531
# for subclasses

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2702,7 +2702,7 @@ def _resolve_offset(freq, kwds):
27022702
from pandas.core.datetools import getOffset
27032703

27042704
if 'timeRule' in kwds or 'offset' in kwds:
2705-
offset = kwds.get('offset')
2705+
offset = kwds.get('offset', None)
27062706
offset = kwds.get('timeRule', offset)
27072707
if isinstance(offset, basestring):
27082708
offset = datetools.getOffset(offset)

pandas/tests/test_frame.py

+14
Original file line numberDiff line numberDiff line change
@@ -5649,6 +5649,20 @@ def test_reindex_multi(self):
56495649

56505650
assert_frame_equal(result, expected)
56515651

5652+
df = DataFrame(np.random.randint(0, 10, (3, 3)))
5653+
5654+
result = df.reindex(range(3), range(3))
5655+
expected = df.reindex(range(3)).reindex(columns=range(3))
5656+
5657+
assert_frame_equal(result, expected)
5658+
5659+
df = df + 1j
5660+
5661+
result = df.reindex(range(4), range(4))
5662+
expected = df.reindex(range(4)).reindex(columns=range(4))
5663+
5664+
assert_frame_equal(result, expected)
5665+
56525666
def test_rename_objects(self):
56535667
renamed = self.mixed_frame.rename(columns=str.upper)
56545668
self.assert_('FOO' in renamed)

pandas/tests/test_index.py

+14
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ def test_union(self):
197197
union = first.union([])
198198
self.assert_(union is first)
199199

200+
union = Index([]).union(first)
201+
self.assert_(union is first)
202+
200203
# non-iterable input
201204
self.assertRaises(Exception, first.union, 0.5)
202205

@@ -395,6 +398,11 @@ def test_drop(self):
395398
expected = self.strIndex[1:]
396399
self.assert_(dropped.equals(expected))
397400

401+
ser = Index([1,2,3])
402+
dropped = ser.drop(1)
403+
expected = Index([1,3])
404+
self.assert_(dropped.equals(expected))
405+
398406
def test_tuple_union_bug(self):
399407
import pandas
400408
import numpy as np
@@ -1468,6 +1476,12 @@ def test_reindex_level(self):
14681476
exp_indexer2 = np.array([0, -1, 0, -1, 0, -1])
14691477
self.assert_(np.array_equal(indexer2, exp_indexer2))
14701478

1479+
self.assertRaises(ValueError, self.index.reindex,
1480+
self.index, method='pad', level='second')
1481+
1482+
self.assertRaises(ValueError, idx.reindex,
1483+
idx, method='bfill', level='first')
1484+
14711485
def test_has_duplicates(self):
14721486
self.assert_(not self.index.has_duplicates)
14731487
self.assert_(self.index.append(self.index).has_duplicates)

pandas/tests/test_series.py

+57-25
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,14 @@ def test_constructor_dict(self):
284284
expected = Series([1, 2, nan, 0], index=['b', 'c', 'd', 'a'])
285285
assert_series_equal(result, expected)
286286

287+
pidx = tm.makePeriodIndex(100)
288+
d = {pidx[0] : 0, pidx[1] : 1}
289+
result = Series(d, index=pidx)
290+
expected = Series(np.nan, pidx)
291+
expected.ix[0] = 0
292+
expected.ix[1] = 1
293+
assert_series_equal(result, expected)
294+
287295
def test_constructor_subclass_dict(self):
288296
data = tm.TestSubDict((x, 10.0 * x) for x in xrange(10))
289297
series = Series(data)
@@ -929,6 +937,12 @@ def test_timeseries_repr_object_dtype(self):
929937
ts = Series(np.random.randn(len(index)), index)
930938
repr(ts)
931939

940+
ts = tm.makeTimeSeries(1000)
941+
self.assert_(repr(ts).splitlines()[-1].startswith('Freq:'))
942+
943+
ts2 = ts.ix[np.random.randint(0, len(ts)-1, 400)]
944+
repr(ts).splitlines()[-1]
945+
932946
def test_iter(self):
933947
for i, val in enumerate(self.series):
934948
self.assertEqual(val, self.series[i])
@@ -1202,46 +1216,48 @@ def test_all_any(self):
12021216
self.assert_(bool_series.any())
12031217

12041218
def test_operators(self):
1205-
series = self.ts
1206-
other = self.ts[::2]
12071219

1208-
def _check_op(other, op, pos_only=False):
1220+
def _check_op(series, other, op, pos_only=False):
12091221
left = np.abs(series) if pos_only else series
12101222
right = np.abs(other) if pos_only else other
12111223

12121224
cython_or_numpy = op(left, right)
12131225
python = left.combine(right, op)
12141226
tm.assert_almost_equal(cython_or_numpy, python)
12151227

1216-
def check(other):
1228+
def check(series, other):
12171229
simple_ops = ['add', 'sub', 'mul', 'truediv', 'floordiv']
12181230

12191231
for opname in simple_ops:
1220-
_check_op(other, getattr(operator, opname))
1221-
_check_op(other, operator.pow, pos_only=True)
1222-
1223-
_check_op(other, lambda x, y: operator.add(y, x))
1224-
_check_op(other, lambda x, y: operator.sub(y, x))
1225-
_check_op(other, lambda x, y: operator.truediv(y, x))
1226-
_check_op(other, lambda x, y: operator.floordiv(y, x))
1227-
_check_op(other, lambda x, y: operator.mul(y, x))
1228-
_check_op(other, lambda x, y: operator.pow(y, x),
1232+
_check_op(series, other, getattr(operator, opname))
1233+
1234+
_check_op(series, other, operator.pow, pos_only=True)
1235+
1236+
_check_op(series, other, lambda x, y: operator.add(y, x))
1237+
_check_op(series, other, lambda x, y: operator.sub(y, x))
1238+
_check_op(series, other, lambda x, y: operator.truediv(y, x))
1239+
_check_op(series, other, lambda x, y: operator.floordiv(y, x))
1240+
_check_op(series, other, lambda x, y: operator.mul(y, x))
1241+
_check_op(series, other, lambda x, y: operator.pow(y, x),
12291242
pos_only=True)
12301243

1231-
check(self.ts * 2)
1232-
check(self.ts * 0)
1233-
check(self.ts[::2])
1234-
check(5)
1244+
check(self.ts, self.ts * 2)
1245+
check(self.ts, self.ts * 0)
1246+
check(self.ts, self.ts[::2])
1247+
check(self.ts, 5)
12351248

1236-
def check_comparators(other):
1237-
_check_op(other, operator.gt)
1238-
_check_op(other, operator.ge)
1239-
_check_op(other, operator.eq)
1240-
_check_op(other, operator.lt)
1241-
_check_op(other, operator.le)
1249+
def check_comparators(series, other):
1250+
_check_op(series, other, operator.gt)
1251+
_check_op(series, other, operator.ge)
1252+
_check_op(series, other, operator.eq)
1253+
_check_op(series, other, operator.lt)
1254+
_check_op(series, other, operator.le)
1255+
1256+
check_comparators(self.ts, 5)
1257+
check_comparators(self.ts, self.ts + 1)
1258+
bool_ser = self.ts > 0
1259+
check_comparators(bool_ser, bool_ser[::2])
12421260

1243-
check_comparators(5)
1244-
check_comparators(self.ts + 1)
12451261

12461262
def test_operators_empty_int_corner(self):
12471263
s1 = Series([], [], dtype=np.int32)
@@ -1941,6 +1957,13 @@ def test_shift(self):
19411957

19421958
self.assertRaises(ValueError, ps.shift, freq='D')
19431959

1960+
#legacy support
1961+
shifted4 = ps.shift(1, timeRule='B')
1962+
assert_series_equal(shifted2, shifted4)
1963+
1964+
shifted5 = ps.shift(1, offset=datetools.bday)
1965+
assert_series_equal(shifted5, shifted4)
1966+
19441967
def test_tshift(self):
19451968
# PeriodIndex
19461969
ps = tm.makePeriodSeries()
@@ -2050,6 +2073,11 @@ def test_asof(self):
20502073
lb = ts.index[14]
20512074
ub = ts.index[30]
20522075

2076+
result = ts.asof(list(dates))
2077+
self.assert_(notnull(result).all())
2078+
lb = ts.index[14]
2079+
ub = ts.index[30]
2080+
20532081
mask = (result.index >= lb) & (result.index < ub)
20542082
rs = result[mask]
20552083
self.assert_((rs == ts[lb]).all())
@@ -2615,6 +2643,10 @@ def test_replace(self):
26152643
ser = Series(self.ts.index)
26162644
assert_series_equal(ser.replace(np.nan, 0), ser.fillna(0))
26172645

2646+
# malformed
2647+
self.assertRaises(ValueError, ser.replace, [1,2,3], [np.nan, 0])
2648+
self.assertRaises(ValueError, ser.replace, xrange(1,3), [np.nan, 0])
2649+
26182650
def test_asfreq(self):
26192651
ts = Series([0., 1., 2.], index=[datetime(2009, 10, 30),
26202652
datetime(2009, 11, 30),

pandas/tseries/tests/test_timeseries.py

+26
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,25 @@ def test_fillna_nat(self):
381381
assert_frame_equal(filled, expected)
382382
assert_frame_equal(filled2, expected)
383383

384+
385+
series = Series([NaT, 0, 1, 2], dtype='M8[us]')
386+
387+
filled = series.fillna(method='bfill')
388+
filled2 = series.fillna(value=series[1])
389+
390+
expected = series.copy()
391+
expected[0] = expected[1]
392+
393+
assert_series_equal(filled, expected)
394+
assert_series_equal(filled2, expected)
395+
396+
df = DataFrame({'A': series})
397+
filled = df.fillna(method='bfill')
398+
filled2 = df.fillna(value=series[1])
399+
expected = DataFrame({'bfill': expected})
400+
assert_frame_equal(filled, expected)
401+
assert_frame_equal(filled2, expected)
402+
384403
def test_string_na_nat_conversion(self):
385404
# GH #999, #858
386405

@@ -469,6 +488,11 @@ def test_index_to_datetime(self):
469488
expected = DatetimeIndex(datetools.to_datetime(idx.values))
470489
self.assert_(result.equals(expected))
471490

491+
idx = Index([datetime.today()], dtype=object)
492+
result = idx.to_datetime()
493+
expected = DatetimeIndex([datetime.today()])
494+
self.assert_(result.equals(expected))
495+
472496
def test_range_misspecified(self):
473497
# GH #1095
474498

@@ -584,6 +608,8 @@ def test_repeat(self):
584608
def test_at_time(self):
585609
rng = date_range('1/1/2000', '1/5/2000', freq='5min')
586610
ts = Series(np.random.randn(len(rng)), index=rng)
611+
self.assert_(ts.at_time(rng[0]), ts.ix[0])
612+
587613
df = DataFrame(np.random.randn(len(rng), 3), index=rng)
588614

589615
result = ts[time(9, 30)]

pandas/util/testing.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,11 @@ def makePeriodIndex(k):
237237
dr = PeriodIndex(start=dt, periods=k, freq='B')
238238
return dr
239239

240-
def makeTimeSeries():
241-
return Series(randn(N), index=makeDateIndex(N))
240+
def makeTimeSeries(nper=N):
241+
return Series(randn(nper), index=makeDateIndex(nper))
242242

243-
def makePeriodSeries():
244-
return Series(randn(N), index=makePeriodIndex(N))
243+
def makePeriodSeries(nper=N):
244+
return Series(randn(nper), index=makePeriodIndex(nper))
245245

246246
def getTimeSeriesData():
247247
return dict((c, makeTimeSeries()) for c in getCols(K))

0 commit comments

Comments
 (0)