Skip to content

Commit

Permalink
Units are now reported properly on NDFrame operations if all columns …
Browse files Browse the repository at this point in the history
…have the same units (#26)

* Reports units for frame ops if units are all the same for each column

* isort black
  • Loading branch information
Samuel Letellier-Duchesne authored Apr 28, 2021
1 parent 3050d9a commit e594989
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
4 changes: 2 additions & 2 deletions energy_pandas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""energy-pandas Module."""

from outdated import warn_if_outdated
from pkg_resources import DistributionNotFound, get_distribution

from .energypandas import EnergySeries, EnergyDataFrame
from pkg_resources import get_distribution, DistributionNotFound
from .energypandas import EnergyDataFrame, EnergySeries

# Version of the package
try:
Expand Down
8 changes: 6 additions & 2 deletions energy_pandas/energypandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
_setup_subplots,
save_and_show,
)
from .units import unit_registry, IP_DEFAULT_CONVERSION, SI_DEFAULT_CONVERSION
from .units import IP_DEFAULT_CONVERSION, SI_DEFAULT_CONVERSION, unit_registry

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -143,7 +143,11 @@ def __finalize__(self, other, method=None, **kwargs):
for name in other._metadata:
if name == "units":
if isinstance(other, EnergyDataFrame):
setattr(self, name, getattr(other, "units").get(self.name))
if len(set(getattr(other, "units").values())) == 1:
# if all the same units
setattr(self, name, *set(getattr(other, "units").values()))
else:
setattr(self, name, getattr(other, "units").get(self.name))
else:
setattr(self, name, getattr(other, "units"))
elif name == "name":
Expand Down
3 changes: 1 addition & 2 deletions energy_pandas/plotting.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Plotting module."""

import logging
import os
import time

Expand All @@ -10,8 +11,6 @@
from numpy.core._asarray import asarray
from pandas.plotting._matplotlib.tools import create_subplots, flatten_axes

import logging

logger = logging.getLogger(__name__)
log = logger.info

Expand Down
2 changes: 1 addition & 1 deletion energy_pandas/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from tokenize import TokenInfo

import pint
import pkg_resources
from pint.compat import tokenizer

import pkg_resources
energyplus_registry = pkg_resources.resource_filename(__name__, "energyplus_en.txt")


Expand Down
16 changes: 13 additions & 3 deletions tests/test_energypandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pandas import date_range

from energy_pandas import EnergyDataFrame, EnergySeries
from energy_pandas.units import unit_registry, dash_to_mul, underline_dash
from energy_pandas.units import dash_to_mul, underline_dash, unit_registry


@pytest.fixture()
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_units(self, es):
assert es.units == unit_registry.degF
assert type(es) == EnergySeries

#set attribute
# set attribute
es.units = "degC"
assert es.units == unit_registry.degC

Expand Down Expand Up @@ -192,10 +192,16 @@ def test_mixed_units(self, edf_from_e_series):
"Series 1 degC": unit_registry.degC
}

def test_mixed_units_ops(self, edf_from_e_series):
col_1 = edf_from_e_series.iloc[:, 0]
col_2 = edf_from_e_series.iloc[:, 1]
# todo: deal with mixed units magnitude
assert (col_1 * col_2).units == "degree_Celsius"

def test_mixed_units_convert(self, edf_from_e_series):
assert edf_from_e_series.to_units("degR").units == {
"Series 1 degC": unit_registry.degR,
"Series 2 degK": unit_registry.degR
"Series 2 degK": unit_registry.degR,
}

def test_units_value(self, edf):
Expand Down Expand Up @@ -235,6 +241,10 @@ def test_slice(self, edf):
# check that the slice keeps the units
assert edf.units == {"Temp": edf["Temp"].units}

def test_numeric_operations(self, edf):
assert edf.mean(axis=1).units == "degree_Celsius"
assert edf.sum(axis=1).units == "degree_Celsius"

def test_repr(self, edf):
# check that a slice returns an EnergySeries
print(edf.__repr__())
Expand Down

0 comments on commit e594989

Please sign in to comment.