Skip to content

BUG: during interchanging from non-pandas tz-aware data #54287

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 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

16 changes: 16 additions & 0 deletions pandas/tests/interchange/test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest

from pandas._libs.tslibs import iNaT
from pandas.errors import SettingWithCopyError
import pandas.util._test_decorators as td

import pandas as pd
Expand Down Expand Up @@ -295,3 +296,18 @@ def test_datetimetzdtype(tz, unit):
)
df = pd.DataFrame({"ts_tz": tz_data})
tm.assert_frame_equal(df, from_dataframe(df.__dataframe__()))


def test_interchange_from_non_pandas_tz_aware_raise_error():
# GH 54239
pa = pytest.importorskip("pyarrow")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may need to set a minimum version here

Suggested change
pa = pytest.importorskip("pyarrow")
pa = pytest.importorskip("pyarrow", "11.0.0")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, I added the minimum version here

import pyarrow.compute as pc

arr = pa.array([datetime(2020, 1, 1), None, datetime(2020, 1, 2)])
arr = pc.assume_timezone(arr, "Asia/Kathmandu")
table = pa.table({"arr": arr})
exchange_df = table.__dataframe__()

msg = "modifications to a method of a datetimelike object are not supported."
with pytest.raises(SettingWithCopyError, match=msg):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be raising an error though

In

        try:
            data[null_pos] = None
        except TypeError:
            # TypeError happens if the `data` dtype appears to be non-nullable
            # in numpy notation (bool, int, uint). If this happens,
            # cast the `data` to nullable float dtype.
            data = data.astype(float)
            data[null_pos] = None

, could we add an extra except, which catches SettingWithCopyError, and in that case makes a copy of data (data = data.copy()) and then sets the nulls (data[null_pos] = None)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I added the extra except, it works very well. I updated my test and added a line to whatsnew/v2.1.0.rst as well.

from_dataframe(exchange_df)