Skip to content

Commit 0d77bcf

Browse files
committed
Make DesignInfo.subset work correctly with name-only DesignInfo objects.
1 parent b34c54d commit 0d77bcf

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

patsy/design_info.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,9 @@ def subset(self, which_terms):
566566
if self.term_codings is None:
567567
# This is a minimal DesignInfo
568568
# If the name is unknown we just let the KeyError escape
569-
new_names = [self.column_names[self.term_name_slices[t]]
570-
for t in which_terms]
569+
new_names = []
570+
for t in which_terms:
571+
new_names += self.column_names[self.term_name_slices[t]]
571572
return DesignInfo(new_names)
572573
else:
573574
term_name_to_term = {}

patsy/test_build.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from patsy.build import *
2020
from patsy.categorical import C
2121
from patsy.user_util import balanced, LookupFactor
22-
from patsy.design_info import DesignMatrix
22+
from patsy.design_info import DesignMatrix, DesignInfo
2323

2424
if have_pandas:
2525
import pandas
@@ -667,7 +667,7 @@ def test_contrast():
667667
[7, 12],
668668
[2, 13]])
669669

670-
def test_DesignMatrixBuilder_subset():
670+
def test_DesignInfo_subset():
671671
# For each combination of:
672672
# formula, term names, term objects, mixed term name and term objects
673673
# check that results match subset of full build
@@ -730,3 +730,9 @@ def t(which_terms, variables, columns):
730730
assert_raises(KeyError, all_builder.subset, ["asdf"])
731731
assert_raises(KeyError,
732732
all_builder.subset, [Term(["asdf"])])
733+
734+
# Also check for a minimal DesignInfo (column names only)
735+
min_di = DesignInfo(["a", "b", "c"])
736+
min_di_subset = min_di.subset(["c", "a"])
737+
assert min_di_subset.column_names == ["c", "a"]
738+
assert min_di_subset.terms is None

0 commit comments

Comments
 (0)