Skip to content

Commit dcc7cca

Browse files
tshauckjreback
authored andcommitted
BUG: Fixes KeyError when indexes don't overlap.
Closes #10291 Author: Trent Hauck <[email protected]> Closes #12287 from tshauck/fix-non-overlapping-crosstab and squashes the following commits: 0a4c4b5 [Trent Hauck] BUG: Fixes KeyError when indexes don't overlap.
1 parent d838db7 commit dcc7cca

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
@@ -965,3 +965,4 @@ Bug Fixes
965965
- Bug in ``.skew`` and ``.kurt`` due to roundoff error for highly similar values (:issue:`11974`)
966966

967967
- Bug in ``buffer_rd_bytes`` src->buffer could be freed more than once if reading failed, causing a segfault (:issue:`12098`)
968+
- 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)