Add support for dataframes

master
Dan Howe 6 years ago
parent 6e15b63087
commit 76e8df9035

@ -28,23 +28,24 @@ def _convert(x, length_scale_factor, input_unit, target_unit):
# Convert to output units
x_scaled *= unit_conversion_factor
# Scale time (dataframe or series only)
try:
x_scaled.index *= length_scale_factor**TIME_EXPONENT
except (AttributeError, TypeError):
pass
return x_scaled
def proto_to_model(x_proto, length_scale, input_unit, target_unit):
def proto_to_model(x_proto,
length_scale,
input_unit,
target_unit,
index_input_unit=None,
index_target_unit=None):
"""Convert prototype value(s) to model value(s) in specified units.
Args:
x_proto: prototype values (array_like, or pandas dataframe)
length_scale: ratio between prototype and model dimensions (float)
input_unit: unit of input (string)
target_unit: unit of output (string)
x_proto: prototype values (array_like, or pandas dataframe)
length_scale: ratio between proto and model dimensions (float)
input_unit: unit of input (string)
target_unit: unit of output (string)
index_input_unit: unit of input index (dataframe only)
index_target_unit: unit of output index (dataframe only)
Returns:
input values in model scale
@ -52,17 +53,36 @@ def proto_to_model(x_proto, length_scale, input_unit, target_unit):
length_scale_factor = 1 / length_scale
return _convert(x_proto, length_scale_factor, input_unit, target_unit)
# Convert values
x_model = _convert(x_proto, length_scale_factor, input_unit, target_unit)
# Convert index (dataframe or series only)
if (index_input_unit is not None) and (index_target_unit is not None):
if type(x_model).__name__ in ['DataFrame', 'Series']:
x_model.index = _convert(x_model.index, length_scale_factor,
index_input_unit, index_target_unit)
else:
raise ValueError("'index_input_unit' and 'index_target_unit' "
"can only be used when input is dataframe")
return x_model
def model_to_proto(x_model, length_scale, input_unit, target_unit):
def model_to_proto(x_model,
length_scale,
input_unit,
target_unit,
index_input_unit=None,
index_target_unit=None):
"""Convert model value(s) to prototype value(s) in specified units.
Args:
x_model: model values (array_like, or pandas dataframe)
length_scale: ratio between prototype and model dimensions (float)
input_unit: unit of input (string)
target_unit: unit of output (string)
x_model: model values (array_like, or pandas dataframe)
length_scale: ratio between proto and model dimensions (float)
input_unit: unit of input (string)
target_unit: unit of output (string)
index_input_unit: unit of input index (dataframe only)
index_target_unit: unit of output index (dataframe only)
Returns:
input values in prototype scale
@ -70,7 +90,19 @@ def model_to_proto(x_model, length_scale, input_unit, target_unit):
length_scale_factor = length_scale
return _convert(x_model, length_scale_factor, input_unit, target_unit)
# Convert values
x_proto = _convert(x_model, length_scale_factor, input_unit, target_unit)
# Convert index (dataframe or series only)
if (index_input_unit is not None) and (index_target_unit is not None):
if type(x_proto).__name__ in ['DataFrame', 'Series']:
x_proto.index = _convert(x_proto.index, length_scale_factor,
index_input_unit, index_target_unit)
else:
raise ValueError("'index_input_unit' and 'index_target_unit' "
"can only be used when input is dataframe")
return x_proto
def dimensions(unit):

Loading…
Cancel
Save