import pandas as pd from util.setup_logging import get_logger logger = get_logger(__name__) from util.data_prepare import COL_DATE, is_mixed_timezones def parse_timestamp(df, timestamp_format, col_timestamp=COL_DATE): is_mix_tz = is_mixed_timezones(df[col_timestamp]) try: df[col_timestamp] = pd.to_datetime(df[col_timestamp], format=timestamp_format, utc=is_mix_tz) return df except: try: df[col_timestamp] = pd.to_datetime(df[col_timestamp], unit='s', utc=is_mix_tz) return df except: try: df[col_timestamp] = pd.to_datetime(df[col_timestamp], infer_datetime_format=True, utc=is_mix_tz) logger.debug(f'Since the timestamp format is neither the ITSI format, nor the Unix epoch format, we infer the timestamp format (example: {df[col_timestamp].iloc[0]}).') return df except: logger.warning(f'The timestamp format "{timestamp_format}" does not match the timestamp format of your data. \ Please ensure your data has a timestamp format supported by ITSI, or check that the timestamp format passed to the "timestamp_format" argument is correct.') raise ValueError(f'The timestamp format "{timestamp_format}" does not match the timestamp format of your data. \ Please ensure your data has a timestamp format supported by ITSI, or check that the timestamp format passed to the "timestamp_format" argument is correct.')