Fill in zeroes when joining or unstacking.

If there are no calls or smses for a particular day, there is no corresponding row in the features dataframe. When joining these, however, NaNs were introduced. Since a value of 0 is meaningful for all of these features, replace NaNs with 0's.
communication
junos 2021-08-21 17:31:15 +02:00
parent 9a319ac6e5
commit 00015a3b8d
1 changed files with 31 additions and 22 deletions

View File

@ -164,7 +164,7 @@ def count_comms(comm_df: pd.DataFrame, group_by=None) -> pd.DataFrame:
data_type = "calls"
comm_counts = (
comm_df.value_counts(subset=group_by + ["participant_id", "call_type"])
.unstack()
.unstack(level="call_type", fill_value=0)
.rename(columns=call_types)
.add_prefix("no_")
)
@ -179,7 +179,7 @@ def count_comms(comm_df: pd.DataFrame, group_by=None) -> pd.DataFrame:
comm_duration_total = (
comm_df.groupby(group_by + ["participant_id", "call_type"])
.sum()["call_duration"]
.unstack()
.unstack(level="call_type", fill_value=0)
.rename(columns=call_types)
.add_prefix("duration_total_")
)
@ -187,7 +187,7 @@ def count_comms(comm_df: pd.DataFrame, group_by=None) -> pd.DataFrame:
comm_duration_max = (
comm_df.groupby(group_by + ["participant_id", "call_type"])
.max()["call_duration"]
.unstack()
.unstack(level="call_type", fill_value=0)
.rename(columns=call_types)
.add_prefix("duration_max_")
)
@ -206,7 +206,7 @@ def count_comms(comm_df: pd.DataFrame, group_by=None) -> pd.DataFrame:
data_type = "sms"
comm_counts = (
comm_df.value_counts(subset=group_by + ["participant_id", "message_type"])
.unstack()
.unstack(level="message_type", fill_value=0)
.rename(columns=sms_types)
.add_prefix("no_")
)
@ -309,9 +309,16 @@ def calls_sms_features(
group_by = []
count_calls = count_comms(df_calls, group_by)
count_sms = count_comms(df_sms, group_by)
count_joined = count_calls.merge(
count_sms, how="outer", left_index=True, right_index=True, validate="one_to_one"
).assign(
count_joined = (
count_calls.merge(
count_sms,
how="outer",
left_index=True,
right_index=True,
validate="one_to_one",
)
.fillna(0, downcast="infer")
.assign(
proportion_calls_all=(
lambda x: x.no_calls_all / (x.no_calls_all + x.no_sms_all)
),
@ -325,8 +332,10 @@ def calls_sms_features(
lambda x: x.no_outgoing / (x.no_outgoing + x.no_sent)
),
proportion_calls_contacts=(
lambda x: x.no_contacts_calls / (x.no_contacts_calls + x.no_contacts_sms)
lambda x: x.no_contacts_calls
/ (x.no_contacts_calls + x.no_contacts_sms)
)
# Calculate new features and create additional columns
)
)
return count_joined