Commit 133660a2 authored by Niklas Selke's avatar Niklas Selke
Browse files

The 'check_data' function now raises a 'ValueError' if there is any problem...

The 'check_data' function now raises a 'ValueError' if there is any problem with the given 'data' argument.
parent 66a77ddf
......@@ -89,16 +89,20 @@ def check_data(data_in, datetimes_in, values_in):
:param datetimes_in: the given datetimes argument
:param values_in: the given values argument
:return: The processed data argument or None if there was a problem
while processing the given data argument
:raises ValueError: raised if no datetime index is given or if no
values are given or if the lengths of the
datetime index and the values are different
:return: A data frame with a datetime index and a column of float or
int values
"""
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():
elif "value" in data_in.columns.to_list():
values_out = data_in["value"].to_numpy()
elif "values" in data_in.columns.to_numpy():
elif "values" in data_in.columns.to_list():
values_out = data_in["values"].to_numpy()
elif data_in.shape[1] > 0:
values_out = data_in.iloc[:, 0].to_numpy()
......@@ -107,16 +111,16 @@ def check_data(data_in, datetimes_in, values_in):
else:
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
if not isinstance(index_out, pd.DatetimeIndex) or index_out.dropna().empty:
raise ValueError("No datetime index given")
if (not isinstance(values_out, np.ndarray) or values_out.ndim != 1
or not (np.issubdtype(values_out.dtype, np.floating)
or np.issubdtype(values_out.dtype, np.integer))
or all(np.isnan(values_out))):
raise ValueError("Values must be given in a one-dimensional float or"
" int numpy array")
if index_out.size != values_out.size:
raise ValueError("Datetime index and values must have the same length")
if index_out.tz:
index_out = index_out.tz_localize(None)
return pd.DataFrame({"values": values_out}, index=index_out)
......
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