Skip to content

Commit 744d27e

Browse files
gliptakjreback
authored andcommitted
DOC/TST: Update truncate() documentation and testcases
TST: assert_panel_equal now can use by_blocks for much faster comparisons closes #13024 closes #11382
1 parent 3681909 commit 744d27e

File tree

5 files changed

+67
-16
lines changed

5 files changed

+67
-16
lines changed

pandas/core/generic.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4633,14 +4633,15 @@ def tshift(self, periods=1, freq=None, axis=0):
46334633

46344634
def truncate(self, before=None, after=None, axis=None, copy=True):
46354635
"""Truncates a sorted NDFrame before and/or after some particular
4636-
dates.
4636+
index value. If the axis contains only datetime values, before/after
4637+
parameters are converted to datetime values.
46374638
46384639
Parameters
46394640
----------
46404641
before : date
4641-
Truncate before date
4642+
Truncate before index value
46424643
after : date
4643-
Truncate after date
4644+
Truncate after index value
46444645
axis : the truncation axis, defaults to the stat axis
46454646
copy : boolean, default is True,
46464647
return a copy of the truncated section

pandas/tests/test_generic.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,23 @@ def test_clip(self):
595595
lower=lower, upper=upper,
596596
axis=bad_axis)
597597

598+
def test_truncate_out_of_bounds(self):
599+
# GH11382
600+
601+
# small
602+
shape = [int(2e3)] + ([1] * (self._ndim - 1))
603+
small = self._construct(shape, dtype='int8')
604+
self._compare(small.truncate(), small)
605+
self._compare(small.truncate(before=0, after=3e3), small)
606+
self._compare(small.truncate(before=-1, after=2e3), small)
607+
608+
# big
609+
shape = [int(2e6)] + ([1] * (self._ndim - 1))
610+
big = self._construct(shape, dtype='int8')
611+
self._compare(big.truncate(), big)
612+
self._compare(big.truncate(before=0, after=3e6), big)
613+
self._compare(big.truncate(before=-1, after=2e6), big)
614+
598615
def test_numpy_clip(self):
599616
lower = 1
600617
upper = 3
@@ -1412,7 +1429,7 @@ def test_to_xarray(self):
14121429

14131430
class TestPanel(tm.TestCase, Generic):
14141431
_typ = Panel
1415-
_comparator = lambda self, x, y: assert_panel_equal(x, y)
1432+
_comparator = lambda self, x, y: assert_panel_equal(x, y, by_blocks=True)
14161433

14171434
def test_to_xarray(self):
14181435

@@ -1434,7 +1451,7 @@ def test_to_xarray(self):
14341451

14351452
class TestPanel4D(tm.TestCase, Generic):
14361453
_typ = Panel4D
1437-
_comparator = lambda self, x, y: assert_panel4d_equal(x, y)
1454+
_comparator = lambda self, x, y: assert_panel4d_equal(x, y, by_blocks=True)
14381455

14391456
def test_sample(self):
14401457
raise nose.SkipTest("sample on Panel4D")

pandas/tests/test_panel4d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def test_comparisons(self):
426426
p1 = tm.makePanel4D()
427427
p2 = tm.makePanel4D()
428428

429-
tp = p1.reindex(labels=p1.labels + ['foo'])
429+
tp = p1.reindex(labels=p1.labels.tolist() + ['foo'])
430430
p = p1[p1.labels[0]]
431431

432432
def test_comp(func):

pandas/tseries/tests/test_plotting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ def test_secondary_y_mixed_freq_ts_xlim(self):
11851185

11861186
ax = ts.plot()
11871187
left_before, right_before = ax.get_xlim()
1188-
ts.resample('D').plot(secondary_y=True, ax=ax)
1188+
ts.resample('D').mean().plot(secondary_y=True, ax=ax)
11891189
left_after, right_after = ax.get_xlim()
11901190

11911191
# a downsample should not have changed either limit

pandas/util/testing.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,26 +1167,59 @@ def assert_frame_equal(left, right, check_dtype=True,
11671167

11681168

11691169
def assert_panelnd_equal(left, right,
1170+
check_dtype=True,
11701171
check_panel_type=False,
11711172
check_less_precise=False,
11721173
assert_func=assert_frame_equal,
1173-
check_names=False):
1174+
check_names=False,
1175+
by_blocks=False):
1176+
"""Check that left and right Panels are equal.
1177+
1178+
Parameters
1179+
----------
1180+
left : Panel (or nd)
1181+
right : Panel (or nd)
1182+
check_dtype : bool, default True
1183+
Whether to check the Panel dtype is identical.
1184+
check_panel_type : bool, default False
1185+
Whether to check the Panel class is identical.
1186+
check_less_precise : bool, default False
1187+
Specify comparison precision. Only used when check_exact is False.
1188+
5 digits (False) or 3 digits (True) after decimal points are compared.
1189+
assert_func : function for comparing data
1190+
check_names : bool, default True
1191+
Whether to check the Index names attribute.
1192+
by_blocks : bool, default False
1193+
Specify how to compare internal data. If False, compare by columns.
1194+
If True, compare by blocks.
1195+
"""
1196+
11741197
if check_panel_type:
11751198
assertIsInstance(left, type(right))
11761199

1177-
for axis in ['items', 'major_axis', 'minor_axis']:
1200+
for axis in left._AXIS_ORDERS:
11781201
left_ind = getattr(left, axis)
11791202
right_ind = getattr(right, axis)
11801203
assert_index_equal(left_ind, right_ind, check_names=check_names)
11811204

1182-
for i, item in enumerate(left._get_axis(0)):
1183-
assert item in right, "non-matching item (right) '%s'" % item
1184-
litem = left.iloc[i]
1185-
ritem = right.iloc[i]
1186-
assert_func(litem, ritem, check_less_precise=check_less_precise)
1205+
if by_blocks:
1206+
rblocks = right.blocks
1207+
lblocks = left.blocks
1208+
for dtype in list(set(list(lblocks.keys()) + list(rblocks.keys()))):
1209+
assert dtype in lblocks
1210+
assert dtype in rblocks
1211+
array_equivalent(lblocks[dtype].values, rblocks[dtype].values)
1212+
else:
1213+
1214+
# can potentially be slow
1215+
for i, item in enumerate(left._get_axis(0)):
1216+
assert item in right, "non-matching item (right) '%s'" % item
1217+
litem = left.iloc[i]
1218+
ritem = right.iloc[i]
1219+
assert_func(litem, ritem, check_less_precise=check_less_precise)
11871220

1188-
for i, item in enumerate(right._get_axis(0)):
1189-
assert item in left, "non-matching item (left) '%s'" % item
1221+
for i, item in enumerate(right._get_axis(0)):
1222+
assert item in left, "non-matching item (left) '%s'" % item
11901223

11911224
# TODO: strangely check_names fails in py3 ?
11921225
_panel_frame_equal = partial(assert_frame_equal, check_names=False)

0 commit comments

Comments
 (0)