Skip to content

Commit

Permalink
Merge pull request #50 from martindurant/bytestring_decimals
Browse files Browse the repository at this point in the history
Add conversion for decimals stored as bytestrings
  • Loading branch information
martindurant committed Dec 19, 2016
2 parents 5a94f08 + 6fc3e62 commit 95dd015
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion fastparquet/converted_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ def convert(data, se):
return out
if ctype == parquet_thrift.ConvertedType.DECIMAL:
scale_factor = 10**-se.scale
return data * scale_factor
if data.dtype.kind in ['i', 'f']:
return data * scale_factor
else: # byte-string
# NB: general but slow method
# could optimize when data.dtype.itemsize <= 8
# NB: `from_bytes` may be py>=3.4 only
return np.array([int.from_bytes(d, byteorder='big', signed=True) *
scale_factor for d in data])
elif ctype == parquet_thrift.ConvertedType.DATE:
return (data * DAYS_TO_MILLIS).view('datetime64[ns]')
elif ctype == parquet_thrift.ConvertedType.TIME_MILLIS:
Expand Down
19 changes: 19 additions & 0 deletions fastparquet/test/test_converted_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,22 @@ def test_uint64():
converted_type=pt.ConvertedType.UINT_64
)
assert convert(pd.Series([-6884376]), schema)[0] == 18446744073702667240


def test_big_decimal():
schema = pt.SchemaElement(
type=pt.Type.FIXED_LEN_BYTE_ARRAY,
name="test",
converted_type=pt.ConvertedType.DECIMAL,
type_length=32,
scale=1,
precision=38
)
data = np.array([
b'', b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1e\\',
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1d\\',
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\r{',
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x19)'],
dtype='|S32')
assert np.isclose(convert(data, schema),
np.array([0., 777.2, 751.6, 345.1, 644.1])).all()

0 comments on commit 95dd015

Please sign in to comment.