Coverage for src/sensai/util/dtype.py: 65%

20 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-13 22:17 +0000

1""" 

2This function contains various functions for data type conversions 

3""" 

4 

5from typing import Union, TypeVar, Sequence, List 

6import logging 

7 

8import numpy as np 

9import pandas as pd 

10 

11 

12log = logging.getLogger(__name__) 

13T = TypeVar("T") 

14 

15 

16def to_float_array(data: Union[pd.DataFrame, np.ndarray]) -> np.ndarray: 

17 if type(data) is np.ndarray: 

18 values = data 

19 elif type(data) is pd.DataFrame: 

20 values = data.values 

21 else: 

22 raise ValueError(f"Expected DataFrame or numpy array, got {data}") 

23 if values.dtype == "object": 

24 log.warning("Input array of dtype 'object' will be converted to float64 - this is potentially unsafe!") 

25 values = values.astype("float64", copy=False) 

26 return values 

27 

28 

29def dict_to_ordered_tuples(d: dict): 

30 keys = sorted(d.keys()) 

31 values = [d[k] for k in keys] 

32 return tuple(keys), tuple(values)