From 09905eed4dd11e957bf7f9365e749cf1ccdd025f Mon Sep 17 00:00:00 2001 From: frankknoll Date: Fri, 2 Jun 2023 01:06:22 +0200 Subject: [PATCH] adding CountryCountsByBatchcodeTablesMerger --- src/CountryCountsByBatchcodeTablesMerger.py | 10 ++++ ...ountryCountsByBatchcodeTablesMergerTest.py | 52 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/CountryCountsByBatchcodeTablesMerger.py create mode 100644 src/CountryCountsByBatchcodeTablesMergerTest.py diff --git a/src/CountryCountsByBatchcodeTablesMerger.py b/src/CountryCountsByBatchcodeTablesMerger.py new file mode 100644 index 00000000000..171c83b52d2 --- /dev/null +++ b/src/CountryCountsByBatchcodeTablesMerger.py @@ -0,0 +1,10 @@ +import pandas as pd + +class CountryCountsByBatchcodeTablesMerger: + + @staticmethod + def merge(countryCountsByBatchcodeTables): + return (pd + .concat(countryCountsByBatchcodeTables) + .groupby(countryCountsByBatchcodeTables[0].index.names) + .sum()) diff --git a/src/CountryCountsByBatchcodeTablesMergerTest.py b/src/CountryCountsByBatchcodeTablesMergerTest.py new file mode 100644 index 00000000000..b78680810d9 --- /dev/null +++ b/src/CountryCountsByBatchcodeTablesMergerTest.py @@ -0,0 +1,52 @@ +import unittest +from pandas.testing import assert_frame_equal +from TestHelper import TestHelper +import pandas as pd +from CountryCountsByBatchcodeTablesMerger import CountryCountsByBatchcodeTablesMerger + +class CountryCountsByBatchcodeTablesMergerTest(unittest.TestCase): + + def test_merge(self): + # Given + countryCountsByBatchcodeTable1 = TestHelper.createDataFrame( + columns = ['COUNTRY_COUNT_BY_VAX_LOT'], + data = [ [10], + [15]], + index = pd.MultiIndex.from_tuples( + names = ['VAX_LOT', 'COUNTRY'], + tuples = [['12345', 'Germany'], + ['AAA', 'United States']])) + countryCountsByBatchcodeTable2 = TestHelper.createDataFrame( + columns = ['COUNTRY_COUNT_BY_VAX_LOT'], + data = [ [20]], + index = pd.MultiIndex.from_tuples( + names = ['VAX_LOT', 'COUNTRY'], + tuples = [['12345', 'Germany']])) + + # When + dataFrame = CountryCountsByBatchcodeTablesMerger.merge([countryCountsByBatchcodeTable1, countryCountsByBatchcodeTable2]) + + # Then + assert_frame_equal( + dataFrame, + TestHelper.createDataFrame( + columns = ['COUNTRY_COUNT_BY_VAX_LOT'], + data = [ [10 + 20], + [15]], + index = pd.MultiIndex.from_tuples( + names = ['VAX_LOT', 'COUNTRY'], + tuples = [['12345', 'Germany'], + ['AAA', 'United States']]))) + +def _getCountryCountsByClickedBatchcode(): + exploration = pd.read_csv('src/data/Country By Clicked Batchcode 20230302-20230430.csv', index_col = 0, skiprows = [0, 1, 2, 3, 4, 5, 7]) + exploration.index.name = 'VAX_LOT' + exploration.rename( + columns = + { + 'Country': 'COUNTRY', + 'Event count': 'COUNTRY_COUNT_BY_VAX_LOT' + }, + inplace = True) + exploration.set_index('COUNTRY',append = True, inplace = True) + return exploration