Skip to content

Commit c7402a3

Browse files
authored
Fix issue pandas-dev#283
1 parent 3c424d6 commit c7402a3

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

CHANGES.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Changelog
22

3+
### 1.34
4+
5+
* Feature: #283 Support for all pandas frequency strings in ChunkStore DateChunker
6+
37
### 1.33 (2016-11-07)
48

59
* Feature: #275 Tuple range object support in DateChunker

arctic/chunkstore/date_chunker.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ def to_chunks(self, df, chunk_size='D', **kwargs):
1111
"""
1212
chunks the dataframe/series by dates
1313
14-
returns
14+
Parameters
15+
----------
16+
df: pandas dataframe or series
17+
chunk_size: str
18+
any valid Pandas frequency string
19+
20+
Returns
1521
-------
1622
generator that produces tuples: (start date, end date,
1723
chunk_size, dataframe/series)
1824
"""
19-
if chunk_size not in ('D', 'M', 'Y', 'A'):
20-
raise Exception("Chunk size must be one of D, M, Y, A")
21-
22-
if chunk_size == 'Y':
23-
chunk_size = 'A'
24-
2525
if 'date' in df.index.names:
2626
dates = df.index.get_level_values('date')
2727
elif 'date' in df.columns:

tests/integration/chunkstore/test_chunkstore.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def test_yearly_df(chunkstore_lib):
258258
name='date'),
259259
columns=['data'])
260260

261-
chunkstore_lib.write('chunkstore_test', df, chunk_size='Y')
261+
chunkstore_lib.write('chunkstore_test', df, chunk_size='A')
262262
ret = chunkstore_lib.read('chunkstore_test', chunk_range=DateRange(dt(2016, 1, 1), dt(2016, 3, 3)))
263263
assert_frame_equal(df, ret)
264264

@@ -1172,3 +1172,18 @@ def test_unnamed_colums(chunkstore_lib):
11721172
with pytest.raises(Exception) as e:
11731173
chunkstore_lib.write('test_df', df, chunk_size='D')
11741174
assert('must be named' in str(e))
1175+
1176+
1177+
def test_quarterly_data(chunkstore_lib):
1178+
df = DataFrame(data={'data': np.random.randint(0, 100, size=366)},
1179+
index=pd.date_range('2016-01-01', '2016-12-31'))
1180+
df.index.name = 'date'
1181+
1182+
chunkstore_lib.write('quarterly', df, chunk_size='Q')
1183+
assert_frame_equal(df, chunkstore_lib.read('quarterly'))
1184+
assert(len(chunkstore_lib.read('quarterly', chunk_range=(None, '2016-01-05'))) == 5)
1185+
count = 0
1186+
for _ in chunkstore_lib._collection.find({SYMBOL: 'quarterly'}, sort=[(START, pymongo.ASCENDING)],):
1187+
count += 1
1188+
1189+
assert(count == 4)

tests/unit/chunkstore/test_date_chunker.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ def test_date_filter_with_pd_date_range():
7474
def test_to_chunks_exceptions():
7575
df = DataFrame(data={'data': [1, 2, 3]})
7676
c = DateChunker()
77-
with pytest.raises(Exception) as e:
78-
six.next(c.to_chunks(None, None))
79-
assert('Chunk size' in str(e))
8077

8178
with pytest.raises(Exception) as e:
8279
six.next(c.to_chunks(df, 'D'))
8380
assert('datetime indexed' in str(e))
8481

82+
df.columns = ['date']
83+
with pytest.raises(Exception) as e:
84+
six.next(c.to_chunks(df, 'ZSDFG'))
85+
assert('Unknown freqstr' in str(e) or 'Invalid frequency' in str(e))
86+
8587

8688
def test_exclude():
8789
c = DateChunker()

0 commit comments

Comments
 (0)