Skip to content

Commit 5b42542

Browse files
authored
REF: Change _NULL_DESCRIPTION[datetime] to use NaT sentinel (#47887)
* REF: Change _NULL_DESCRIPTION[datetime] to use NaT sentinel * Add test * Add roundtrip test and change to use iNaT
1 parent 6db95e7 commit 5b42542

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

pandas/core/interchange/column.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy as np
66

77
from pandas._libs.lib import infer_dtype
8+
from pandas._libs.tslibs import iNaT
89
from pandas.util._decorators import cache_readonly
910

1011
import pandas as pd
@@ -38,7 +39,7 @@
3839

3940
_NULL_DESCRIPTION = {
4041
DtypeKind.FLOAT: (ColumnNullType.USE_NAN, None),
41-
DtypeKind.DATETIME: (ColumnNullType.USE_NAN, None),
42+
DtypeKind.DATETIME: (ColumnNullType.USE_SENTINEL, iNaT),
4243
DtypeKind.INT: (ColumnNullType.NON_NULLABLE, None),
4344
DtypeKind.UINT: (ColumnNullType.NON_NULLABLE, None),
4445
DtypeKind.BOOL: (ColumnNullType.NON_NULLABLE, None),

pandas/core/interchange/dataframe_protocol.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class ColumnNullType(enum.IntEnum):
7070
NON_NULLABLE : int
7171
Non-nullable column.
7272
USE_NAN : int
73-
Use explicit float NaN/NaT value.
73+
Use explicit float NaN value.
7474
USE_SENTINEL : int
7575
Sentinel value besides NaN/NaT.
7676
USE_BITMASK : int

pandas/tests/interchange/test_impl.py

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import numpy as np
55
import pytest
66

7+
from pandas._libs.tslibs import iNaT
8+
79
import pandas as pd
810
import pandas._testing as tm
911
from pandas.core.interchange.dataframe_protocol import (
@@ -176,3 +178,15 @@ def test_nonstring_object():
176178
col = df.__dataframe__().get_column_by_name("A")
177179
with pytest.raises(NotImplementedError, match="not supported yet"):
178180
col.dtype
181+
182+
183+
def test_datetime():
184+
df = pd.DataFrame({"A": [pd.Timestamp("2022-01-01"), pd.NaT]})
185+
col = df.__dataframe__().get_column_by_name("A")
186+
187+
assert col.size == 2
188+
assert col.null_count == 1
189+
assert col.dtype[0] == DtypeKind.DATETIME
190+
assert col.describe_null == (ColumnNullType.USE_SENTINEL, iNaT)
191+
192+
tm.assert_frame_equal(df, from_dataframe(df.__dataframe__()))

0 commit comments

Comments
 (0)