Skip to content

Commit

Permalink
Fix #16: Ensure that irr data doesn't lose datetime precision
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew MacDonald <andrew@maccas.net>
  • Loading branch information
amacd31 committed Aug 8, 2017
1 parent b1f23cf commit 719246b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions phildb/writer.py
Expand Up @@ -238,6 +238,9 @@ def write_irregular_data(tsdb_file, series):
"""
existing = __read(tsdb_file)

if series.dtype == np.float32:
series = series.astype(np.float64)

overlap_idx = existing.index.intersection(series.index)
modified = series.ix[overlap_idx] != existing.value.ix[overlap_idx]
records_to_modify = existing.loc[overlap_idx].ix[modified.values]
Expand Down
25 changes: 25 additions & 0 deletions tests/test_writer.py
Expand Up @@ -548,3 +548,28 @@ def test_empty_series_write(self):
log_entries = writer.write(self.tsdb_existing_file, pd.Series([]), 'D')
self.assertEqual(0, len(log_entries['C']))
self.assertEqual(0, len(log_entries['U']))

def test_float32_irregular_write(self):
"""
Test irregular write of float32 data.
See: https://github.com/amacd31/phildb/issues/16
"""

sample = pd.Series(
pd.np.array(
[x + 0.1 for x in range(10)],
dtype=pd.np.float32
),
index=pd.date_range(
'2017-08-06 06:50:00+00:00',
periods=10,
freq='1T'
)
)

log_entries = writer.write(self.tsdb_existing_file, sample, 'IRR')
data = reader.read(self.tsdb_existing_file)

self.assertEqual(datetime(2017,8,6,6,50,0,0), data.index[0].to_pydatetime())
self.assertEqual(datetime(2017,8,6,6,51,0,0), data.index[1].to_pydatetime())

0 comments on commit 719246b

Please sign in to comment.