8000 Make PyArrow datetime timezone test pass · Issue #5014 · plotly/plotly.py · GitHub
[go: up one dir, main page]

Skip to content

Make PyArrow datetime timezone test pass #5014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign 8000 up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gvwilson opened this issue Feb 6, 2025 · 1 comment
Closed

Make PyArrow datetime timezone test pass #5014

gvwilson opened this issue Feb 6, 2025 · 1 comment
Assignees
Labels
bug something broken P1 needed for current cycle testing automated tests

Comments

@gvwilson
Copy link
Contributor
gvwilson commented Feb 6, 2025

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]]                       |
└───────────────────────────────────┘
@gvwilson gvwilson added bug something broken P1 needed for current cycle testing automated tests labels Feb 6, 2025
@gvwilson gvwilson self-assigned this Feb 6, 2025
@FBruzzesi
Copy link
Contributor
FBruzzesi commented Feb 6, 2025

Hey @gvwilson , thanks for the ping.
I might be missing something but the test is not marked to xfail for pyarrow at the moment and I don't see it failing either in your CI (at least for pyarrow).
Similarly, locally I don't get any issue.

Just to make sure, this is the test:

def test_date_in_hover(constructor):
df = nw.from_native(
constructor({"date": ["2015-04-04 19:31:30+01:00"], "value": [3]})
).with_columns(date=nw.col("date").str.to_datetime(format="%Y-%m-%d %H:%M:%S%z"))
fig = px.scatter(df.to_native(), x="value", y="value", hover_data=["date"])
# Check that what gets displayed is the local datetime
assert nw.to_py_scalar(fig.data[0].customdata[0][0]) == nw.to_py_scalar(
df.item(row=0, column="date")
).replace(tzinfo=None)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken P1 needed for current cycle testing automated tests
Projects
None yet
Development

No branches or pull requests

2 participants
0