timeseries_shaper.filter.datetime_filter
1import pandas as pd 2from ..base import Base 3 4class DateTimeFilter(Base): 5 """ 6 Provides class methods for filtering time columns in a pandas DataFrame. 7 Allows specification of which column to operate on. 8 9 Inherits from: 10 Base (class): Base class with common initializations for DataFrame handling. 11 """ 12 13 @classmethod 14 def filter_after_date(cls, dataframe: pd.DataFrame, column_name: str = 'systime', date: str = None) -> pd.DataFrame: 15 """ 16 Filters the DataFrame to include only rows after the specified date. 17 18 Args: 19 - date (str): The cutoff date in 'YYYY-MM-DD' format. 20 21 Returns: 22 - pd.DataFrame: A DataFrame containing rows where the 'systime' is after the specified date. 23 24 Example: 25 -------- 26 >>> filtered_data = DateTimeFilter.filter_after_date(df, "systime", "2023-01-01") 27 >>> print(filtered_data) 28 """ 29 return dataframe[dataframe[column_name] > pd.to_datetime(date)] 30 31 @classmethod 32 def filter_before_date(cls, dataframe: pd.DataFrame, column_name: str = 'systime', date: str = None) -> pd.DataFrame: 33 """ 34 Filters the DataFrame to include only rows before the specified date. 35 36 Args: 37 - date (str): The cutoff date in 'YYYY-MM-DD' format. 38 39 Returns: 40 - pd.DataFrame: A DataFrame containing rows where the 'systime' is before the specified date. 41 42 Example: 43 -------- 44 >>> filtered_data = DateTimeFilter.filter_before_date(df, "systime", "2023-01-01") 45 >>> print(filtered_data) 46 """ 47 return dataframe[dataframe[column_name] < pd.to_datetime(date)] 48 49 @classmethod 50 def filter_between_dates(cls, dataframe: pd.DataFrame, column_name: str = 'systime', start_date: str = None, end_date: str = None) -> pd.DataFrame: 51 """ 52 Filters the DataFrame to include only rows between the specified start and end dates. 53 54 Args: 55 - start_date (str): The start date of the interval in 'YYYY-MM-DD' format. 56 - end_date (str): The end date of the interval in 'YYYY-MM-DD' format. 57 58 Returns: 59 - pd.DataFrame: A DataFrame containing rows where the 'systime' is between the specified dates. 60 61 Example: 62 -------- 63 >>> filtered_data = DateTimeFilter.filter_between_dates(df, "systime", "2023-01-01", "2023-02-01") 64 >>> print(filtered_data) 65 """ 66 mask = (dataframe[column_name] > pd.to_datetime(start_date)) & (dataframe[column_name] < pd.to_datetime(end_date)) 67 return dataframe[mask] 68 69 @classmethod 70 def filter_after_datetime(cls, dataframe: pd.DataFrame, column_name: str = 'systime', datetime: str = None) -> pd.DataFrame: 71 """ 72 Filters the DataFrame to include only rows after the specified datetime. 73 74 Args: 75 - datetime (str): The cutoff datetime in 'YYYY-MM-DD HH:MM:SS' format. 76 77 Returns: 78 - pd.DataFrame: A DataFrame containing rows where the 'systime' is after the specified datetime. 79 80 Example: 81 -------- 82 >>> filtered_data = DateTimeFilter.filter_after_datetime(df, "systime", "2023-01-01 12:00:00") 83 >>> print(filtered_data) 84 """ 85 return dataframe[dataframe[column_name] > pd.to_datetime(datetime)] 86 87 @classmethod 88 def filter_before_datetime(cls, dataframe: pd.DataFrame, column_name: str = 'systime', datetime: str = None) -> pd.DataFrame: 89 """ 90 Filters the DataFrame to include only rows before the specified datetime. 91 92 Args: 93 - datetime (str): The cutoff datetime in 'YYYY-MM-DD HH:MM:SS' format. 94 95 Returns: 96 - pd.DataFrame: A DataFrame containing rows where the 'systime' is before the specified datetime. 97 98 Example: 99 -------- 100 >>> filtered_data = DateTimeFilter.filter_before_datetime(df, "systime", "2023-01-01 12:00:00") 101 >>> print(filtered_data) 102 """ 103 return dataframe[dataframe[column_name] < pd.to_datetime(datetime)] 104 105 @classmethod 106 def filter_between_datetimes(cls, dataframe: pd.DataFrame, column_name: str = 'systime', start_datetime: str = None, end_datetime: str = None) -> pd.DataFrame: 107 """ 108 Filters the DataFrame to include only rows between the specified start and end datetimes. 109 110 Args: 111 - start_datetime (str): The start datetime of the interval in 'YYYY-MM-DD HH:MM:SS' format. 112 - end_datetime (str): The end datetime of the interval in 'YYYY-MM-DD HH:MM:SS' format. 113 114 Returns: 115 - pd.DataFrame: A DataFrame containing rows where the 'systime' is between the specified datetimes. 116 117 Example: 118 -------- 119 >>> filtered_data = DateTimeFilter.filter_between_datetimes(df, "systime", "2023-01-01 12:00:00", "2023-02-01 12:00:00") 120 >>> print(filtered_data) 121 """ 122 mask = (dataframe[column_name] > pd.to_datetime(start_datetime)) & (dataframe[column_name] < pd.to_datetime(end_datetime)) 123 return dataframe[mask]
5class DateTimeFilter(Base): 6 """ 7 Provides class methods for filtering time columns in a pandas DataFrame. 8 Allows specification of which column to operate on. 9 10 Inherits from: 11 Base (class): Base class with common initializations for DataFrame handling. 12 """ 13 14 @classmethod 15 def filter_after_date(cls, dataframe: pd.DataFrame, column_name: str = 'systime', date: str = None) -> pd.DataFrame: 16 """ 17 Filters the DataFrame to include only rows after the specified date. 18 19 Args: 20 - date (str): The cutoff date in 'YYYY-MM-DD' format. 21 22 Returns: 23 - pd.DataFrame: A DataFrame containing rows where the 'systime' is after the specified date. 24 25 Example: 26 -------- 27 >>> filtered_data = DateTimeFilter.filter_after_date(df, "systime", "2023-01-01") 28 >>> print(filtered_data) 29 """ 30 return dataframe[dataframe[column_name] > pd.to_datetime(date)] 31 32 @classmethod 33 def filter_before_date(cls, dataframe: pd.DataFrame, column_name: str = 'systime', date: str = None) -> pd.DataFrame: 34 """ 35 Filters the DataFrame to include only rows before the specified date. 36 37 Args: 38 - date (str): The cutoff date in 'YYYY-MM-DD' format. 39 40 Returns: 41 - pd.DataFrame: A DataFrame containing rows where the 'systime' is before the specified date. 42 43 Example: 44 -------- 45 >>> filtered_data = DateTimeFilter.filter_before_date(df, "systime", "2023-01-01") 46 >>> print(filtered_data) 47 """ 48 return dataframe[dataframe[column_name] < pd.to_datetime(date)] 49 50 @classmethod 51 def filter_between_dates(cls, dataframe: pd.DataFrame, column_name: str = 'systime', start_date: str = None, end_date: str = None) -> pd.DataFrame: 52 """ 53 Filters the DataFrame to include only rows between the specified start and end dates. 54 55 Args: 56 - start_date (str): The start date of the interval in 'YYYY-MM-DD' format. 57 - end_date (str): The end date of the interval in 'YYYY-MM-DD' format. 58 59 Returns: 60 - pd.DataFrame: A DataFrame containing rows where the 'systime' is between the specified dates. 61 62 Example: 63 -------- 64 >>> filtered_data = DateTimeFilter.filter_between_dates(df, "systime", "2023-01-01", "2023-02-01") 65 >>> print(filtered_data) 66 """ 67 mask = (dataframe[column_name] > pd.to_datetime(start_date)) & (dataframe[column_name] < pd.to_datetime(end_date)) 68 return dataframe[mask] 69 70 @classmethod 71 def filter_after_datetime(cls, dataframe: pd.DataFrame, column_name: str = 'systime', datetime: str = None) -> pd.DataFrame: 72 """ 73 Filters the DataFrame to include only rows after the specified datetime. 74 75 Args: 76 - datetime (str): The cutoff datetime in 'YYYY-MM-DD HH:MM:SS' format. 77 78 Returns: 79 - pd.DataFrame: A DataFrame containing rows where the 'systime' is after the specified datetime. 80 81 Example: 82 -------- 83 >>> filtered_data = DateTimeFilter.filter_after_datetime(df, "systime", "2023-01-01 12:00:00") 84 >>> print(filtered_data) 85 """ 86 return dataframe[dataframe[column_name] > pd.to_datetime(datetime)] 87 88 @classmethod 89 def filter_before_datetime(cls, dataframe: pd.DataFrame, column_name: str = 'systime', datetime: str = None) -> pd.DataFrame: 90 """ 91 Filters the DataFrame to include only rows before the specified datetime. 92 93 Args: 94 - datetime (str): The cutoff datetime in 'YYYY-MM-DD HH:MM:SS' format. 95 96 Returns: 97 - pd.DataFrame: A DataFrame containing rows where the 'systime' is before the specified datetime. 98 99 Example: 100 -------- 101 >>> filtered_data = DateTimeFilter.filter_before_datetime(df, "systime", "2023-01-01 12:00:00") 102 >>> print(filtered_data) 103 """ 104 return dataframe[dataframe[column_name] < pd.to_datetime(datetime)] 105 106 @classmethod 107 def filter_between_datetimes(cls, dataframe: pd.DataFrame, column_name: str = 'systime', start_datetime: str = None, end_datetime: str = None) -> pd.DataFrame: 108 """ 109 Filters the DataFrame to include only rows between the specified start and end datetimes. 110 111 Args: 112 - start_datetime (str): The start datetime of the interval in 'YYYY-MM-DD HH:MM:SS' format. 113 - end_datetime (str): The end datetime of the interval in 'YYYY-MM-DD HH:MM:SS' format. 114 115 Returns: 116 - pd.DataFrame: A DataFrame containing rows where the 'systime' is between the specified datetimes. 117 118 Example: 119 -------- 120 >>> filtered_data = DateTimeFilter.filter_between_datetimes(df, "systime", "2023-01-01 12:00:00", "2023-02-01 12:00:00") 121 >>> print(filtered_data) 122 """ 123 mask = (dataframe[column_name] > pd.to_datetime(start_datetime)) & (dataframe[column_name] < pd.to_datetime(end_datetime)) 124 return dataframe[mask]
Provides class methods for filtering time columns in a pandas DataFrame. Allows specification of which column to operate on.
Inherits from: Base (class): Base class with common initializations for DataFrame handling.
14 @classmethod 15 def filter_after_date(cls, dataframe: pd.DataFrame, column_name: str = 'systime', date: str = None) -> pd.DataFrame: 16 """ 17 Filters the DataFrame to include only rows after the specified date. 18 19 Args: 20 - date (str): The cutoff date in 'YYYY-MM-DD' format. 21 22 Returns: 23 - pd.DataFrame: A DataFrame containing rows where the 'systime' is after the specified date. 24 25 Example: 26 -------- 27 >>> filtered_data = DateTimeFilter.filter_after_date(df, "systime", "2023-01-01") 28 >>> print(filtered_data) 29 """ 30 return dataframe[dataframe[column_name] > pd.to_datetime(date)]
Filters the DataFrame to include only rows after the specified date.
Args:
- date (str): The cutoff date in 'YYYY-MM-DD' format.
Returns:
- pd.DataFrame: A DataFrame containing rows where the 'systime' is after the specified date.
Example:
>>> filtered_data = DateTimeFilter.filter_after_date(df, "systime", "2023-01-01")
>>> print(filtered_data)
32 @classmethod 33 def filter_before_date(cls, dataframe: pd.DataFrame, column_name: str = 'systime', date: str = None) -> pd.DataFrame: 34 """ 35 Filters the DataFrame to include only rows before the specified date. 36 37 Args: 38 - date (str): The cutoff date in 'YYYY-MM-DD' format. 39 40 Returns: 41 - pd.DataFrame: A DataFrame containing rows where the 'systime' is before the specified date. 42 43 Example: 44 -------- 45 >>> filtered_data = DateTimeFilter.filter_before_date(df, "systime", "2023-01-01") 46 >>> print(filtered_data) 47 """ 48 return dataframe[dataframe[column_name] < pd.to_datetime(date)]
Filters the DataFrame to include only rows before the specified date.
Args:
- date (str): The cutoff date in 'YYYY-MM-DD' format.
Returns:
- pd.DataFrame: A DataFrame containing rows where the 'systime' is before the specified date.
Example:
>>> filtered_data = DateTimeFilter.filter_before_date(df, "systime", "2023-01-01")
>>> print(filtered_data)
50 @classmethod 51 def filter_between_dates(cls, dataframe: pd.DataFrame, column_name: str = 'systime', start_date: str = None, end_date: str = None) -> pd.DataFrame: 52 """ 53 Filters the DataFrame to include only rows between the specified start and end dates. 54 55 Args: 56 - start_date (str): The start date of the interval in 'YYYY-MM-DD' format. 57 - end_date (str): The end date of the interval in 'YYYY-MM-DD' format. 58 59 Returns: 60 - pd.DataFrame: A DataFrame containing rows where the 'systime' is between the specified dates. 61 62 Example: 63 -------- 64 >>> filtered_data = DateTimeFilter.filter_between_dates(df, "systime", "2023-01-01", "2023-02-01") 65 >>> print(filtered_data) 66 """ 67 mask = (dataframe[column_name] > pd.to_datetime(start_date)) & (dataframe[column_name] < pd.to_datetime(end_date)) 68 return dataframe[mask]
Filters the DataFrame to include only rows between the specified start and end dates.
Args:
- start_date (str): The start date of the interval in 'YYYY-MM-DD' format.
- end_date (str): The end date of the interval in 'YYYY-MM-DD' format.
Returns:
- pd.DataFrame: A DataFrame containing rows where the 'systime' is between the specified dates.
Example:
>>> filtered_data = DateTimeFilter.filter_between_dates(df, "systime", "2023-01-01", "2023-02-01")
>>> print(filtered_data)
70 @classmethod 71 def filter_after_datetime(cls, dataframe: pd.DataFrame, column_name: str = 'systime', datetime: str = None) -> pd.DataFrame: 72 """ 73 Filters the DataFrame to include only rows after the specified datetime. 74 75 Args: 76 - datetime (str): The cutoff datetime in 'YYYY-MM-DD HH:MM:SS' format. 77 78 Returns: 79 - pd.DataFrame: A DataFrame containing rows where the 'systime' is after the specified datetime. 80 81 Example: 82 -------- 83 >>> filtered_data = DateTimeFilter.filter_after_datetime(df, "systime", "2023-01-01 12:00:00") 84 >>> print(filtered_data) 85 """ 86 return dataframe[dataframe[column_name] > pd.to_datetime(datetime)]
Filters the DataFrame to include only rows after the specified datetime.
Args:
- datetime (str): The cutoff datetime in 'YYYY-MM-DD HH:MM:SS' format.
Returns:
- pd.DataFrame: A DataFrame containing rows where the 'systime' is after the specified datetime.
Example:
>>> filtered_data = DateTimeFilter.filter_after_datetime(df, "systime", "2023-01-01 12:00:00")
>>> print(filtered_data)
88 @classmethod 89 def filter_before_datetime(cls, dataframe: pd.DataFrame, column_name: str = 'systime', datetime: str = None) -> pd.DataFrame: 90 """ 91 Filters the DataFrame to include only rows before the specified datetime. 92 93 Args: 94 - datetime (str): The cutoff datetime in 'YYYY-MM-DD HH:MM:SS' format. 95 96 Returns: 97 - pd.DataFrame: A DataFrame containing rows where the 'systime' is before the specified datetime. 98 99 Example: 100 -------- 101 >>> filtered_data = DateTimeFilter.filter_before_datetime(df, "systime", "2023-01-01 12:00:00") 102 >>> print(filtered_data) 103 """ 104 return dataframe[dataframe[column_name] < pd.to_datetime(datetime)]
Filters the DataFrame to include only rows before the specified datetime.
Args:
- datetime (str): The cutoff datetime in 'YYYY-MM-DD HH:MM:SS' format.
Returns:
- pd.DataFrame: A DataFrame containing rows where the 'systime' is before the specified datetime.
Example:
>>> filtered_data = DateTimeFilter.filter_before_datetime(df, "systime", "2023-01-01 12:00:00")
>>> print(filtered_data)
106 @classmethod 107 def filter_between_datetimes(cls, dataframe: pd.DataFrame, column_name: str = 'systime', start_datetime: str = None, end_datetime: str = None) -> pd.DataFrame: 108 """ 109 Filters the DataFrame to include only rows between the specified start and end datetimes. 110 111 Args: 112 - start_datetime (str): The start datetime of the interval in 'YYYY-MM-DD HH:MM:SS' format. 113 - end_datetime (str): The end datetime of the interval in 'YYYY-MM-DD HH:MM:SS' format. 114 115 Returns: 116 - pd.DataFrame: A DataFrame containing rows where the 'systime' is between the specified datetimes. 117 118 Example: 119 -------- 120 >>> filtered_data = DateTimeFilter.filter_between_datetimes(df, "systime", "2023-01-01 12:00:00", "2023-02-01 12:00:00") 121 >>> print(filtered_data) 122 """ 123 mask = (dataframe[column_name] > pd.to_datetime(start_datetime)) & (dataframe[column_name] < pd.to_datetime(end_datetime)) 124 return dataframe[mask]
Filters the DataFrame to include only rows between the specified start and end datetimes.
Args:
- start_datetime (str): The start datetime of the interval in 'YYYY-MM-DD HH:MM:SS' format.
- end_datetime (str): The end datetime of the interval in 'YYYY-MM-DD HH:MM:SS' format.
Returns:
- pd.DataFrame: A DataFrame containing rows where the 'systime' is between the specified datetimes.
Example:
>>> filtered_data = DateTimeFilter.filter_between_datetimes(df, "systime", "2023-01-01 12:00:00", "2023-02-01 12:00:00")
>>> print(filtered_data)
Inherited Members
- timeseries_shaper.base.Base
- Base
- dataframe
- get_dataframe