sqlmesh.utils.pandas
1from datetime import date, datetime 2 3import pandas as pd 4 5 6def filter_df_by_timelike( 7 df: pd.DataFrame, 8 column: str, 9 col_format: str, 10 start: datetime, 11 end: datetime, 12) -> pd.DataFrame: 13 """ 14 Inclusively filters a DataFrame by a column that is a datetime-like type. This is done by converting the DF column 15 to a string and having it match the format of the start and end dates. 16 """ 17 18 start_format = start.strftime(col_format) 19 end_format = end.strftime(col_format) 20 21 df_time = df[column] 22 if len(df_time) == 0: 23 return df 24 elif isinstance(df_time[0], (datetime, date)): 25 df_time = df_time.apply(lambda x: x.strftime(col_format)) # type: ignore 26 elif not pd.api.types.is_string_dtype(df_time.dtype): # type: ignore 27 df_time = df_time.dt.strftime(col_format) 28 return df[df_time.between(start_format, end_format)]
def
filter_df_by_timelike( df: pandas.core.frame.DataFrame, column: str, col_format: str, start: datetime.datetime, end: datetime.datetime) -> pandas.core.frame.DataFrame:
7def filter_df_by_timelike( 8 df: pd.DataFrame, 9 column: str, 10 col_format: str, 11 start: datetime, 12 end: datetime, 13) -> pd.DataFrame: 14 """ 15 Inclusively filters a DataFrame by a column that is a datetime-like type. This is done by converting the DF column 16 to a string and having it match the format of the start and end dates. 17 """ 18 19 start_format = start.strftime(col_format) 20 end_format = end.strftime(col_format) 21 22 df_time = df[column] 23 if len(df_time) == 0: 24 return df 25 elif isinstance(df_time[0], (datetime, date)): 26 df_time = df_time.apply(lambda x: x.strftime(col_format)) # type: ignore 27 elif not pd.api.types.is_string_dtype(df_time.dtype): # type: ignore 28 df_time = df_time.dt.strftime(col_format) 29 return df[df_time.between(start_format, end_format)]
Inclusively filters a DataFrame by a column that is a datetime-like type. This is done by converting the DF column to a string and having it match the format of the start and end dates.