in kats/consts.py [0:0]
def _perform_op(self, other: object, op_type: "OperationsEnum") -> TimeSeriesData:
# Extract DataFrames with same time column name for joining
self_df = self.to_dataframe(standard_time_col_name=True)
other_df = self._get_binary_op_other_arg(other).to_dataframe(
standard_time_col_name=True
)
# Join DataFrames on time column
combo_df = pd.merge(
self_df,
other_df,
on=DEFAULT_TIME_NAME,
how="outer",
suffixes=(PREFIX_OP_1, PREFIX_OP_2),
)
# Map the final column name to the sub column names
col_map = {}
for col_name in list(combo_df.columns):
if PREFIX_OP_1 in col_name:
prefix = col_name.split(PREFIX_OP_1)[0]
col_map[prefix] = col_map.get(prefix, []) + [col_name]
elif PREFIX_OP_2 in col_name:
prefix = col_name.split(PREFIX_OP_2)[0]
col_map[prefix] = col_map.get(prefix, []) + [col_name]
for col_name in list(col_map.keys()):
# Perform operation on two columns and merge back to one column
col_1, col_2 = col_map[col_name]
if op_type == OperationsEnum.ADD:
combo_df[col_name] = combo_df[col_1] + combo_df[col_2]
elif op_type == OperationsEnum.SUB:
combo_df[col_name] = combo_df[col_1] - combo_df[col_2]
elif op_type == OperationsEnum.DIV:
combo_df[col_name] = combo_df[col_1] / combo_df[col_2]
elif op_type == OperationsEnum.MUL:
combo_df[col_name] = combo_df[col_1] * combo_df[col_2]
else:
raise ValueError("Unsupported Operations Type")
combo_df.drop([col_1, col_2], axis=1, inplace=True)
# Set columns only present in one of the objects to None
final_col_list = set([DEFAULT_TIME_NAME] + list(col_map.keys()))
for col_name in list(combo_df.columns):
if col_name not in final_col_list:
combo_df[col_name] = np.nan
# Change time col name back if needed
if self.time_col_name != DEFAULT_TIME_NAME:
combo_df[self.time_col_name] = combo_df[DEFAULT_TIME_NAME]
combo_df.drop(DEFAULT_TIME_NAME, axis=1, inplace=True)
return TimeSeriesData(df=combo_df, time_col_name=self.time_col_name)