Skip to content

Commit 32a9a77

Browse files
author
Adrian Teng
committed
refactor last_update to support mult-index
1 parent aea66e5 commit 32a9a77

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

arctic/store/bitemporal_store.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
class BitemporalStore(object):
13-
""" A versioned pandas DataFrame store. (currently only supports single index df)
13+
""" A versioned pandas DataFrame store.
1414
1515
As the name hinted, this holds versions of DataFrame by maintaining an extra 'insert time' index internally.
1616
"""
@@ -22,8 +22,8 @@ def __init__(self, version_store, observe_column='observed_dt'):
2222
version_store : `VersionStore`
2323
The version store that keeps the underlying data frames
2424
observe_column : `str`
25-
Column name for the datetime index that represents the insertion time of a row of data. This column is
26-
internal to this store.
25+
Column name for the datetime index that represents the insertion time of a row of data. Unless you intent to
26+
read raw data out, this column is internal to this store.
2727
"""
2828
self._store = version_store
2929
self.observe_column = observe_column
@@ -48,16 +48,17 @@ def read(self, symbol, as_of=None, raw=False, **kwargs):
4848
BitemporalItem namedtuple which contains a .data and .metadata element
4949
"""
5050
item = self._store.read(symbol, **kwargs)
51+
last_updated = max(item.data.index.get_level_values(self.observe_column))
5152
if raw:
5253
return BitemporalItem(symbol=symbol, library=self._store._arctic_lib.get_name(), data=item.data,
5354
metadata=item.metadata,
54-
last_updated=max(item.data.index, key=lambda x: x[1]))
55+
last_updated=last_updated)
5556
else:
5657
index_names = list(item.data.index.names)
5758
index_names.remove(self.observe_column)
5859
return BitemporalItem(symbol=symbol, library=self._store._arctic_lib.get_name(),
5960
data=groupby_asof(item.data, as_of=as_of, dt_col=index_names, asof_col=self.observe_column),
60-
metadata=item.metadata, last_updated=max(item.data.index, key=lambda x: x[1]))
61+
metadata=item.metadata, last_updated=last_updated)
6162

6263
def update(self, symbol, data, metadata=None, upsert=True, as_of=None, **kwargs):
6364
""" Append 'data' under the specified 'symbol' name to this library.

tests/integration/store/test_bitemporal_store.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ def test_read_ts_raw(bitemporal_library):
4444
2012-11-08 17:06:11.040 | 2015-05-01 | 3.0""", num_index=2))
4545

4646

47+
def test_last_update(bitemporal_library):
48+
bitemporal_library.update('spam', ts1, as_of=dt(2015, 1, 1))
49+
bitemporal_library.update('spam', ts1, as_of=dt(2015, 1, 2))
50+
assert bitemporal_library.read('spam').last_updated == dt(2015, 1, 2)
51+
52+
4753
def test_existing_ts_update_and_read(bitemporal_library):
4854
bitemporal_library.update('spam', ts1)
4955
bitemporal_library.update('spam', ts1_update[-1:])
@@ -245,6 +251,7 @@ def test_multi_index_update(bitemporal_library):
245251
2012-11-08 17:06:11.040 | SPAM Index | 3.0
246252
2012-12-08 17:06:11.040 | EGG Index |
247253
2012-12-08 17:06:11.040 | SPAM Index | 4.0""", num_index=2)
248-
bitemporal_library.update('spam', ts)
249-
bitemporal_library.update('spam', ts2)
254+
bitemporal_library.update('spam', ts, as_of=dt(2015, 1, 1))
255+
bitemporal_library.update('spam', ts2, as_of=dt(2015, 1, 2))
250256
assert_frame_equal(expected_ts, bitemporal_library.read('spam').data)
257+
assert bitemporal_library.read('spam').last_updated == dt(2015, 1, 2)

0 commit comments

Comments
 (0)