Skip to content

Make PyArrow datetime timezone test pass #5014

Closed
@gvwilson

Description

@gvwilson

plotly/tests/test_optional/test_px/test_px_hover.py contains a test test_date_in_hover that currently marks the PyArrow case xfail because of problems with the constructor. We need to fix that test.


@FBruzzesi writes:

My understanding is that you would like to build the table directly with a "date" being datetime type.
Not sure which kind of conversions are possible in the initialization - especially, what we use from string to datetime (pyarrow.compute.strptime) requires an explicit format, so I would be surprised if only during initialization this is inferred.

What you can do is to pass a datetime object:

from datetime import datetime
from zoneinfo import ZoneInfo
import pyarrow as pa

data = {
    'date': [datetime.datetime(2015, 4, 4, 19, 31, 30, tzinfo=zoneinfo.ZoneInfo(key='Europe/Rome'))],
    'value': [3]
}

schema = pa.schema([("date", pa.timestamp(unit="ms", tz="Europe/Berlin")), ("value", pa.int64())])
pa.table(data, schema=schema)

pyarrow.Table
date: timestamp[ms, tz=Europe/Berlin]
value: int64
----
date: [[2015-04-04 17:31:30.000Z]]
value: [[3]]

In narwhals you could do the same:

import narwhals as nw

schema = {
    "date": nw.Datetime(time_zone="Europe/Rome", time_unit="ms"),
    "value": nw.Int64()
}

nw.from_dict(data, schema, native_namespace=pa)
┌───────────────────────────────────┐
|        Narwhals DataFrame         |
|-----------------------------------|
|pyarrow.Table                      |
|date: timestamp[ms, tz=Europe/Rome]|
|value: int64                       |
|----                               |
|date: [[2015-04-04 17:31:30.000Z]] |
|value: [[3]]                       |
└───────────────────────────────────┘

Metadata

Metadata

Assignees

Labels

P1needed for current cyclebugsomething brokentestingautomated tests

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions