Skip to content

Commit 43840f7

Browse files
authored
Fix issue 216 (pandas-dev#321)
Addresses issue 216 by allowing index column to be named in tickstore
1 parent c0fc4d0 commit 43840f7

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Bugfix: #300 to_datetime deprecated in pandas, use to_pydatetime instead
55
* Bugfix: #309 formatting change for DateRange ```__str__```
66
* Feature: #313 set and read user specified metadata in chunkstore
7+
* Bugfix: #216 Tickstore write fails with named index column
78

89
### 1.36 (2016-12-13)
910

arctic/tickstore/tickstore.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ def write(self, symbol, data, initial_image=None):
509509
data : list of dicts or a pandas.DataFrame
510510
List of ticks to store to the tick-store.
511511
if a list of dicts, each dict must contain a 'index' datetime
512-
if a pandas.DataFrame the index must be a Timestamp that can be converted to a datetime
512+
if a pandas.DataFrame the index must be a Timestamp that can be converted to a datetime.
513+
Index names will not be preserved.
513514
initial_image : dict
514515
Dict of the initial image at the start of the document. If this contains a 'index' entry it is
515516
assumed to be the time of the timestamp of the index
@@ -628,6 +629,7 @@ def _pandas_to_bucket(df, symbol, initial_image):
628629
logger.warning("NB treating all values as 'exists' - no longer sparse")
629630
rowmask = Binary(lz4.compressHC(np.packbits(np.ones(len(df), dtype='uint8'))))
630631

632+
index_name = df.index.names[0] or "index"
631633
recs = df.to_records(convert_datetime64=False)
632634
for col in df:
633635
array = TickStore._ensure_supported_dtypes(recs[col])
@@ -636,9 +638,8 @@ def _pandas_to_bucket(df, symbol, initial_image):
636638
col_data[ROWMASK] = rowmask
637639
col_data[DTYPE] = TickStore._str_dtype(array.dtype)
638640
rtn[COLUMNS][col] = col_data
639-
rtn[INDEX] = Binary(lz4.compressHC(np.concatenate(([recs['index'][0].astype('datetime64[ms]').view('uint64')],
640-
np.diff(recs['index'].astype('datetime64[ms]').view('uint64')))
641-
).tostring()))
641+
rtn[INDEX] = Binary(lz4.compressHC(np.concatenate(([recs[index_name][0].astype('datetime64[ms]').view('uint64')],
642+
np.diff(recs[index_name].astype('datetime64[ms]').view('uint64')))).tostring()))
642643
return rtn, final_image
643644

644645
@staticmethod

tests/integration/tickstore/test_ts_write.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,20 @@ def test_ts_write_pandas(tickstore_lib):
7979

8080
read = tickstore_lib.read('SYM', columns=None)
8181
assert_frame_equal(read, data, check_names=False)
82+
83+
84+
def test_ts_write_named_col(tickstore_lib):
85+
data = DUMMY_DATA
86+
tickstore_lib.write('SYM', data)
87+
88+
data = tickstore_lib.read('SYM')
89+
assert data.index[0] == dt(2013, 1, 1, tzinfo=mktz('Europe/London'))
90+
assert data.a[0] == 1
91+
assert(data.index.name is None)
92+
data.index.name = 'IndexName'
93+
94+
tickstore_lib.delete('SYM')
95+
tickstore_lib.write('SYM', data)
96+
97+
read = tickstore_lib.read('SYM')
98+
assert(read.index.name is None)

0 commit comments

Comments
 (0)