Commit c9ad31c4 authored by Niklas Selke's avatar Niklas Selke
Browse files

Refactored the 'check_data' function in ''. It now returns a data frame.

parent b44c5acf
"""Functions to check the given user inputs.
This module contains the following function:
This module contains the following functions:
check_sampling - check the given sampling argument
check_statistics - check the given statistics argument
check_data - check the given data argument
import numpy as np
......@@ -56,46 +58,46 @@ def check_statistics(statistics_in):
return statistics_out
def check_data(input_argument, alt_input_idx, alt_input_vals):
def check_data(data_in, datetimes_in, values_in):
"""Check the given data argument.
:param input_argument: the given argument
:param alt_input_idx: the alternative input's index
:param alt_input_vals: the alternative input's values
:param data_in: the given data argument
:param datetimes_in: the given datetimes argument
:param values_in: the given values argument
:return: The processed arguments or None if there was a problem with
the input
:return: The processed data argument or None if there was a problem
while processing the given data argument
if ((input_argument is not None and not (alt_input_idx is None
and alt_input_vals is None))
or (input_argument is None and (alt_input_idx is None
or alt_input_vals is None))):
return None, None
if isinstance(input_argument, (pd.DataFrame, pd.Series)):
index = input_argument.index
if isinstance(input_argument, pd.Series):
param_vals = input_argument.to_numpy()
elif "value" in input_argument.columns.to_numpy():
param_vals = input_argument["value"].to_numpy()
elif "values" in input_argument.columns.to_numpy():
param_vals = input_argument["values"].to_numpy()
elif input_argument.shape[1] > 0:
param_vals = input_argument.iloc[:, 0].to_numpy()
if (data_in is not None
and not (datetimes_in is None and values_in is None)):
return None
if data_in is None and (datetimes_in is None or values_in is None):
return None
if isinstance(data_in, (pd.DataFrame, pd.Series)):
index_out = data_in.index
if isinstance(data_in, pd.Series):
values_out = data_in.to_numpy()
elif "value" in data_in.columns.to_numpy():
values_out = data_in["value"].to_numpy()
elif "values" in data_in.columns.to_numpy():
values_out = data_in["values"].to_numpy()
elif data_in.shape[1] > 0:
values_out = data_in.iloc[:, 0].to_numpy()
return None, None
values_out = np.array([])
index = alt_input_idx
param_vals = alt_input_vals
if not isinstance(index, pd.DatetimeIndex):
return None, None
if not isinstance(param_vals, np.ndarray) or param_vals.ndim != 1:
return None, None
if (index.dropna().empty or all(np.isnan(param_vals))
or index.size != param_vals.size):
return None, None
if not (np.issubdtype(param_vals.dtype, np.floating)
or np.issubdtype(param_vals.dtype, np.integer)):
return None, None
index = index.tz_localize(None)
return index, param_vals
index_out = datetimes_in
values_out = values_in
if not isinstance(index_out, pd.DatetimeIndex):
return None
if not isinstance(values_out, np.ndarray) or values_out.ndim != 1:
return None
if not (np.issubdtype(values_out.dtype, np.floating)
or np.issubdtype(values_out.dtype, np.integer)):
return None
if (index_out.dropna().empty or all(np.isnan(values_out))
or index_out.size != values_out.size):
return None
index_out = index_out.tz_localize(None)
return pd.DataFrame({"values": values_out}, index=index_out)
......@@ -92,4 +92,4 @@ def calculate_statistics(sampling, statistics, data=None, metadata=None,
sampling_method = check_sampling(sampling)
stats_list = check_statistics(statistics)
datetime_index, parameter_values = check_data(data, datetimes, values)
data_frame = check_data(data, datetimes, values)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment