Skip to content

Commit

Permalink
🚑 Fixes union and intersection expressions between time series
Browse files Browse the repository at this point in the history
  • Loading branch information
SynStratos authored and Luca Spartera committed May 4, 2022
1 parent bfa2848 commit 2e486ac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
32 changes: 19 additions & 13 deletions outatime/timeseries/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


def take_first_available(a, b):
if all([a, b]):
if a is not None:
return a
elif b:
elif b is not None:
return b
else:
raise Exception("Both arguments are None.")
Expand All @@ -19,20 +19,23 @@ def intersection(tsl_a: TimeSeries, tsl_b: TimeSeries, conflict_method=take_firs
Args:
tsl_a (TimeSeries): First input time series.
tsl_b (TimeSeries): Second input time series.
conflict_method (None, optional): Method to apply when choosing
data for matching days. Defaults to take_first_available.
Returns:
TimeSeries: Output timeseries with shared days.
"""
intersection_dates = tsl_a.dates and tsl_b.dates
res = []
intersection_dates = list(set(tsl_a.dates) & set(tsl_b.dates))
intersection_dates.sort()
intersection_result = []
for int_date in intersection_dates:
res.append(
intersection_result.append(
TimeSeriesData(
day=int_date,
data=conflict_method(tsl_a.get(day=int_date).data, tsl_b.get(day=int_date).data)
)
)
return TimeSeries(res)
return TimeSeries(intersection_result)


def union(tsl_a: TimeSeries, tsl_b: TimeSeries, conflict_method=take_first_available) -> TimeSeries:
Expand All @@ -43,17 +46,20 @@ def union(tsl_a: TimeSeries, tsl_b: TimeSeries, conflict_method=take_first_avail
Args:
tsl_a (TimeSeries): First input time series.
tsl_b (TimeSeries): Second input time series.
conflict_method (None, optional): Method to apply when choosing
data for matching days. Defaults to take_first_available.
Returns:
TimeSeries: Output timeseries with all days.
"""
union_dates = tsl_a.dates or tsl_b.dates
res = []
for int_date in union_dates:
res.append(
union_dates = list(set(tsl_a.dates) | set(tsl_b.dates))
union_dates.sort()
union_result = []
for uni_date in union_dates:
union_result.append(
TimeSeriesData(
day=int_date,
data=conflict_method(tsl_a.get(day=int_date).data, tsl_b.get(day=int_date).data)
day=uni_date,
data=conflict_method(tsl_a.get(day=uni_date).data, tsl_b.get(day=uni_date).data)
)
)
return TimeSeries(res)
return TimeSeries(union_result)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
long_description = long_description.split("</p>")[1]
long_description = "![](https://github.com/SynStratos/outatime/blob/main/docs/outatime_banner.png)" + long_description
long_description = "# OUTATIME" + long_description

setuptools.setup(
name='outatime',
packages=setuptools.find_packages(),
version='1.0.2',
version='1.0.3',
description='Python framework to manage time series.',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 2e486ac

Please sign in to comment.