Skip to content

Commit

Permalink
Add precision to Timestamp.link()
Browse files Browse the repository at this point in the history
  • Loading branch information
moeyensj committed Oct 10, 2023
1 parent 7b08228 commit 0cc5764
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
54 changes: 54 additions & 0 deletions adam_core/time/tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,57 @@ def test_Timestamp_link(rescale):
assert left_table.nanos.to_pylist() == [4, 4]
assert right_table.days.to_pylist() == [68020]
assert right_table.nanos.to_pylist() == [4]


def test_Timestamp_link_precision():
# Test that the link method works as expected with user defined precisions
time = Timestamp.from_kwargs(
days=[1, 1, 1, 1, 1, 1, 1, 1],
nanos=[9, 99, 999, 9_999, 99_999, 999_999, 9_999_999, 99_999_999],
scale="tdb",
)

# 1 nano per nano :)
time_ns = Timestamp.from_kwargs(
days=[1, 1, 1, 1, 1, 1, 1, 1],
nanos=[9, 99, 999, 9_999, 99_999, 999_999, 9_999_999, 99_999_999],
scale="tdb",
)
linkage = time.link(time_ns, precision="ns")
assert len(linkage.all_unique_values) == 8

# 1_000 nanos per micro
time_micros = Timestamp.from_kwargs(
days=[1, 1, 1, 1, 1, 1, 1, 1],
nanos=[0, 0, 0, 9_999, 99_999, 999_999, 9_999_999, 99_999_999],
scale="tdb",
)
linkage = time.link(time_micros, precision="us")
assert len(linkage.all_unique_values) == 6

# 1_000_000 nanos per milli
time_millis = Timestamp.from_kwargs(
days=[1, 1, 1, 1, 1, 1, 1, 1],
nanos=[0, 0, 0, 0, 0, 0, 9_999_999, 99_999_999],
scale="tdb",
)
linkage = time.link(time_millis, precision="ms")
assert len(linkage.all_unique_values) == 3

# 1_000_000_000 nanos per second
time_s = Timestamp.from_kwargs(
days=[1, 1, 1, 1, 1, 1, 1, 1],
nanos=[
0,
0,
0,
0,
0,
0,
0,
0,
],
scale="tdb",
)
linkage = time.link(time_s, precision="s")
assert len(linkage.all_unique_values) == 1
22 changes: 11 additions & 11 deletions adam_core/time/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,17 +464,21 @@ def rescale(self, new_scale: str) -> Timestamp:
else:
raise ValueError("Unknown scale: {}".format(new_scale))

def link(self, other: Timestamp) -> qv.MultiKeyLinkage[Timestamp, Timestamp]:
def link(
self, other: Timestamp, precision: str = "ns"
) -> qv.MultiKeyLinkage[Timestamp, Timestamp]:
"""
Link this Timestamp to another. This function assumes an exact integer match
between the days and nanos columns of the two Timestamps.
Link this Timestamp to another. The default precision is nanoseconds, but if
other precisions are desired then both this class and the other Timestamp will
be rounded to the desired precision.
If the timescales are different, the other Timestamp will be rescaled to
this Timestamp's timescale.
Parameters
----------
other : The Timestamp to link to.
precision : The precision to use when linking. The default is 'ns'.
Returns
-------
Expand All @@ -483,15 +487,11 @@ def link(self, other: Timestamp) -> qv.MultiKeyLinkage[Timestamp, Timestamp]:
if self.scale != other.scale:
other = other.rescale(self.scale)

left_keys = {
"days": self.days,
"nanos": self.nanos,
}
right_keys = {
"days": other.days,
"nanos": other.nanos,
}
rounded = self.rounded(precision)
other_rounded = other.rounded(precision)

left_keys = {"days": rounded.days, "nanos": rounded.nanos}
right_keys = {"days": other_rounded.days, "nanos": other_rounded.nanos}
return qv.MultiKeyLinkage(self, other, left_keys, right_keys)


Expand Down

0 comments on commit 0cc5764

Please sign in to comment.