TypeConverter.py 867 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Fri Apr 24 09:06:13 2020
  5. @author: tanya
  6. """
  7. import numpy as np
  8. import pandas as pd
  9. class TypeConverter:
  10. """
  11. Library for methods to manage python types
  12. """
  13. def __init__(self):
  14. """
  15. """
  16. from cdplib.log import Log
  17. self._logger = Log("TypeConverter")
  18. def convert_to_ndarray(self, x: (pd.DataFrame, np.ndarray)) -> np.ndarray:
  19. '''
  20. Converts an DataFrame to an numpy array.
  21. '''
  22. if isinstance(x, np.ndarray):
  23. return x
  24. elif (isinstance(x, pd.core.frame.DataFrame))\
  25. or (isinstance(x, pd.core.series.Series)):
  26. return x.values
  27. else:
  28. self._logger.log_and_raise_error_stack_info(
  29. 'The argument must be a numpy array or a pandas DataFrame')