Skip to content

Adding by_charge_key argument to calculate_itemized_cost, and some bug fixes#51

Merged
fletchapin merged 4 commits into
mainfrom
itemize-charge-key
Feb 19, 2026
Merged

Adding by_charge_key argument to calculate_itemized_cost, and some bug fixes#51
fletchapin merged 4 commits into
mainfrom
itemize-charge-key

Conversation

@dalyw

@dalyw dalyw commented Feb 5, 2026

Copy link
Copy Markdown
Contributor

Main update: adding by_charge_key argument to calculate_itemized_cost in costs.py, to also return itemized costs by charge key if requested. (for use with flows-mpc)

Adding test for by_charge_key = True

Other bug fixes:n

  • Adding missing pytz requirement for tests
  • adding copy of charge in costs.py during unit conversion to avoid pandas warning
  • Fixing read-only issue in hours definition in get_charge_dict and create_charge_array. Also in test_create_charge_array. Issue is that with pandas 3.0.0, datetime.dt.hours.values is read-only and can't have minutes added

Reformatted with black

Tests passing with python 3.11 and pandas 3.0

Pull request recommendations:

  • Name your pull request your-development-type/short-description. Ex: feature/gui
  • Link to any relevant issue in the PR description. Ex: Resolves [ENH: Support optimization of flexibility metrics #12]
  • Provide context of changes.
  • Provide relevant tests for your feature or bug fix.
  • Provide or update documentation for any feature added by your pull request.

Thanks for contributing!

dalyw added 2 commits February 5, 2026 13:40
…to also return itemized costs by charge key if requested. (for use with flows-mpc)

Adding missing pytz requirement for tests

adding copy of charge in costs.py during unit conversion to avoid pandas issue

Fixing read-only issue in hours definition in get_charge_dict and create_charge_array. Also in test_create_charge_array. Issue is that with pandas 3.0.0,   datetime.dt.hours.values is read-only and can't have minutes added

Reformatted with black

Tests passing with python 3.11 and pandas 3.0
Integrating total_cost into calculate_itemized_cost loop since dict has a different structure with by_charge_key True
@codecov-commenter

codecov-commenter commented Feb 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.38%. Comparing base (63ef646) to head (c2c2244).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #51      +/-   ##
==========================================
+ Coverage   93.40%   94.38%   +0.97%     
==========================================
  Files          10       10              
  Lines        1593     1603      +10     
==========================================
+ Hits         1488     1513      +25     
+ Misses        105       90      -15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

dalyw added 2 commits February 5, 2026 14:34
…s data has no_day_var=False and rollover=True across months

Adding more specific charge key checks in test_costs updates for by_charge_key

@fletchapin fletchapin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one question about copying the charges

Comment thread eeco/costs.py
# if not specified, assume the old format (imperial units)
if utility == GAS:
# convert from therms to default units of cubic meters
charge = charge.copy()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the goal of copying this dictionary? Was there a bug that you found?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This came up when running with pandas 3.0 from flows-mpc, which had the older billing format in tests.

It was raising a ChainedAssignmentError: https://pandas.pydata.org/docs/user_guide/copy_on_write.html#chained-assignment

Instead of making a copy, we could possibly use .loc (like the linked example) if modifying limit_charges directly instead of charge, but that would probably be more complicated.

I ended up changing the flows-mpc billing csvs to the new format so it's not actually hitting this code anymore. Think it's worth adding a test case for this "except" section?

@fletchapin fletchapin merged commit 272d0d8 into main Feb 19, 2026
2 checks passed
@dalyw dalyw deleted the itemize-charge-key branch February 19, 2026 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants