Skip to content

Commit 329fe8f

Browse files
authored
BUG: during interchanging from non-pandas tz-aware data (#54287)
* TST: add a test for interchanging from non-pandas tz-aware * TST: correct the test for interchanging from non-pandas tz-aware * check an error in the test * correct test * add extra except in set_nulls * fix bug during interchanging from non-pandas tz-aware * correct test * correct wording
1 parent 792e290 commit 329fe8f

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Diff for: doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ Other
779779
- Bug in :class:`DatetimeIndex` where ``repr`` of index passed with time does not print time is midnight and non-day based freq(:issue:`53470`)
780780
- Bug in :class:`FloatingArray.__contains__` with ``NaN`` item incorrectly returning ``False`` when ``NaN`` values are present (:issue:`52840`)
781781
- Bug in :func:`api.interchange.from_dataframe` was not respecting ``allow_copy`` argument (:issue:`54322`)
782+
- Bug in :func:`api.interchange.from_dataframe` was raising during interchanging from non-pandas tz-aware data containing null values (:issue:`54287`)
782783
- Bug in :func:`api.interchange.from_dataframe` when converting an empty DataFrame object (:issue:`53155`)
783784
- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)
784785
- Bug in :func:`assert_frame_equal` checks category dtypes even when asked not to check index type (:issue:`52126`)

Diff for: pandas/core/interchange/from_dataframe.py

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numpy as np
88

99
from pandas.compat._optional import import_optional_dependency
10+
from pandas.errors import SettingWithCopyError
1011

1112
import pandas as pd
1213
from pandas.core.interchange.dataframe_protocol import (
@@ -514,5 +515,9 @@ def set_nulls(
514515
# cast the `data` to nullable float dtype.
515516
data = data.astype(float)
516517
data[null_pos] = None
518+
except SettingWithCopyError:
519+
# `SettingWithCopyError` may happen for datetime-like with missing values.
520+
data = data.copy()
521+
data[null_pos] = None
517522

518523
return data

Diff for: pandas/tests/interchange/test_impl.py

+19
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,22 @@ def test_datetimetzdtype(tz, unit):
308308
)
309309
df = pd.DataFrame({"ts_tz": tz_data})
310310
tm.assert_frame_equal(df, from_dataframe(df.__dataframe__()))
311+
312+
313+
def test_interchange_from_non_pandas_tz_aware():
314+
# GH 54239, 54287
315+
pa = pytest.importorskip("pyarrow", "11.0.0")
316+
import pyarrow.compute as pc
317+
318+
arr = pa.array([datetime(2020, 1, 1), None, datetime(2020, 1, 2)])
319+
arr = pc.assume_timezone(arr, "Asia/Kathmandu")
320+
table = pa.table({"arr": arr})
321+
exchange_df = table.__dataframe__()
322+
result = from_dataframe(exchange_df)
323+
324+
expected = pd.DataFrame(
325+
["2020-01-01 00:00:00+05:45", "NaT", "2020-01-02 00:00:00+05:45"],
326+
columns=["arr"],
327+
dtype="datetime64[us, Asia/Kathmandu]",
328+
)
329+
tm.assert_frame_equal(expected, result)

0 commit comments

Comments
 (0)