Skip to content

Commit

Permalink
class method for mct creation from dataframe
Browse files Browse the repository at this point in the history
  • Loading branch information
mxndrwgrdnr committed Apr 13, 2020
1 parent dbf56aa commit c625072
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions choicemodels/tools/mergedchoicetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,22 +186,34 @@ def __init__(self, observations, alternatives, chosen_alternatives=None,
@classmethod
def from_df(cls, df):
"""
Create an object instance from a dataframe
Create an object instance from a dataframe.
The MergedChoiceTable class requires two dataframes to initialize
representing observations and alternatives for the choice scenario,
so this classmethod simply passes in two empty dataframes. Similarly,
it also passes a dummy for the `chosen_alternatives` arg in order
to trigger the creation of the `MergedChoiceTable.choice_col` property.
Parameters
----------
df : a Pandas DataFrame object with 1) a MultiIndex in which the
first level corresponds to the index of the observations and the
second to the index of the alternatives; and 2) a binary column
named 'chosen' that indicated whether the corresponding
named 'chosen' that indicated whether the corresponding
alternative was chosen in the observation data.
choice_col : Name of the column containing a binary representation
of whether each alternative was chosen in the given choice scenario.
Returns
-------
MergedChoiceTable
"""
obj = cls(pd.DataFrame(), pd.DataFrame())
obj = cls(
observations=pd.DataFrame(),
alternatives=pd.DataFrame(),
chosen_alternatives=-999)
obj._merged_table = df

return obj
Expand Down

0 comments on commit c625072

Please sign in to comment.