Functions#
Auxiliary functions.
- osc_physrisk_financial.functions.check_all_nonnumeric(arr)[source]#
Check if all elements in a numpy array or Python list are non-numeric.
This function tries to convert each element in the array or list to a float. If the conversion raises a ValueError or TypeError, or if the value is nan, it means the element is non-numeric, so the function continues to the next element. If the conversion does not raise an exception and the value is not nan, it means the element is numeric, so the function immediately returns False. If the function finishes checking all elements without finding a numeric one, it returns True.
- Parameters:
arr (numpy.ndarray or list) – The array or list to check.
- Returns:
True if all elements are non-numeric, False otherwise.
- Return type:
bool
- osc_physrisk_financial.functions.contains_word(string_list, word)[source]#
Check if strings in the given list contain the specified word. Words in each string are separated by underscores.
- Parameters:
string_list (list of str) – The list of strings where each string has words separated by underscores.
word (str) – The word to search for within the strings.
- Returns:
A list of strings from the input string_list that contain the specified word.
- Return type:
list of str
Examples
>>> contains_word(['word1_word2', 'word3_word4', 'word2_word5'], 'word2') ['word1_word2', 'word2_word5']
- osc_physrisk_financial.functions.dates_formatting(*date_sets)[source]#
Convert dates to a consistent format and sort them in ascending order.
- Parameters:
date_sets (pandas.DatetimeIndex,list of strings, pandas.Timestamp, or string) – Dates to be formatted. It can be a single date (as a pandas.Timestamp or its string representation) or an array-like object (as a pandas.DatetimeIndex or a list of its string representation) containing dates.
- Returns:
sorted_dates – A pandas DatetimeIndex object containing the formatted dates in ascending order.
- Return type:
pandas.DatetimeIndex
Examples
>>> dates_formatting('2022-01-01') DatetimeIndex(['2022-01-01'], dtype='datetime64[ns]', freq=None)
>>> dates_formatting(['2022-01-03', '2022-01-01', '2022-01-02']) DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03'], dtype='datetime64[ns]', freq=None)
>>> dates_formatting(['2022-01-03', '2022-01-01', '2022-01-02'], '2022-01-01') [DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03'], dtype='datetime64[ns]', freq=None), DatetimeIndex(['2022-01-01'], dtype='datetime64[ns]', freq=None)]
- osc_physrisk_financial.functions.find_root(func, x0, interval, tolerance=1e-08, fprime=None)[source]#
Find the root of a given function, using several methods.
Each method is tried in turn until one succeeds.
If none succeeds, we plot the function in interval.
- Parameters:
func (callable) – The function for which the root is to be computed.
x0 (float) – Initial guess for the root.
interval (list) – Interval [a,b] for ridder, bisecction and brentq.
tolerance (float) – If func(solution)>tolerance an exception is raised.
fprime (callable, optional) – The derivative of the function. If not provided, the Newton method will use the secant method.
- Returns:
The root found by the successful method (unless all methods failed).
- Return type:
float