Skip to content

Commit

Permalink
adjust date parsing for tz aware and naive inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
danieltsoukup committed Nov 6, 2024
1 parent d56be55 commit 0bf8946
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
3 changes: 1 addition & 2 deletions app/src/data_loading/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ def _get_active_time_limit(self) -> datetime:
"""
Get the current threshold for marking sensor as active.
"""
limit = pd.Timestamp("now") + pd.Timedelta(-4, unit="H")
limit += pd.Timedelta(-1, unit="H")
limit = pd.Timestamp("now", tz='EST') + pd.Timedelta(-2, unit="H")

return limit

Expand Down
23 changes: 18 additions & 5 deletions app/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ def _enum_col_names_to_string(df: pd.DataFrame) -> pd.DataFrame:

return new_df

@staticmethod
def _set_data_types(df: pd.DataFrame) -> pd.DataFrame:
def _set_data_types(self, df: pd.DataFrame) -> pd.DataFrame:
"""
Sets the right data types for noise data columns in place.
"""
Expand All @@ -251,13 +250,27 @@ def _set_data_types(df: pd.DataFrame) -> pd.DataFrame:
if col in df.columns:
df[col] = df[col].astype(type_)

date_cols = [COLUMN.TIMESTAMP, COLUMN.LATEST_TIMESTAMP]
for col in date_cols:
tz_aware_date_cols = [COLUMN.TIMESTAMP, COLUMN.LATEST_TIMESTAMP]
for col in tz_aware_date_cols:
if col in df.columns:
df[col] = self._convert_tz_aware_to_est(df[col])

tz_naive_date_cols = [COLUMN.START, COLUMN.END]
for col in tz_naive_date_cols:
if col in df.columns:
df[col] = pd.to_datetime(df[col]).dt.tz_localize(None)
print(df[col])
df[col] = self._convert_tz_naive_to_est(df[col])

return df

@staticmethod
def _convert_tz_naive_to_est(datetime_col: pd.Series) -> pd.Series:
return pd.to_datetime(datetime_col).dt.tz_localize('EST')

@staticmethod
def _convert_tz_aware_to_est(datetime_col: pd.Series) -> pd.Series:
return pd.to_datetime(datetime_col).dt.tz_convert('EST')

@staticmethod
def _raw_to_dataframe(raw_data: List[Dict[str, Any]]) -> pd.DataFrame:
"""
Expand Down
12 changes: 12 additions & 0 deletions app/tests/test_data_processing.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import pandas as pd
from src.utils import DataFormatter, COLUMN, date_to_string
from src.data_loading.main import AppDataManager
import pytest
from datetime import datetime, date

def test_time_comparison():
data_manager = AppDataManager()
limit = data_manager._get_active_time_limit()

column = pd.Series(["2024-05-18T11:00:00-04:00"])

data_formatter = DataFormatter()
column = data_formatter._convert_tz_aware_to_est(column)

assert (column < limit).all()


def date_to_string():
"""
Expand Down
6 changes: 5 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,8 @@ debug:
#############

count:
wc -l **/*.py makefile Dockerfile README.md
wc -l **/*.py makefile Dockerfile README.md

remove_branch:
git push -d origin $(BRANCH)
git branch -d $(BRANCH)

0 comments on commit 0bf8946

Please sign in to comment.