Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoscon authored Feb 14, 2017
1 parent a36ab03 commit cf45000
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions arctic/chunkstore/date_chunker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ def to_chunks(self, df, chunk_size='D', **kwargs):
"""
if 'date' in df.index.names:
dates = df.index.get_level_values('date')
if not df.index.is_monotonic_increasing:
df = df.sort_index()
elif 'date' in df.columns:
dates = pd.DatetimeIndex(df.date)
if not dates.is_monotonic_increasing:
df = df.sort(columns='date')
dates = pd.DatetimeIndex(df.date)
else:
raise Exception("Data must be datetime indexed or have a column named 'date'")

Expand Down
30 changes: 29 additions & 1 deletion tests/integration/chunkstore/test_chunkstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,5 +1331,33 @@ def test_chunkstore_misc(chunkstore_lib):

assert("arctic_test.TEST" in str(chunkstore_lib))
assert(str(chunkstore_lib) == repr(chunkstore_lib))


def test_unsorted_index(chunkstore_lib):
df = pd.DataFrame({'date': [dt(2016,9,1), dt(2016,8,31)],
'vals': range(2)}).set_index('date')
df2 = pd.DataFrame({'date': [dt(2016,9,2), dt(2016,9,1)],
'vals': range(2)}).set_index('date')


chunkstore_lib.write('test_symbol', df)
assert_frame_equal(df.sort_index(), chunkstore_lib.read('test_symbol'))
chunkstore_lib.update('test_symbol', df2)
assert_frame_equal(chunkstore_lib.read('test_symbol'),
pd.DataFrame({'date': pd.date_range('2016-8-31',
'2016-9-2'),
'vals': [1,1,0]}).set_index('date'))

def test_unsorted_date_col(chunkstore_lib):
df = pd.DataFrame({'date': [dt(2016,9,1), dt(2016,8,31)],
'vals': range(2)})
df2 = pd.DataFrame({'date': [dt(2016,9,2), dt(2016,9,1)],
'vals': range(2)})

chunkstore_lib.write('test_symbol', df)
assert_frame_equal(df.sort(columns='date').reset_index(drop=True), chunkstore_lib.read('test_symbol'))
chunkstore_lib.update('test_symbol', df2)
assert_frame_equal(chunkstore_lib.read('test_symbol'),
pd.DataFrame({'date': pd.date_range('2016-8-31',
'2016-9-2'),
'vals': [1,1,0]}))

0 comments on commit cf45000

Please sign in to comment.