Skip to content

Commit

Permalink
BUG: pandas int extension dtypes has no attribute byteorder (pandas-d…
Browse files Browse the repository at this point in the history
…ev#57173)

* support nullable integers in from_dataframe

* gh issue number

* use BaseMaskedDtype

* only skip if int8[pyarrow]
  • Loading branch information
MarcoGorelli authored Feb 1, 2024
1 parent 24f7db7 commit 0fd7176
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which was raising for Nullable integers (:issue:`55069`)
- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which was raising for empty inputs (:issue:`56700`)
- Fixed bug in :meth:`DataFrame.__getitem__` for empty :class:`DataFrame` with Copy-on-Write enabled (:issue:`57130`)

Expand Down
3 changes: 3 additions & 0 deletions pandas/core/interchange/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from pandas.core.dtypes.dtypes import (
ArrowDtype,
BaseMaskedDtype,
DatetimeTZDtype,
)

Expand Down Expand Up @@ -143,6 +144,8 @@ def _dtype_from_pandasdtype(self, dtype) -> tuple[DtypeKind, int, str, str]:
byteorder = dtype.numpy_dtype.byteorder
elif isinstance(dtype, DatetimeTZDtype):
byteorder = dtype.base.byteorder # type: ignore[union-attr]
elif isinstance(dtype, BaseMaskedDtype):
byteorder = dtype.numpy_dtype.byteorder
else:
byteorder = dtype.byteorder

Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/interchange/test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
is_ci_environment,
is_platform_windows,
)
import pandas.util._test_decorators as td

import pandas as pd
import pandas._testing as tm
Expand Down Expand Up @@ -394,6 +395,17 @@ def test_large_string():
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"dtype", ["Int8", pytest.param("Int8[pyarrow]", marks=td.skip_if_no("pyarrow"))]
)
def test_nullable_integers(dtype: str) -> None:
# https://github.com/pandas-dev/pandas/issues/55069
df = pd.DataFrame({"a": [1]}, dtype=dtype)
expected = pd.DataFrame({"a": [1]}, dtype="int8")
result = pd.api.interchange.from_dataframe(df.__dataframe__())
tm.assert_frame_equal(result, expected)


def test_empty_dataframe():
# https://github.com/pandas-dev/pandas/issues/56700
df = pd.DataFrame({"a": []}, dtype="int8")
Expand Down

0 comments on commit 0fd7176

Please sign in to comment.