Add util function to compute difference between two columns
This commit is contained in:
parent
498b9f4d15
commit
94941538d8
2 changed files with 62 additions and 0 deletions
|
@ -70,6 +70,45 @@ class TestDataModelUtil(unittest.TestCase):
|
||||||
)
|
)
|
||||||
assert_frame_equal(result_df, expected_df, check_dtype=False)
|
assert_frame_equal(result_df, expected_df, check_dtype=False)
|
||||||
|
|
||||||
|
def test_compute_column_difference(self) -> None:
|
||||||
|
input_df = DataFrame(
|
||||||
|
data=[
|
||||||
|
{
|
||||||
|
'a': 10,
|
||||||
|
'b': 13,
|
||||||
|
'c': 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'a': 1,
|
||||||
|
'b': 3,
|
||||||
|
'c': 69,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
expected_df = DataFrame(
|
||||||
|
data=[
|
||||||
|
{
|
||||||
|
'a': 10,
|
||||||
|
'b': 13,
|
||||||
|
'c': 1,
|
||||||
|
'diff': 3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'a': 1,
|
||||||
|
'b': 3,
|
||||||
|
'c': 69,
|
||||||
|
'diff': 2,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
DataModelUtil.compute_column_difference(
|
||||||
|
input_df,
|
||||||
|
'b',
|
||||||
|
'a',
|
||||||
|
'diff',
|
||||||
|
)
|
||||||
|
assert_frame_equal(input_df, expected_df)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -79,6 +79,23 @@ class DataModelUtil():
|
||||||
)
|
)
|
||||||
return df
|
return df
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def compute_column_difference(
|
||||||
|
df: DataFrame,
|
||||||
|
left_column: str,
|
||||||
|
right_column: str,
|
||||||
|
diff_column: str,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
Create new column with difference between two columns.
|
||||||
|
|
||||||
|
:param df: the dataframe (inplace)
|
||||||
|
:param left_column: the name of the left column
|
||||||
|
:param right_column: the name of the right column
|
||||||
|
:param diff_column: the name of the new column with differences
|
||||||
|
"""
|
||||||
|
df[diff_column] = df.apply(lambda row: row[left_column] - row[right_column], axis=1)
|
||||||
|
|
||||||
|
|
||||||
class ProfileDataModelUtil(DataModelUtil):
|
class ProfileDataModelUtil(DataModelUtil):
|
||||||
"""Profiling data model utility class."""
|
"""Profiling data model utility class."""
|
||||||
|
@ -127,6 +144,12 @@ class ProfileDataModelUtil(DataModelUtil):
|
||||||
(tid_df['depth'] == depth) &
|
(tid_df['depth'] == depth) &
|
||||||
(tid_df['function_name'] == name)
|
(tid_df['function_name'] == name)
|
||||||
][['start_timestamp', 'duration', 'actual_duration']]
|
][['start_timestamp', 'duration', 'actual_duration']]
|
||||||
|
self.compute_column_difference(
|
||||||
|
data,
|
||||||
|
'duration',
|
||||||
|
'actual_duration',
|
||||||
|
'duration_difference',
|
||||||
|
)
|
||||||
functions_data.append({
|
functions_data.append({
|
||||||
'depth': depth,
|
'depth': depth,
|
||||||
'function_name': name,
|
'function_name': name,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue