Skip to content

[Python] Repr of Timestamp scalars error if they are outside range of stdlib datetime.datetime #36323

@jorisvandenbossche

Description

@jorisvandenbossche

Describe the bug, including details regarding any error messages, version, and platform.

Currently, the repr of the TimestampScalar (well, of scalars in general) essentially uses the repr of the Python object. For TimestampScalar, this thus fails if this conversion fails, such as for timestamps that are outside of the range of datetime.datetime ([1-9999]):

>>> res = pa.scalar("0000-01-01").cast(pa.timestamp("s"))
>>>type(res)
pyarrow.lib.TimestampScalar
>>> repr(res)
...
File ~/miniconda3/envs/dev39/lib/python3.9/site-packages/pyarrow/scalar.pxi:117, in pyarrow.lib.Scalar.__repr__()

File ~/miniconda3/envs/dev39/lib/python3.9/site-packages/pyarrow/scalar.pxi:521, in pyarrow.lib.TimestampScalar.as_py()

File ~/miniconda3/envs/dev39/lib/python3.9/site-packages/pyarrow/scalar.pxi:437, in pyarrow.lib._datetime_from_int()

OverflowError: date value out of range

>>> pc.strftime(res)
<pyarrow.StringScalar: '0000-01-01T00:00:00'>

Our generic Scalar __repr__ implementation:

def __repr__(self):
return '<pyarrow.{}: {!r}>'.format(
self.__class__.__name__, self.as_py()
)

I think for Timestamps we should just convert to a string repr ourselves (strftime, or our own pretty printer as is used for the Array repr)

Component(s)

Python

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions