From 991b49e9a916e3639f72a9e12b088d367a4b12e0 Mon Sep 17 00:00:00 2001 From: fjetter Date: Sun, 10 Jun 2018 20:51:42 +0200 Subject: [PATCH] Fix scalar string conversion for strings --- python/pyarrow/scalar.pxi | 6 ++++++ python/pyarrow/tests/test_convert_builtin.py | 12 ++++++++---- python/pyarrow/tests/test_scalars.py | 12 ++++++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/python/pyarrow/scalar.pxi b/python/pyarrow/scalar.pxi index 964eadbfeb3a..3aba9f9e43a1 100644 --- a/python/pyarrow/scalar.pxi +++ b/python/pyarrow/scalar.pxi @@ -62,6 +62,12 @@ cdef class ArrayValue(Scalar): else: return super(Scalar, self).__repr__() + def __str__(self): + if hasattr(self, 'as_py'): + return str(self.as_py()) + else: + return super(Scalar, self).__str__() + def __eq__(self, other): if hasattr(self, 'as_py'): if isinstance(other, ArrayValue): diff --git a/python/pyarrow/tests/test_convert_builtin.py b/python/pyarrow/tests/test_convert_builtin.py index a4b2151bdf40..31228b41e095 100644 --- a/python/pyarrow/tests/test_convert_builtin.py +++ b/python/pyarrow/tests/test_convert_builtin.py @@ -537,22 +537,26 @@ def test_sequence_timestamp_from_int_with_unit(): arr_s = pa.array(data, type=s) assert len(arr_s) == 1 assert arr_s.type == s - assert str(arr_s[0]) == "Timestamp('1970-01-01 00:00:01')" + assert repr(arr_s[0]) == "Timestamp('1970-01-01 00:00:01')" + assert str(arr_s[0]) == "1970-01-01 00:00:01" arr_ms = pa.array(data, type=ms) assert len(arr_ms) == 1 assert arr_ms.type == ms - assert str(arr_ms[0]) == "Timestamp('1970-01-01 00:00:00.001000')" + assert repr(arr_ms[0]) == "Timestamp('1970-01-01 00:00:00.001000')" + assert str(arr_ms[0]) == "1970-01-01 00:00:00.001000" arr_us = pa.array(data, type=us) assert len(arr_us) == 1 assert arr_us.type == us - assert str(arr_us[0]) == "Timestamp('1970-01-01 00:00:00.000001')" + assert repr(arr_us[0]) == "Timestamp('1970-01-01 00:00:00.000001')" + assert str(arr_us[0]) == "1970-01-01 00:00:00.000001" arr_ns = pa.array(data, type=ns) assert len(arr_ns) == 1 assert arr_ns.type == ns - assert str(arr_ns[0]) == "Timestamp('1970-01-01 00:00:00.000000001')" + assert repr(arr_ns[0]) == "Timestamp('1970-01-01 00:00:00.000000001')" + assert str(arr_ns[0]) == "1970-01-01 00:00:00.000000001" with pytest.raises(pa.ArrowException): class CustomClass(): diff --git a/python/pyarrow/tests/test_scalars.py b/python/pyarrow/tests/test_scalars.py index 0b910723595d..d6cdb6697c96 100644 --- a/python/pyarrow/tests/test_scalars.py +++ b/python/pyarrow/tests/test_scalars.py @@ -37,6 +37,7 @@ def test_bool(self): v = arr[0] assert isinstance(v, pa.BooleanValue) assert repr(v) == "True" + assert str(v) == "True" assert v.as_py() is True assert arr[1] is pa.NA @@ -47,6 +48,7 @@ def test_int64(self): v = arr[0] assert isinstance(v, pa.Int64Value) assert repr(v) == "1" + assert str(v) == "1" assert v.as_py() == 1 assert v == 1 @@ -58,6 +60,7 @@ def test_double(self): v = arr[0] assert isinstance(v, pa.DoubleValue) assert repr(v) == "1.5" + assert str(v) == "1.5" assert v.as_py() == 1.5 assert v == 1.5 @@ -71,6 +74,7 @@ def test_half_float(self): v = arr[0] assert isinstance(v, pa.HalfFloatValue) assert repr(v) == "1.5" + assert str(v) == "1.5" assert v.as_py() == 1.5 assert v == 1.5 @@ -81,8 +85,10 @@ def test_string_unicode(self): v = arr[0] assert isinstance(v, pa.StringValue) - assert v.as_py() == 'foo' - assert v == 'foo' + assert v.as_py() == u'foo' + assert repr(v) == repr(u"foo") + assert str(v) == str(u"foo") + assert v == u'foo' # Assert that newly created values are equal to the previously created # one. assert v == arr[0] @@ -99,6 +105,8 @@ def test_bytes(self): v = arr[0] assert isinstance(v, pa.BinaryValue) assert v.as_py() == b'foo' + assert str(v) == str(b"foo") + assert repr(v) == repr(b"foo") assert v == b'foo' assert arr[1] is pa.NA