diff --git a/flasc/flasc_dataframe.py b/flasc/flasc_dataframe.py index bdf48498..5d78ea81 100644 --- a/flasc/flasc_dataframe.py +++ b/flasc/flasc_dataframe.py @@ -23,7 +23,8 @@ def __init__(self, *args, name_map=None, **kwargs): Args: *args: arguments to pass to the DataFrame constructor name_map (dict): Dictionary of column names to map from the user format to the FLASC - format. + format, where the key string is the user format and the value string is the FLASC + equivalent. Defaults to None. **kwargs: keyword arguments to pass to the DataFrame constructor """ super().__init__(*args, **kwargs) @@ -59,6 +60,15 @@ def __str__(self): def convert_to_user_format(self, inplace=False): """Convert the DataFrame to the format that the user expects, given the name_map.""" + # Convert the format + if self._user_format == "long": + self._convert_wide_to_long() # Should this be assigned to something? + elif self._user_format == "semiwide": + self._convert_wide_to_semiwide() # Should this be assigned to something? + elif self._user_format == "wide": + pass + + # Convert column names and return if self.name_map is not None: return self.rename(columns={v: k for k, v in self.name_map.items()}, inplace=inplace) else: @@ -66,6 +76,15 @@ def convert_to_user_format(self, inplace=False): def convert_to_flasc_format(self, inplace=False): """Convert the DataFrame to the format that FLASC expects.""" + # Convert the format + if self._user_format == "long": + self._convert_long_to_wide() # Should this be assigned to something? + elif self._user_format == "semiwide": + self._convert_semiwide_to_wide() # Should this be assigned to something? + elif self._user_format == "wide": + pass + + # Convert column names and return if self.name_map is not None: return self.rename(columns=self.name_map, inplace=inplace) else: @@ -88,7 +107,14 @@ def _convert_wide_to_long(self): def _convert_wide_to_semiwide(self): """Convert a wide format DataFrame to a semiwide format DataFrame.""" - pass + if "time" not in self.columns: + raise ValueError("Column 'time' must be present in the DataFrame") + + # Should have columns: + # time + # turbine_id (as specified by the user) + # variable + # value # Likely this will be used for testing, later but it's convenient for prototyping here @@ -143,6 +169,6 @@ def _convert_wide_to_semiwide(self): - One column for time stamp - One column for each channel for each turbine - Converting between semilong and wide should be relatively straightforward. + Converting between semiwide and wide should be relatively straightforward. Actually, neither of these should be too bad """ diff --git a/tests/flasc_dataframe_test.py b/tests/flasc_dataframe_test.py index 7390ed4d..d59f776b 100644 --- a/tests/flasc_dataframe_test.py +++ b/tests/flasc_dataframe_test.py @@ -1,11 +1,55 @@ +import os + import pandas as pd from flasc.flasc_dataframe import FlascDataFrame +test_data_dict = {"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]} + +test_name_map = {"a": "AA"} + def test_type(): - df = FlascDataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}, name_map={"a": "AA"}) + df = FlascDataFrame(test_data_dict, name_map=test_name_map) assert isinstance(df, FlascDataFrame) # Assert df is a pandas DataFrame assert isinstance(df, pd.DataFrame) + + +def test_pickle(): + df = FlascDataFrame(test_data_dict) + df.name_map = test_name_map + df.to_pickle("test_pickle.pkl") + + df2 = pd.read_pickle("test_pickle.pkl") + assert isinstance(df2, FlascDataFrame) + assert df2.name_map == test_name_map + + os.remove("test_pickle.pkl") + + +def test_feather(): + df = FlascDataFrame(test_data_dict, name_map=test_name_map) + df.to_feather("test_feather.ftr") + + df2 = pd.read_feather("test_feather.ftr") + # Loaded DataFrame is a pandas DataFrame, not a FlascDataFrame + assert not isinstance(df2, FlascDataFrame) + assert isinstance(df2, pd.DataFrame) + assert not hasattr(df2, "name_map") + + os.remove("test_feather.ftr") + + +def test_csv(): + df = FlascDataFrame(test_data_dict, name_map=test_name_map) + df.to_csv("test_csv.csv") + + df2 = pd.read_csv("test_csv.csv") + # Loaded DataFrame is a pandas DataFrame, not a FlascDataFrame + assert not isinstance(df2, FlascDataFrame) + assert isinstance(df2, pd.DataFrame) + assert not hasattr(df2, "name_map") + + os.remove("test_csv.csv")