diff --git a/pandas/core/interchange/column.py b/pandas/core/interchange/column.py index 9ef73aa1f40e0..9ba237a94b8fe 100644 --- a/pandas/core/interchange/column.py +++ b/pandas/core/interchange/column.py @@ -5,6 +5,7 @@ import numpy as np from pandas._libs.lib import infer_dtype +from pandas._libs.tslibs import iNaT from pandas.util._decorators import cache_readonly import pandas as pd @@ -38,7 +39,7 @@ _NULL_DESCRIPTION = { DtypeKind.FLOAT: (ColumnNullType.USE_NAN, None), - DtypeKind.DATETIME: (ColumnNullType.USE_NAN, None), + DtypeKind.DATETIME: (ColumnNullType.USE_SENTINEL, iNaT), DtypeKind.INT: (ColumnNullType.NON_NULLABLE, None), DtypeKind.UINT: (ColumnNullType.NON_NULLABLE, None), DtypeKind.BOOL: (ColumnNullType.NON_NULLABLE, None), diff --git a/pandas/core/interchange/dataframe_protocol.py b/pandas/core/interchange/dataframe_protocol.py index 036f84a393903..6f4ec673f03e0 100644 --- a/pandas/core/interchange/dataframe_protocol.py +++ b/pandas/core/interchange/dataframe_protocol.py @@ -70,7 +70,7 @@ class ColumnNullType(enum.IntEnum): NON_NULLABLE : int Non-nullable column. USE_NAN : int - Use explicit float NaN/NaT value. + Use explicit float NaN value. USE_SENTINEL : int Sentinel value besides NaN/NaT. USE_BITMASK : int diff --git a/pandas/tests/interchange/test_impl.py b/pandas/tests/interchange/test_impl.py index 4c4c2a99c5558..0c2cffff8a159 100644 --- a/pandas/tests/interchange/test_impl.py +++ b/pandas/tests/interchange/test_impl.py @@ -4,6 +4,8 @@ import numpy as np import pytest +from pandas._libs.tslibs import iNaT + import pandas as pd import pandas._testing as tm from pandas.core.interchange.dataframe_protocol import ( @@ -176,3 +178,15 @@ def test_nonstring_object(): col = df.__dataframe__().get_column_by_name("A") with pytest.raises(NotImplementedError, match="not supported yet"): col.dtype + + +def test_datetime(): + df = pd.DataFrame({"A": [pd.Timestamp("2022-01-01"), pd.NaT]}) + col = df.__dataframe__().get_column_by_name("A") + + assert col.size == 2 + assert col.null_count == 1 + assert col.dtype[0] == DtypeKind.DATETIME + assert col.describe_null == (ColumnNullType.USE_SENTINEL, iNaT) + + tm.assert_frame_equal(df, from_dataframe(df.__dataframe__()))