timeseries_shaper.base

 1import pandas as pd
 2
 3class Base:
 4    def __init__(self, dataframe: pd.DataFrame, column_name: str = 'systime') -> pd.DataFrame:
 5        """
 6        Initializes the Base with a DataFrame, detects time columns, converts them to datetime,
 7        and sorts the DataFrame by the specified column (or the detected time column if applicable).
 8        
 9        Args:
10        - dataframe (pd.DataFrame): The DataFrame to be processed.
11        - column_name (str): The column to sort by. Default is 'systime'. If the column is not found
12                             or is not a time column, the class will attempt to detect other time columns.
13        """
14        self.dataframe = dataframe.copy()
15        
16        # Attempt to convert the specified column_name to datetime if it exists
17        if column_name in self.dataframe.columns:
18            self.dataframe[column_name] = pd.to_datetime(self.dataframe[column_name], errors='coerce')
19        else:
20            # If the column_name is not in the DataFrame, fallback to automatic time detection
21            time_columns = [col for col in self.dataframe.columns if 'time' in col.lower() or 'date' in col.lower()]
22            
23            # Convert all detected time columns to datetime, if any
24            for col in time_columns:
25                self.dataframe[col] = pd.to_datetime(self.dataframe[col], errors='coerce')
26            
27            # If any time columns are detected, sort by the first one; otherwise, do nothing
28            if time_columns:
29                column_name = time_columns[0]
30        
31        # Sort by the datetime column (either specified or detected)
32        if column_name in self.dataframe.columns:
33            self.dataframe = self.dataframe.sort_values(by=column_name)
34    
35    def get_dataframe(self) -> pd.DataFrame:
36        """Returns the processed DataFrame."""
37        return self.dataframe
class Base:
 4class Base:
 5    def __init__(self, dataframe: pd.DataFrame, column_name: str = 'systime') -> pd.DataFrame:
 6        """
 7        Initializes the Base with a DataFrame, detects time columns, converts them to datetime,
 8        and sorts the DataFrame by the specified column (or the detected time column if applicable).
 9        
10        Args:
11        - dataframe (pd.DataFrame): The DataFrame to be processed.
12        - column_name (str): The column to sort by. Default is 'systime'. If the column is not found
13                             or is not a time column, the class will attempt to detect other time columns.
14        """
15        self.dataframe = dataframe.copy()
16        
17        # Attempt to convert the specified column_name to datetime if it exists
18        if column_name in self.dataframe.columns:
19            self.dataframe[column_name] = pd.to_datetime(self.dataframe[column_name], errors='coerce')
20        else:
21            # If the column_name is not in the DataFrame, fallback to automatic time detection
22            time_columns = [col for col in self.dataframe.columns if 'time' in col.lower() or 'date' in col.lower()]
23            
24            # Convert all detected time columns to datetime, if any
25            for col in time_columns:
26                self.dataframe[col] = pd.to_datetime(self.dataframe[col], errors='coerce')
27            
28            # If any time columns are detected, sort by the first one; otherwise, do nothing
29            if time_columns:
30                column_name = time_columns[0]
31        
32        # Sort by the datetime column (either specified or detected)
33        if column_name in self.dataframe.columns:
34            self.dataframe = self.dataframe.sort_values(by=column_name)
35    
36    def get_dataframe(self) -> pd.DataFrame:
37        """Returns the processed DataFrame."""
38        return self.dataframe
Base(dataframe: pandas.core.frame.DataFrame, column_name: str = 'systime')
 5    def __init__(self, dataframe: pd.DataFrame, column_name: str = 'systime') -> pd.DataFrame:
 6        """
 7        Initializes the Base with a DataFrame, detects time columns, converts them to datetime,
 8        and sorts the DataFrame by the specified column (or the detected time column if applicable).
 9        
10        Args:
11        - dataframe (pd.DataFrame): The DataFrame to be processed.
12        - column_name (str): The column to sort by. Default is 'systime'. If the column is not found
13                             or is not a time column, the class will attempt to detect other time columns.
14        """
15        self.dataframe = dataframe.copy()
16        
17        # Attempt to convert the specified column_name to datetime if it exists
18        if column_name in self.dataframe.columns:
19            self.dataframe[column_name] = pd.to_datetime(self.dataframe[column_name], errors='coerce')
20        else:
21            # If the column_name is not in the DataFrame, fallback to automatic time detection
22            time_columns = [col for col in self.dataframe.columns if 'time' in col.lower() or 'date' in col.lower()]
23            
24            # Convert all detected time columns to datetime, if any
25            for col in time_columns:
26                self.dataframe[col] = pd.to_datetime(self.dataframe[col], errors='coerce')
27            
28            # If any time columns are detected, sort by the first one; otherwise, do nothing
29            if time_columns:
30                column_name = time_columns[0]
31        
32        # Sort by the datetime column (either specified or detected)
33        if column_name in self.dataframe.columns:
34            self.dataframe = self.dataframe.sort_values(by=column_name)

Initializes the Base with a DataFrame, detects time columns, converts them to datetime, and sorts the DataFrame by the specified column (or the detected time column if applicable).

Args:

  • dataframe (pd.DataFrame): The DataFrame to be processed.
  • column_name (str): The column to sort by. Default is 'systime'. If the column is not found or is not a time column, the class will attempt to detect other time columns.
dataframe
def get_dataframe(self) -> pandas.core.frame.DataFrame:
36    def get_dataframe(self) -> pd.DataFrame:
37        """Returns the processed DataFrame."""
38        return self.dataframe

Returns the processed DataFrame.