Skip to content
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
d8f7eaf
update pandas package requirements
catherinebirney Jun 10, 2025
02484a9
update pandas package requirements for google collab
catherinebirney Jun 10, 2025
95ac2fc
update additional package requirements for google collab
catherinebirney Jun 10, 2025
d368445
change numpy version to align with colab requirements
catherinebirney Jun 10, 2025
c1d5088
Merge branch 'dqi_update' into colab
catherinebirney Jun 13, 2025
8f6a088
avoid warning: "Downcasting object dtype arrays on .fillna, .ffill, .…
catherinebirney Jun 15, 2025
d998565
modify how index is assigned to a column name for new pkg versions
catherinebirney Jun 15, 2025
8d426de
updates to convert_to_geoscale() for updated package versions
catherinebirney Jun 15, 2025
2f2a0b7
update how indexes are assigned to column name
catherinebirney Jun 15, 2025
6150e24
address 'future.no_silent_downcasting' issue
catherinebirney Jun 15, 2025
87dfa55
put sum in quotes - avoid error from package update
catherinebirney Jun 16, 2025
e2a887a
use "convert_df_to_flowby" to indicate when certain 'flowby' code sho…
catherinebirney Jun 16, 2025
6feefbe
revert changes made when determining issues caused by pandas 2.2 upgrade
catherinebirney Jun 16, 2025
81c38d1
changes for package upgrades
catherinebirney Jun 16, 2025
a59c632
readd dev branch package requirements
catherinebirney Sep 4, 2025
eaf16cb
Merge branch 'develop' into colab
catherinebirney Oct 7, 2025
9792657
update package versions
catherinebirney Oct 8, 2025
d9851fc
modify apply() use to address futurewarning dataframegroupby.apply()
catherinebirney Oct 9, 2025
8439092
update how data is merged to correctly pull in national values
catherinebirney Oct 14, 2025
29e65ab
typo
catherinebirney Oct 14, 2025
cd3f5c4
update min python test version to 3.10 with updates to numpy/pandas p…
catherinebirney Oct 24, 2025
986bfad
run gh workflows with py-3.12 bc updates to numpy/pandas pkg versions
catherinebirney Oct 24, 2025
a21a494
update estimate_suppressed_sectors_equal_attribution() for updated pa…
catherinebirney Oct 27, 2025
e5583aa
drop numpy/pandas pkg requirements to maintain support for python 3.1…
catherinebirney Oct 28, 2025
c8dac73
for data pulled from stewi, use convert_df_to_flowby=True to ensure r…
catherinebirney Oct 28, 2025
1834345
option to modify subset cols and rounding cols if not all DQI columns…
catherinebirney Oct 28, 2025
1ec098c
address issue of stewi NAICS data imported with decimals
catherinebirney Oct 28, 2025
a93032b
address pandas future warnings on .fillna and .replace and concating …
catherinebirney Oct 28, 2025
9ec5db4
update canadian exchange rates
catherinebirney Oct 28, 2025
ac808d8
add to unit conversions
catherinebirney Oct 28, 2025
4de2ba3
address FutureWarning: Downcasting behavior in replace is deprecated
catherinebirney Oct 29, 2025
3ac8e1e
address numpy.exceptions.DTypePromotionError: The DType <class 'numpy…
catherinebirney Oct 29, 2025
a03ea1a
update python version requirement due to numpy version
catherinebirney Oct 29, 2025
e1ab626
address future warning Downcasting object dtype arrays on .fillna
catherinebirney Oct 29, 2025
fbc0e35
spelling
catherinebirney Oct 29, 2025
7450eee
address np.nan not being dropped in map_to_sectors()
catherinebirney Oct 29, 2025
d3db58e
skip equal attribution on columns that are all np.nan
catherinebirney Oct 29, 2025
6b9f9bb
at log statement for level currently being estimated
catherinebirney Oct 29, 2025
814dda8
address .loc warning
catherinebirney Oct 29, 2025
5bb0176
replace np.nan in sector columns with empty string when comparing 2 f…
catherinebirney Oct 30, 2025
f6abba8
in compare fbs fillna for all string merge cols to ensure correct merge
catherinebirney Oct 30, 2025
58ebe68
move where np.nans are converted to string and maintain leading 0s in…
catherinebirney Oct 30, 2025
e383238
reorder code in compare_FBS()
catherinebirney Oct 30, 2025
534b350
convert FBS to dfs in compare_fbs()
catherinebirney Oct 30, 2025
975a1f5
speed up weighted_average()
catherinebirney Nov 6, 2025
9e88ac0
when cacluatling golf/crop irrigation ensure activity cols are object…
catherinebirney Nov 7, 2025
db3f608
Merge pull request #458 from USEPA/colab
catherinebirney Nov 7, 2025
8762620
include FBS example to generate FBS, overwriting local copy
catherinebirney Nov 7, 2025
99ca25c
drop develop branch from requirements
catherinebirney Nov 7, 2025
f5d4b4a
bump flowsa package version
catherinebirney Nov 7, 2025
acc56f6
update estimate suppressed qcew fxn to work with updated pandas
catherinebirney Nov 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/compare_single_FBA.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.10"
python-version: "3.12"

- name: Update pip
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/compare_single_FBS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.10"
python-version: "3.12"

- name: Update pip
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/generate_FBA.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.10"
python-version: "3.12"

- name: Update pip
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
py-version: ['3.9', '3.10', '3.11']
py-version: ['3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -74,7 +74,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.10"
python-version: "3.12"

- name: Update pip & install testing pkgs
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test_methods.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.10"
python-version: "3.12"

- name: Update pip & install testing pkgs
run: |
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.10"
python-version: "3.12"

- name: Update pip & install testing pkgs
run: |
Expand Down
2 changes: 1 addition & 1 deletion docs/user_guide/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ To attribute a primary data source to target level sectors, identify the primary
specified attribution data source
- equal: Equally attribute parent values to child values until reach target
sector length
- inheritance: Assign parent values to all child values. Usefull in
- inheritance: Assign parent values to all child values. Useful in
situations where value is a rate, such as kg/m2.

### Recursive vs Sequential Attribution
Expand Down
8 changes: 8 additions & 0 deletions examples/get_flows_by_sector.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
:return: dataframe in flow by sector format

"""
import flowsa
from flowsa import getFlowBySector, collapse_FlowBySector, \
seeAvailableFlowByModels

Expand All @@ -54,3 +55,10 @@
# `Water_national_2015_m1` FBS will have 2 fewer rows, as the df is aggregated
# after dropping "SectorProducedBy" information
fbs_water_collapsed = collapse_FlowBySector('Water_national_2015_m1')

# alternatively, you can generate an FBS dataset even if you have a local copy. Your local copy will be overwritten
# if the same git hash.
flowsa.flowbysector.FlowBySector.generateFlowBySector(
method='Land_national_2012',
download_sources_ok=True # optionally download FBA data used to generate FBS
)
3 changes: 3 additions & 0 deletions flowsa/data/unit_conversion.csv
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ TON,kg,907.185
tons,kg,907.185
short tons,kg,907.185
Thousands of Tons,kg,907185
million short tons,kg,907184740
MT,kg,1000
Metric Tons,kg,1000
LB,kg,0.45359
ACRE FEET / ACRE,kg/m2,304.8000006
gal/USD,kg/USD,3.79
Quadrillion Btu,MJ,1.06E+12
Trillion Btu,MJ,1055055900
trillion Btu,MJ,1055055900
Tbtu,MJ,1055055900
million kWh,MJ,3600000
MT CO2e,MMT CO2e,0.000001
employees,p,1
Thousand USD,USD,1000
Expand Down
2 changes: 1 addition & 1 deletion flowsa/data_source_scripts/EIA_CBECS_Land.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def disaggregate_eia_cbecs_vacant_and_other(df_load):
df_nvno = df_nvno[df_nvno['ActivityConsumedBy'] != 'All buildings']
df_nvno = df_nvno.groupby(
['Unit', 'Location', 'LocationSystem', 'Year', 'Description'],
as_index=False).agg({'FlowAmount': sum})
as_index=False).agg({'FlowAmount': "sum"})
df_act = df_nvno.rename(columns={'FlowAmount': 'NonVacantNonOther'})

# merge df and subtract to determine FlowAmount to allocate to
Expand Down
4 changes: 2 additions & 2 deletions flowsa/data_source_scripts/EIA_MECS.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,8 @@ def update_regions_to_states(fba: FlowByActivity,
fba = (
fba
.drop(columns=['group_id', 'group_total'])
.reset_index(drop=True).reset_index()
.rename(columns={'index': 'group_id'})
.reset_index(drop=True)
.reset_index(names='group_id')
.assign(group_total=fba.FlowAmount)
)

Expand Down
27 changes: 11 additions & 16 deletions flowsa/data_source_scripts/EPA_GHGI.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
ANNEX_ENERGY_TABLES = ["A-" + str(x) for x in list(range(4,16))]

DROP_COLS = ["Unnamed: 0"] + list(pd.date_range(
start="1990", end="2010", freq='Y').year.astype(str))
start="1990", end="2010", freq='YE').year.astype(str))

YEARS = list(pd.date_range(start="2010", end="2024", freq='Y').year.astype(str))
YEARS = list(pd.date_range(start="2010", end="2024", freq='YE').year.astype(str))


def ghg_url_helper(*, build_url, config, **_):
Expand Down Expand Up @@ -496,20 +496,15 @@ def ghg_parse(*, df_list, year, config, **_):
# set suppressed values to 0 but mark as suppressed
# otherwise set non-numeric to nan
try:
df = (df.assign(
Suppressed = np.where(df.FlowAmount.str.strip() == "+", "+",
np.nan),
FlowAmount = pd.Series(
np.where(df.FlowAmount.str.strip() == "+", 0,
df.FlowAmount.str.replace(',',''))))
)
df = (df.assign(
FlowAmount = np.where(pd.to_numeric(
df.FlowAmount, errors='coerce').isnull(),
np.nan, pd.to_numeric(
df.FlowAmount, errors='coerce')))
.dropna(subset='FlowAmount')
)
df['Suppressed'] = (df['FlowAmount']
.astype(str).str.strip().eq('+')
.replace({True: '+', False: np.nan})
.infer_objects(copy=False)
)
df['FlowAmount'] = df['FlowAmount'].astype(str).str.replace(',', '').infer_objects(copy=False)
df['FlowAmount'] = df['FlowAmount'].replace('+', '0').infer_objects(copy=False)
df['FlowAmount'] = pd.to_numeric(df['FlowAmount'], errors='coerce')
df = df.dropna(subset='FlowAmount')
except AttributeError:
# if no string in FlowAmount, then proceed
df = df.dropna(subset='FlowAmount')
Expand Down
14 changes: 7 additions & 7 deletions flowsa/data_source_scripts/USDA_ERS_MLU.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def allocate_usda_ers_mlu_land_in_urban_areas(
df_non_urban_transport_area[['Location', 'Unit', 'FlowAmount']]
non_urban_transport_area_sum = df_non_urban_transport_area.groupby(
['Location', 'Unit'], as_index=False).agg(
{'FlowAmount': sum}).rename(columns={'FlowAmount': 'NonTransport'})
{'FlowAmount': "sum"}).rename(columns={'FlowAmount': 'NonTransport'})
# compare units
compare_df_units(fba, df_non_urban_transport_area)
# calculate total urban transportation by subtracting
Expand Down Expand Up @@ -232,7 +232,7 @@ def allocate_usda_ers_mlu_land_in_urban_areas(
air_rail_area = air_rail_area[['Location', 'Unit', 'FlowAmount']]
air_rail_area_sum = air_rail_area.groupby(
['Location', 'Unit'], as_index=False).agg(
{'FlowAmount': sum}).rename(columns={'FlowAmount': 'AirRail'})
{'FlowAmount': "sum"}).rename(columns={'FlowAmount': 'AirRail'})

df_highway = df_transport.merge(air_rail_area_sum, how='left')
df_highway = df_highway.assign(
Expand All @@ -256,8 +256,8 @@ def allocate_usda_ers_mlu_land_in_urban_areas(
allocated_urban_areas_df
.drop(columns=['group_id', 'group_total', 'rurl_res', 'total_area'],
errors='ignore')
.reset_index(drop=True).reset_index()
.rename(columns={'index': 'group_id'})
.reset_index(drop=True)
.reset_index(names='group_id')
.assign(group_total=allocated_urban_areas_df.FlowAmount)
)

Expand Down Expand Up @@ -317,7 +317,7 @@ def allocate_usda_ers_mlu_land_in_rural_transportation_areas(
air_rail_area = air_rail_area[['Location', 'Unit', 'FlowAmount']]
air_rail_area_sum = air_rail_area.groupby(
['Location', 'Unit'], as_index=False).agg(
{'FlowAmount': sum}).rename(columns={'FlowAmount': 'AirRail'})
{'FlowAmount': "sum"}).rename(columns={'FlowAmount': 'AirRail'})

# compare units
compare_df_units(fba, air_rail_area)
Expand All @@ -341,8 +341,8 @@ def allocate_usda_ers_mlu_land_in_rural_transportation_areas(
allocated_rural_trans = (
allocated_rural_trans
.drop(columns=['group_id', 'group_total'])
.reset_index(drop=True).reset_index()
.rename(columns={'index': 'group_id'})
.reset_index(drop=True)
.reset_index(names='group_id')
.assign(group_total=allocated_rural_trans.FlowAmount)
)

Expand Down
7 changes: 7 additions & 0 deletions flowsa/data_source_scripts/USGS_NWIS_WU.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,13 @@ def subset_and_merge_irrigation_types(df: FlowByActivity):
"ActivityConsumedBy_y": "Crop_ACB",
"Description_x": 'Description',
"Description_y": "Crop_Description"})

# ensure activity cols are object, not float, so not converted to 0
col = ['ActivityProducedBy', 'Golf_APB', 'Crop_APB']
for c in col:
df_m2[c] = df_m2[c].astype(str)


# fill na and sum crop and golf
for col in df_m2:
if df_m2[col].dtype in ("int", "float"):
Expand Down
1 change: 1 addition & 0 deletions flowsa/data_source_scripts/stewiFBS.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ def prepare_stewi_fbs(df_load, config) -> 'FlowBySector':
.reset_index(drop=True),
full_name=config.get('full_name'),
config=config,
convert_df_to_flowby=True
).prepare_fbs()

fbs.config.update({'data_format': 'FBS'})
Expand Down
19 changes: 11 additions & 8 deletions flowsa/dataclean.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,24 @@ def add_missing_flow_by_fields(flowby_partial_df, flowbyfields):
flowby_partial_df[col] = np.nan
# convert all None, 'nan' to np.nan
flowby_partial_df = (flowby_partial_df
.replace('None', np.nan)
.replace('nan', np.nan))
.replace({'None': np.nan, 'nan': np.nan})
.infer_objects(copy=False)
)
# convert data types to match those defined in flow_by_activity_fields
for k, v in flowbyfields.items():
if k in flowby_partial_df.columns:
flowby_partial_df[k] = \
flowby_partial_df[k].astype(v[0]['dtype'])
if v[0]['dtype'] in ['string', 'str', 'object']:
flowby_partial_df[k] = flowby_partial_df[k].fillna(np.nan)
flowby_partial_df[k] = flowby_partial_df[k].fillna(np.nan).infer_objects(copy=False)
else:
flowby_partial_df[k] = flowby_partial_df[k].fillna(0)
flowby_partial_df[k] = flowby_partial_df[k].fillna(0).infer_objects(copy=False)
# convert all None, 'nan' to np.nan
flowby_partial_df = (flowby_partial_df
.replace('None', np.nan)
.replace('nan', np.nan))
with pd.option_context('future.no_silent_downcasting', True):
flowby_partial_df = (flowby_partial_df
.replace({'None': np.nan, 'nan': np.nan})
.infer_objects(copy=False)
)
# Resort it so order is correct
cols = [e for e in flowbyfields.keys() if e in flowby_partial_df.columns]
flowby_df = flowby_partial_df[cols]
Expand Down Expand Up @@ -99,7 +102,7 @@ def standardize_units(df):
.merge(conversion_table, how='left',
left_on='Unit', right_on='old_unit')
.assign(Unit=lambda x: x.new_unit.mask(x.new_unit.isna(), x.Unit),
conversion_factor=lambda x: x.conversion_factor.fillna(1),
conversion_factor=lambda x: x.conversion_factor.fillna(1).infer_objects(copy=False),
FlowAmount=lambda x: x.FlowAmount * x.conversion_factor)
.drop(columns=['old_unit', 'new_unit', 'conversion_factor'])
)
Expand Down
Loading