Skip to content

Commit

Permalink
patch datefiltering fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ktehranchi committed Sep 12, 2024
1 parent aecd454 commit 311b29f
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/r2x/parser/parser_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ def pl_filter_year(df, year: int | None = None, year_columns=["t", "year"], **kw

def filter_property_dates(system_data: pl.DataFrame, study_year: int):
"""filters query by date_from and date_to"""
# Remove Property by study year & date_from/to
study_year_date = datetime(study_year, 1, 1)
date_filter = ((pl.col("date_from").is_null()) | (pl.col("date_from") < study_year_date)) & (
(pl.col("date_to").is_null()) | (pl.col("date_to") > study_year_date)
)

# Convert date_from and date_to to datetime
system_data = system_data.with_columns(
[
Expand All @@ -39,6 +33,18 @@ def filter_property_dates(system_data: pl.DataFrame, study_year: int):
]
)

# Create two new columns year_from and year_to
system_data = system_data.with_columns(
[
pl.col("date_from").dt.year().alias("year_from"),
pl.col("date_to").dt.year().alias("year_to"),
]
)

# Remove Property by study year & date_from/to
date_filter = ((pl.col("date_from").is_null()) | (pl.col("year_from") <= study_year)) & (
(pl.col("date_to").is_null()) | (pl.col("year_to") >= study_year)
)
system_data = system_data.filter(date_filter)
return system_data

Expand Down

0 comments on commit 311b29f

Please sign in to comment.