Skip to content

Commit 0a4c4b5

Browse files
committed
BUG: Fixes KeyError when indexes don't overlap.
GH10291
1 parent 98b9f86 commit 0a4c4b5

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v0.18.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,3 +954,4 @@ Bug Fixes
954954
- Bug in ``.skew`` and ``.kurt`` due to roundoff error for highly similar values (:issue:`11974`)
955955

956956
- Bug in ``buffer_rd_bytes`` src->buffer could be freed more than once if reading failed, causing a segfault (:issue:`12098`)
957+
- Bug in ``crosstab`` where arguments with non-overlapping indexes would return a KeyError (:issue:`10291`)

pandas/tools/pivot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',
153153
margins_name=margins_name)
154154

155155
# discard the top level
156-
if values_passed and not values_multi:
156+
if values_passed and not values_multi and not table.empty:
157157
table = table[values[0]]
158158

159159
if len(index) == 0 and len(columns) > 0:
@@ -396,6 +396,9 @@ def crosstab(index, columns, values=None, rownames=None, colnames=None,
396396
Any Series passed will have their name attributes used unless row or column
397397
names for the cross-tabulation are specified
398398
399+
In the event that there aren't overlapping indexes an empty DataFrame will
400+
be returned.
401+
399402
Examples
400403
--------
401404
>>> a

pandas/tools/tests/test_pivot.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,17 @@ def test_categorical_margins(self):
892892
table = data.pivot_table('x', 'y', 'z', margins=True)
893893
tm.assert_frame_equal(table, expected)
894894

895+
def test_crosstab_no_overlap(self):
896+
# GS 10291
897+
898+
s1 = pd.Series([1, 2, 3], index=[1, 2, 3])
899+
s2 = pd.Series([4, 5, 6], index=[4, 5, 6])
900+
901+
actual = crosstab(s1, s2)
902+
expected = pd.DataFrame()
903+
904+
tm.assert_frame_equal(actual, expected)
905+
895906
if __name__ == '__main__':
896907
import nose
897908
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

0 commit comments

Comments
 (0)