Skip to content

Commit

Permalink
Merge pull request #315 from USEPA/v1.3.1-release
Browse files Browse the repository at this point in the history
V1.3.1 release
  • Loading branch information
catherinebirney authored Apr 20, 2023
2 parents 13c18d5 + d6b8561 commit 45a2220
Show file tree
Hide file tree
Showing 24 changed files with 2,257 additions and 1,852 deletions.
4 changes: 2 additions & 2 deletions examples/generate_data_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@
####### GHG Bar Chart ############
# Option 1 - GHG emissions by GHG
dv.stackedBarChart('GHG_national_2018_m1',
sectors_to_include=['111110', '112120', '325312'],
selection_fields={'Sector': ['111110', '112120', '325312']},
filename='GHGEmissions')

# Option 2 - specify indicator, much have LCIAformatter installed
# https://github.com/USEPA/LCIAformatter
dv.stackedBarChart('GHG_national_2018_m1', impact_cat='Global warming',
sectors_to_include=['111110', '112120', '325312'],
selection_fields={'Sector': ['111110', '112120', '325312']},
filename='GHGEmissionsGlobalWarming')
112 changes: 57 additions & 55 deletions flowsa/data/VisualizationEssentials.csv
Original file line number Diff line number Diff line change
@@ -1,55 +1,57 @@
Sector,Color,Abrv_Name
11,#33A02C,Ag
115112,#228B22,Land App
21,#000000,Mining
22,#696969,Utilities
2212,#00EEEE,Nat Gas
221320,#3090C7,Sewer
23,#C0C0C0,Construction
236115,#a45a52,Single-Family Housing Const
236116,#7fffd4,Multifamily Housing Const
236117,#dd8888,Housing For-Sale Builders
236118,#657065,Res. Remodelers
236210,#FF7F00,Industrial Bldg Const
236220,#7D9EC0,Commercial/Institutional Bldg Const
237110,#8B0000,Water/Sewer Const
237130,#71C671,Power/Com. Const
237310,#4876FF,Road/Bridge Const
237990,#912CEE,Other Const
238910,#FF1493,Site Prep
31,#E31A1C,Mfg - 31
311111,#C1FFC1,Dog/Cat Food Mfg
311119,#954535,Animal Feed
32,#E31A1C,Mfg - 32
324110,#00688B,Biochem Proc
327310,#16645f,Concrete Mfg
33,#E31A1C,Mfg - 33
42,#1F78B4,Wholesale Trade
44,#A6CEE3,Retail Trade - 44
45,#ECC5C0,Retail Trade - 45
48,#FF6F00,Transp & Whs - 48
49,#E56717,Transp & Whs - 49
51,#FDBF6F,Info
52,#6A3D9A,Finance and Ins
53,#99C68E,Real Estate
54,#CAB2D6,Prof and Tech Sev
55,#583759,Mngmt
56,#6667AB,Admin & Waste Serv
5622121,#FFF380,MSW Landfill
562213,#654A8B,Solid Waste
5622191,#C88141,Anaerobic Digestion
5622192,#F67280,Composting
5629201,#FFA15A,MSW Recycling
5629202,#91a3b0,Mixed CDD Proc.
5629203,#9999ff,Concrete Proc.
61,#800000,Educ
62,#7E354D,Health Care
624210,#CCCCFF,Food Bank
71,#BAB86C,Arts & Rec
72,#D2691E,Acc & Food Serv
81,#FF007F,Other Serv
92,#9F000F,Pub Admin
F010,#D58A94,Residential
S00201,#B2DF8A,Gov Loc Psg Transit
S00101,B5EAAA,Gov Fed Elec Util
S00202,#98FF98,Gov Loc Elec Util
Sector,AttributionSource,Color,Abrv_Name
11,,#33A02C,Ag
115112,,#228B22,Land App
21,,#000000,Mining
22,,#696969,Utilities
2212,,#00EEEE,Nat Gas
221320,,#3090C7,Sewer
23,,#C0C0C0,Construction
236115,,#a45a52,Single-Family Housing Const
236116,,#7fffd4,Multifamily Housing Const
236117,,#dd8888,Housing For-Sale Builders
236118,,#657065,Res. Remodelers
236210,,#FF7F00,Industrial Bldg Const
236220,,#7D9EC0,Commercial/Institutional Bldg Const
237110,,#8B0000,Water/Sewer Const
237130,,#71C671,Power/Com. Const
237310,,#4876FF,Road/Bridge Const
237990,,#912CEE,Other Const
238910,,#FF1493,Site Prep
31,,#E31A1C,Mfg - 31
311111,,#C1FFC1,Dog/Cat Food Mfg
311119,,#954535,Animal Feed
32,,#E31A1C,Mfg - 32
324110,,#00688B,Biochem Proc
327310,,#16645f,Concrete Mfg
33,,#E31A1C,Mfg - 33
42,,#1F78B4,Wholesale Trade
44,,#A6CEE3,Retail Trade - 44
45,,#ECC5C0,Retail Trade - 45
48,,#FF6F00,Transp & Whs - 48
49,,#E56717,Transp & Whs - 49
51,,#FDBF6F,Info
52,,#6A3D9A,Finance and Ins
53,,#99C68E,Real Estate
54,,#CAB2D6,Prof and Tech Sev
55,,#583759,Mngmt
56,,#6667AB,Admin & Waste Serv
5622121,,#FFF380,MSW Landfill
562213,,#654A8B,Solid Waste
5622191,,#C88141,Anaerobic Digestion
5622192,,#F67280,Composting
5629201,,#FFA15A,MSW Recycling
5629202,,#91a3b0,Mixed CDD Proc.
5629203,,#9999ff,Concrete Proc.
61,,#800000,Educ
62,,#7E354D,Health Care
624210,,#CCCCFF,Food Bank
71,,#BAB86C,Arts & Rec
72,,#D2691E,Acc & Food Serv
81,,#FF007F,Other Serv
92,,#9F000F,Pub Admin
F010,,#D58A94,Residential
S00201,,#B2DF8A,Gov Loc Psg Transit
S00101,,B5EAAA,Gov Fed Elec Util
S00202,,#98FF98,Gov Loc Elec Util
,Direct,#4C61AD,Direct
,Allocated,#6CDF97,Allocated
221 changes: 136 additions & 85 deletions flowsa/data/activitytosectormapping/NAICS_Crosswalk_EPA_GHGI.csv

Large diffs are not rendered by default.

50 changes: 25 additions & 25 deletions flowsa/data/external_data/GHGI_Table_3-22b.csv
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
"Carbon emissions (MMT CO2eq), Updated to 2018 data year, based on Table 3-22 in 2021 GHGI Report",,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(MMT CO2 Eq.),(MMT CO2 Eq.),(MMT CO2 Eq.),(MMT CO2 Eq.),(MMT CO2 Eq.),(MMT CO2 Eq.)
Sector/Fuel Type,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018
Industry,112.8,112.8,112.8,112.8,112.8,112.8,112.8,112.8,112.8,112.8,112.8,112.8,112.8,101.8,132.8,123.4,120.8,117,117.5,111.1,111.9,113.4,93.9,108.4,101.6,110.5,97.6,108.5,120.2
Industrial Coking Coal,8.42,8.42,8.42,8.42,8.42,8.42,8.42,8.42,8.42,8.42,8.42,8.42,8.42,8.418181818,21.9,14,8.8,3.4,5.1,0.6,5.5,6.2,12.5,12.2,2.4,12.5,9.1,11.5,12.8
Industrial Other Coal,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.4,0.4,0.4,0.4,0.4,0.5,0.5,0.4,0.4,0.3,0.3,0.3,0.3,0.3,0.3,0.3
Natural Gas to Chemical Plants,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,7.4,7.6,7.5,8.2,8.1,7.9,8.1,4.8,4.8,4.7,5.3,5.6,5.3,4.6,5.3,5.6
Asphalt & Road Oil,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0,0,0,0,0,0,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3
LPG,38.58,38.58,38.58,38.58,38.58,38.58,38.58,38.58,38.58,38.58,38.58,38.58,38.58,31.9,33.2,34.3,35.3,35.2,34.2,37.9,46.1,49.9,35.7,43.7,45.6,44.6,39.7,44.8,53.9
Lubricants,9.88,9.88,9.88,9.88,9.88,9.88,9.88,9.88,9.88,9.88,9.88,9.88,9.88,10.8,10.8,10.8,8.8,10.8,10.1,9,10.1,9.5,8.8,9.3,9.7,10.5,10,8.4,8.2
Pentanes Plus,2.43,2.43,2.43,2.43,2.43,2.43,2.43,2.43,2.43,2.43,2.43,2.43,2.43,3.8,3.9,3.8,2.7,3.4,3.1,2.7,1.9,0.7,0.9,1.1,1.1,1.8,1.1,1.9,2.5
Naphtha (<401 F),13.27,13.27,13.27,13.27,13.27,13.27,13.27,13.27,13.27,13.27,13.27,13.27,13.27,13.7,17.2,17.4,15.2,14,12.3,12.9,13.1,12.9,8.8,11.5,10.2,9.4,8.1,9.2,9.9
Other Oil (>401 F),11.88,11.88,11.88,11.88,11.88,11.88,11.88,11.88,11.88,11.88,11.88,11.88,11.88,13.5,15.4,14.6,16.1,14.5,17.2,12.2,12.2,10.4,5.3,5.2,6,5.3,4.5,5.9,5.6
Still Gas,2.89,2.89,2.89,2.89,2.89,2.89,2.89,2.89,2.89,2.89,2.89,2.89,2.89,3,1.5,1.7,3,2.2,1.2,3.6,3.8,4.4,3.1,3.6,3.6,3.4,3.2,3.4,3.7
Petroleum Coke,5.33,5.33,5.33,5.33,5.33,5.33,5.33,5.33,5.33,5.33,5.33,5.33,5.33,4.6,9.7,7.4,9.1,11.8,11.8,9.5,0.1,0,0,0,0,0,0,NO,+
Special Naphtha,1.61,1.61,1.61,1.61,1.61,1.61,1.61,1.61,1.61,1.61,1.61,1.61,1.61,2,1.3,1.7,1.9,2.1,2.3,1.3,0.8,0.6,0.3,2.4,2.6,2.3,1.9,2.2,2.2
Distillate Fuel Oil,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.4,0.4,0.4,0.4,0.4,0.6,0.6,0.6,0.6,0.6,0.2,0.2,0.2,0.2,0.2,0.2
Waxes,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.9,0.9,1,0.8,0.7,0.6,0.4,0.5,0.4,0.5,0.5,0.5,0.4,0.4,0.3,0.4
Miscellaneous Products,10.85,10.85,10.85,10.85,10.85,10.85,10.85,10.85,10.85,10.85,10.85,10.85,10.85,9.3,8.5,8.4,9.9,9.9,10.6,11.3,11.8,12.2,12,12.7,13.6,14.1,14.2,14.8,14.7
Transportation,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,10.2,10.2,10.2,9.9,10.2,9.5,8.5,9.5,9,8.3,8.8,9.1,10,9.5,9.6,9.3
Lubricants,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,10.2,10.2,10.2,9.9,10.2,9.5,8.5,9.5,9,8.3,8.8,9.1,10,9.5,9.6,9.3
U.S. Territories,6.34,6.34,6.34,6.34,6.34,6.34,6.34,6.34,6.34,6.34,6.34,6.34,6.34,5.9,10.3,8.7,7.3,6.7,7.2,3.7,3.7,8.2,8.2,2.7,3.5,5.1,5.1,5.1,5.1
Lubricants,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0,0.1,0.6,0.4,0.3,0.4,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
Other Petroleum (Misc. Prod.),6.14,6.14,6.14,6.14,6.14,6.14,6.14,6.14,6.14,6.14,6.14,6.14,6.14,5.9,10.2,8.1,6.9,6.3,6.8,3.6,3.6,8.1,8.1,2.6,3.5,5,5,5,5
Total,128.52,128.52,128.52,128.52,128.52,128.52,128.52,128.52,128.52,128.52,128.52,128.52,128.52,118,152.3,142.3,138,133.9,134.2,123.4,125.1,130.6,110.3,119.8,114.3,125.5,112.2,123.2,134.6
"Carbon emissions (MMT CO2eq), Updated to 2018 data year, based on Table 3-22 in 2021 GHGI Report",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(Tg CO2 Eq.),(MMT CO2 Eq.),(MMT CO2 Eq.),(MMT CO2 Eq.),(MMT CO2 Eq.),(MMT CO2 Eq.),(MMT CO2 Eq.),(MMT CO2 Eq.)
Sector/Fuel Type,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019
Industry,112.8,112.8,112.8,112.8,112.8,112.8,112.8,112.8,112.8,112.8,112.8,112.8,112.8,101.8,132.8,123.4,120.8,117,117.5,111.1,111.9,113.4,93.9,108.4,101.6,110.5,97.6,108.5,120.2,119.2
Industrial Coking Coal,8.42,8.42,8.42,8.42,8.42,8.42,8.42,8.42,8.42,8.42,8.42,8.42,8.42,8.418181818,21.9,14,8.8,3.4,5.1,0.6,5.5,6.2,12.5,12.2,2.4,12.5,9.1,11.5,12.8,11.2
Industrial Other Coal,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.4,0.4,0.4,0.4,0.4,0.5,0.5,0.4,0.4,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3
Natural Gas to Chemical Plants,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,6.67,7.4,7.6,7.5,8.2,8.1,7.9,8.1,4.8,4.8,4.7,5.3,5.6,5.3,4.6,5.3,5.6,13.4
Asphalt & Road Oil,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0,0,0,0,0,0,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3
LPG,38.58,38.58,38.58,38.58,38.58,38.58,38.58,38.58,38.58,38.58,38.58,38.58,38.58,31.9,33.2,34.3,35.3,35.2,34.2,37.9,46.1,49.9,35.7,43.7,45.6,44.6,39.7,44.8,53.9,59.8
Lubricants,9.88,9.88,9.88,9.88,9.88,9.88,9.88,9.88,9.88,9.88,9.88,9.88,9.88,10.8,10.8,10.8,8.8,10.8,10.1,9,10.1,9.5,8.8,9.3,9.7,10.5,10,8.4,8.2,7.9
Pentanes Plus,2.43,2.43,2.43,2.43,2.43,2.43,2.43,2.43,2.43,2.43,2.43,2.43,2.43,3.8,3.9,3.8,2.7,3.4,3.1,2.7,1.9,0.7,0.9,1.1,1.1,1.8,1.1,1.9,2.5,3.9
Naphtha (<401 F),13.27,13.27,13.27,13.27,13.27,13.27,13.27,13.27,13.27,13.27,13.27,13.27,13.27,13.7,17.2,17.4,15.2,14,12.3,12.9,13.1,12.9,8.8,11.5,10.2,9.4,8.1,9.2,9.9,9.5
Other Oil (>401 F),11.88,11.88,11.88,11.88,11.88,11.88,11.88,11.88,11.88,11.88,11.88,11.88,11.88,13.5,15.4,14.6,16.1,14.5,17.2,12.2,12.2,10.4,5.3,5.2,6,5.3,4.5,5.9,5.6,6
Still Gas,2.89,2.89,2.89,2.89,2.89,2.89,2.89,2.89,2.89,2.89,2.89,2.89,2.89,3,1.5,1.7,3,2.2,1.2,3.6,3.8,4.4,3.1,3.6,3.6,3.4,3.2,3.4,3.7,3.9
Petroleum Coke,5.33,5.33,5.33,5.33,5.33,5.33,5.33,5.33,5.33,5.33,5.33,5.33,5.33,4.6,9.7,7.4,9.1,11.8,11.8,9.5,0.1,0,0,0,0,0,0,NO,+,NO
Special Naphtha,1.61,1.61,1.61,1.61,1.61,1.61,1.61,1.61,1.61,1.61,1.61,1.61,1.61,2,1.3,1.7,1.9,2.1,2.3,1.3,0.8,0.6,0.3,2.4,2.6,2.3,1.9,2.2,2.2,2.5
Distillate Fuel Oil,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.4,0.4,0.4,0.4,0.4,0.6,0.6,0.6,0.6,0.6,0.2,0.2,0.2,0.2,0.2,0.2,0.2
Waxes,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.9,0.9,1,0.8,0.7,0.6,0.4,0.5,0.4,0.5,0.5,0.5,0.4,0.4,0.3,0.4,0.3
Miscellaneous Products,10.85,10.85,10.85,10.85,10.85,10.85,10.85,10.85,10.85,10.85,10.85,10.85,10.85,9.3,8.5,8.4,9.9,9.9,10.6,11.3,11.8,12.2,12,12.7,13.6,14.1,14.2,14.8,14.7,0
Transportation,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,10.2,10.2,10.2,9.9,10.2,9.5,8.5,9.5,9,8.3,8.8,9.1,10,9.5,9.6,9.3,8.9
Lubricants,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,9.45,10.2,10.2,10.2,9.9,10.2,9.5,8.5,9.5,9,8.3,8.8,9.1,10,9.5,9.6,9.3,8.9
U.S. Territories,6.34,6.34,6.34,6.34,6.34,6.34,6.34,6.34,6.34,6.34,6.34,6.34,6.34,5.9,10.3,8.7,7.3,6.7,7.2,3.7,3.7,8.2,8.2,2.7,3.5,5.1,5.1,5.1,5.1,0.7
Lubricants,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0,0.1,0.6,0.4,0.3,0.4,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
Other Petroleum (Misc. Prod.),6.14,6.14,6.14,6.14,6.14,6.14,6.14,6.14,6.14,6.14,6.14,6.14,6.14,5.9,10.2,8.1,6.9,6.3,6.8,3.6,3.6,8.1,8.1,2.6,3.5,5,5,5,5,0.6
Total,128.52,128.52,128.52,128.52,128.52,128.52,128.52,128.52,128.52,128.52,128.52,128.52,128.52,118,152.3,142.3,138,133.9,134.2,123.4,125.1,130.6,110.3,119.8,114.3,125.5,112.2,123.2,134.6,128.8
86 changes: 72 additions & 14 deletions flowsa/data_source_scripts/BEA.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
Source csv files for BEA data are documented
in scripts/write_BEA_Use_from_useeior.py
"""
from functools import reduce
import pandas as pd
from flowsa.location import US_FIPS
from flowsa.common import fbs_activity_fields
from flowsa.schema import activity_fields
from flowsa.settings import externaldatapath
from flowsa.flowbyfunctions import assign_fips_location_system
from flowsa.flowbyfunctions import assign_fips_location_system, aggregator
from flowsa.fbs_allocation import allocation_helper


Expand Down Expand Up @@ -150,25 +151,33 @@ def bea_make_ar_parse(*, year, **_):
return df


def subset_BEA_table(df, attr, **_):
def subset_BEA_table(df_load, attr, **_):
"""
Modify loaded BEA table (make or use) based on data in the FBA method yaml
:param df: df, flowbyactivity format
:param df_load: df, flowbyactivity format
:param attr: dictionary, attribute data from method yaml for activity set
:return: modified BEA dataframe
"""
df2 = pd.DataFrame()
# extract commodity to filter and which Activity column used to filter
(commodity, ActivityCol), *rest = attr['clean_parameter'].items()
df = df.loc[df[ActivityCol] == commodity].reset_index(drop=True)

# set column to None to enable generalizing activity column later
df.loc[:, ActivityCol] = None
if set(fbs_activity_fields).issubset(df.columns):
for v in activity_fields.values():
if v[0]['flowbyactivity'] == ActivityCol:
SectorCol = v[1]['flowbysector']
df.loc[:, SectorCol] = None
return df
for commodity, ActivityCol in attr['clean_parameter'].items():
df = df_load.loc[df_load[ActivityCol] == commodity].reset_index(
drop=True)

# set column to None to enable generalizing activity column later
df.loc[:, ActivityCol] = None
if set(fbs_activity_fields).issubset(df.columns):
for v in activity_fields.values():
if v[0]['flowbyactivity'] == ActivityCol:
SectorCol = v[1]['flowbysector']
df.loc[:, SectorCol] = None
df2 = pd.concat([df, df2])

# aggregate cols
df3 = aggregator(df2, list(df2.select_dtypes(include=['object',
'int']).columns))

return df3


def subset_and_allocate_BEA_table(df, attr, **_):
Expand Down Expand Up @@ -200,3 +209,52 @@ def subset_and_allocate_BEA_table(df, attr, **_):
# Drop remaining rows with no sectors e.g. T001 and other final demands
df2 = df2.dropna(subset=['SectorConsumedBy']).reset_index(drop=True)
return df2


def subset_and_equally_allocate_BEA_table(df, attr, **_):
"""
Temporary function to equally attribute BEA table. This function will
be unnecessary after merge with recursive branch
"""
# Necessary to run equal attribution before subsetting dataset by an
# activity because in some situations both the BEA ActivityConsumedBy
# and ActivityProducedBy values map to multiple sectors. The
# "subset_BEA_table()" fxn resets one of the activity columns to NaN.
# For example, for "Liming" in the GHG attribution model,
# the ActivityProducedBy 327500 maps to both 327410 and 327420 - so data
# is double counted, but subsetting the BEA table resets
# ActivityProducedBy to NaN and that double counting is lost.

# equally attribute bea codes to each mapped sector
groupby_cols = ['group_id']
for c in ['Produced', 'Consumed']:
df = (
df
.assign(
**{f'_naics_{n}': df[f'Sector{c}By'].str.slice(stop=n)
for n in range(2, 7)},
**{f'_unique_naics_{n}_by_group': lambda x, i=n: (
x.groupby(groupby_cols if i == 2
else [*groupby_cols, f'_naics_{i - 1}'],
dropna=False)
[[f'_naics_{i}']]
.transform('nunique', dropna=False)
)
for n in range(2, 7)},
FlowAmount=lambda x: reduce(
lambda x, y: x / y,
[x.FlowAmount, *[x[f'_unique_naics_{n}_by_group']
for n in range(2, 7)]]
)
)
)
groupby_cols.append(f'Sector{c}By')

df = df.drop(
columns=[*[f'_naics_{n}' for n in range(2, 7)],
*[f'_unique_naics_{n}_by_group' for n in range(2, 7)]]
)

df2 = subset_BEA_table(df, attr)

return df2
Loading

0 comments on commit 45a2220

Please sign in to comment.