Skip to content

Commit

Permalink
ARROW-18264: [Python] Add missing value accessor to temporal types (a…
Browse files Browse the repository at this point in the history
…pache#14746)

Authored-by: aandres <[email protected]>
Signed-off-by: Joris Van den Bossche <[email protected]>
  • Loading branch information
0x26res authored Dec 13, 2022
1 parent 9753a67 commit 84101a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 10 additions & 0 deletions python/pyarrow/scalar.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ cdef class Date32Scalar(Scalar):
Concrete class for date32 scalars.
"""

@property
def value(self):
cdef CDate32Scalar* sp = <CDate32Scalar*> self.wrapped.get()
return sp.value if sp.is_valid else None

def as_py(self):
"""
Return this value as a Python datetime.datetime instance.
Expand All @@ -365,6 +370,11 @@ cdef class Date64Scalar(Scalar):
Concrete class for date64 scalars.
"""

@property
def value(self):
cdef CDate64Scalar* sp = <CDate64Scalar*> self.wrapped.get()
return sp.value if sp.is_valid else None

def as_py(self):
"""
Return this value as a Python datetime.datetime instance.
Expand Down
20 changes: 19 additions & 1 deletion python/pyarrow/tests/test_scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,32 @@ def test_time_from_datetime_time():
@pytest.mark.parametrize(['value', 'time_type'], [
(1, pa.time32("s")),
(2**30, pa.time32("s")),
(None, pa.time32("s")),
(1, pa.time32("ms")),
(2**30, pa.time32("ms")),
(None, pa.time32("ms")),
(1, pa.time64("us")),
(2**62, pa.time64("us")),
(None, pa.time64("us")),
(1, pa.time64("ns")),
(2**62, pa.time64("ns")),
(None, pa.time64("ns")),
(1, pa.date32()),
(2**30, pa.date32()),
(None, pa.date32()),
(1, pa.date64()),
(2**62, pa.date64()),
(None, pa.date64()),
(1, pa.timestamp("ns")),
(2**62, pa.timestamp("ns")),
(None, pa.timestamp("ns")),
(1, pa.duration("ns")),
(2**62, pa.duration("ns")),
(None, pa.duration("ns")),
((1, 2, -3), pa.month_day_nano_interval()),
(None, pa.month_day_nano_interval()),
])
def test_time_values(value, time_type):
def test_temporal_values(value, time_type: pa.DataType):
time_scalar = pa.scalar(value, type=time_type)
assert time_scalar.value == value

Expand Down

0 comments on commit 84101a5

Please sign in to comment.