Skip to content

Commit

Permalink
Fix issue pandas-dev#275
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoscon authored Nov 4, 2016
1 parent 5794407 commit d794bcb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### 1.33

* Feature: #275 Tuple range object support in DateChunker
* Bugfix: #273 Duplicate columns breaking serializer
* Feature: #267 Tickstore.delete returns deleted data
* Dependency: #266 Remove pytest-dbfixtures in favor of pytest-server-fixtures

Expand Down
12 changes: 6 additions & 6 deletions arctic/chunkstore/date_chunker.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def to_mongo(self, range_obj):
-------
dict
"""
if isinstance(range_obj, pd.DatetimeIndex):
range_obj = DateRange(range_obj.min(), range_obj.max())
if isinstance(range_obj, (pd.DatetimeIndex, tuple)):
range_obj = DateRange(range_obj[0], range_obj[-1])
if range_obj.start and range_obj.end:
return {'$and': [{START: {'$lte': range_obj.end}}, {END: {'$gte': range_obj.start}}]}
elif range_obj.start:
Expand All @@ -95,8 +95,8 @@ def filter(self, data, range_obj):
-------
data, filtered by range_obj
"""
if isinstance(range_obj, pd.DatetimeIndex):
range_obj = DateRange(range_obj.min(), range_obj.max())
if isinstance(range_obj, (pd.DatetimeIndex, tuple)):
range_obj = DateRange(range_obj[0], range_obj[-1])
if 'date' in data.index.names:
return data[range_obj.start:range_obj.end]
elif 'date' in data.columns:
Expand All @@ -119,8 +119,8 @@ def exclude(self, data, range_obj):
-------
data, filtered by range_obj
"""
if isinstance(range_obj, pd.DatetimeIndex):
range_obj = DateRange(range_obj.min(), range_obj.max())
if isinstance(range_obj, (pd.DatetimeIndex, tuple)):
range_obj = DateRange(range_obj[0], range_obj[-1])
if 'date' in data.index.names:
return data[(data.index.get_level_values('date') < range_obj.start) | (data.index.get_level_values('date') > range_obj.end)]
elif 'date' in data.columns:
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/chunkstore/test_date_chunker.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,28 @@ def test_exclude_no_index():

assert(c.exclude(df, DateRange(dt(2016, 1, 1), dt(2016, 1, 1))).equals(c.exclude(df, pd.date_range(dt(2016, 1, 1), dt(2016, 1, 1)))))
assert(c.exclude(df2, None).equals(df2))


def test_with_tuples():
c = DateChunker()
df = DataFrame(data={'data': [1, 2, 3],
'date': [dt(2016, 1, 1),
dt(2016, 1, 2),
dt(2016, 1, 3)]
}
)

# OPEN - CLOSED
assert_frame_equal(c.filter(df, (None, dt(2016, 1, 3))), df)
# CLOSED - OPEN
assert_frame_equal(c.filter(df, (dt(2016, 1, 1), None)), df)
# OPEN - OPEN
assert_frame_equal(c.filter(df, (None, None)), df)
# CLOSED - OPEN (far before data range)
assert_frame_equal(c.filter(df, (dt(2000, 1, 1), None)), df)
# CLOSED - OPEN (far after range)
assert(c.filter(df, (dt(2020, 1, 2), None)).empty)
# OPEN - CLOSED
assert_frame_equal(c.filter(df, (None, dt(2020, 1, 1))), df)
# CLOSED - CLOSED (after range)
assert(c.filter(df, (dt(2017, 1, 1), dt(2018, 1, 1))).empty)

0 comments on commit d794bcb

Please sign in to comment.