Skip to content

Commit

Permalink
ENH: Add min date function (pandas-dev#478)
Browse files Browse the repository at this point in the history
* ENH: Add min date function

Add min date function to match max date function

* TST: Add test for min and max dates

Add test for min and max dates

* ENH: Improve error for min/max date

Raise NoDataFoundException rather than TypeError when no result
is returned.
  • Loading branch information
bashtage authored and bmoscon committed Jan 6, 2018
1 parent 70540dc commit 678a047
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
17 changes: 17 additions & 0 deletions arctic/tickstore/tickstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,4 +754,21 @@ def max_date(self, symbol):
"""
res = self._collection.find_one({SYMBOL: symbol}, projection={ID: 0, END: 1},
sort=[(START, pymongo.DESCENDING)])
if res is None:
raise NoDataFoundException("No Data found for {}".format(symbol))
return utc_dt_to_local_dt(res[END])

def min_date(self, symbol):
"""
Return the minimum datetime stored for a particular symbol
Parameters
----------
symbol : `str`
symbol name for the item
"""
res = self._collection.find_one({SYMBOL: symbol}, projection={ID: 0, START: 1},
sort=[(START, pymongo.ASCENDING)])
if res is None:
raise NoDataFoundException("No Data found for {}".format(symbol))
return utc_dt_to_local_dt(res[START])
26 changes: 26 additions & 0 deletions tests/integration/tickstore/test_toplevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,29 @@ def test_should_write_top_level_with_correct_timezone(arctic):
lib2010 = arctic['FEED_2010.LEVEL1']
# Check that only one point was written into 2010 being 3am on 31st
assert len(lib2010.read('blah', DateRange(start=dt(2010, 12, 1), end=dt(2011, 1, 1)))) == 1


def test_min_max_date(arctic):
arctic.initialize_library('FEED_2010.LEVEL1', tickstore.TICK_STORE_TYPE)
tstore = arctic['FEED_2010.LEVEL1']
dates = pd.date_range('20100101', periods=6, tz=mktz('Europe/London'))
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
tstore.write('blah', df)

min_date = tstore.min_date('blah')
max_date = tstore.max_date('blah')
assert min_date == dates[0].to_pydatetime()
assert max_date == dates[-1].to_pydatetime()


def test_no_min_max_date(arctic):
arctic.initialize_library('FEED_2010.LEVEL1', tickstore.TICK_STORE_TYPE)
tstore = arctic['FEED_2010.LEVEL1']
dates = pd.date_range('20100101', periods=6, tz=mktz('Europe/London'))
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
tstore.write('blah', df)

with pytest.raises(NoDataFoundException):
tstore.min_date('unknown-symbol')
with pytest.raises(NoDataFoundException):
tstore.max_date('unknown-symbol')

0 comments on commit 678a047

Please sign in to comment.