Skip to content

Commit aaefe4f

Browse files
committed
Fix timezone-aware DatetimeIndex converting to an object array
1 parent bcc6863 commit aaefe4f

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

‎_plotly_utils/basevalidators.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ def copy_to_readonly_numpy_array(v, kind=None, force_numeric=False):
105105
"O": "object",
106106
}
107107

108+
pd = get_module("pandas", should_load=False)
109+
if pd is not None and isinstance(v, pd.DatetimeIndex) and v.tz is not None:
110+
# Remove time zone so that local time is displayed, and so that converting
111+
# to numpy doesn't result in object dtype
112+
v = v.tz_localize(None)
113+
108114
# With `pass_through=True`, the original object will be returned if unable to convert
109115
# to a Narwhals DataFrame or Series.
110116
v = nw.from_native(v, allow_series=True, pass_through=True)

‎tests/test_plotly_utils/validators/test_pandas_series_input.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ def test_data_array_validator_dates_series(
186186
np.testing.assert_array_equal(res, dates_array)
187187

188188

189+
def test_data_array_validator_tz_aware_datetime_index(data_array_validator):
190+
index = pd.date_range("2024-03-30", periods=3, freq="D", tz="Europe/Berlin")
191+
res = data_array_validator.validate_coerce(index)
192+
193+
assert isinstance(res, np.ndarray)
194+
assert res.dtype.kind == "M"
195+
np.testing.assert_array_equal(res, index.tz_localize(None).to_numpy())
196+
197+
189198
def test_data_array_validator_dates_dataframe(
190199
data_array_validator, datetime_pandas, dates_array
191200
):

0 commit comments

Comments
 (0)