Skip to content

Commit

Permalink
REF: Change _NULL_DESCRIPTION[datetime] to use NaT sentinel (pandas-d…
Browse files Browse the repository at this point in the history
…ev#47887)

* REF: Change _NULL_DESCRIPTION[datetime] to use NaT sentinel

* Add test

* Add roundtrip test and change to use iNaT
  • Loading branch information
mroeschke authored and noatamir committed Nov 9, 2022
1 parent 62c57bb commit 711b672
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pandas/core/interchange/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/interchange/dataframe_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/interchange/test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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__()))

0 comments on commit 711b672

Please sign in to comment.