Skip to content

Commit 83ec1de

Browse files
author
Matt Roeschke
committed
BUG: repr of np.datetime64('NaT') in Series/DataFrame with dtype object
1 parent 7b32412 commit 83ec1de

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ Sparse
233233
Other
234234
^^^^^
235235

236-
-
236+
- Bug in :class:`Series` and :class:`DataFrame` repr where ``np.datetime64('NaT')`` and ``np.timedelta64('NaT')`` with ``dtype=object`` would be represented as ``NaN`` (:issue:``)
237237
-
238238
-
239239

pandas/_libs/tslibs/nattype.pyx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ _nat_scalar_rules[Py_GE] = False
3939
# ----------------------------------------------------------------------
4040

4141

42+
cpdef bint is_np_nat(x):
43+
"""Compat check for np.datetime('NaT')"""
44+
try:
45+
return np.isnat(x)
46+
except AttributeError:
47+
# numpy 1.12 compat
48+
return str(x) == 'NaT'
49+
except TypeError:
50+
# np.isnat only defined for datetime, timedelta
51+
return False
52+
53+
4254
def _make_nan_func(func_name, doc):
4355
def f(*args, **kwargs):
4456
return np.nan

pandas/io/formats/format.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pandas._libs import lib
1414
from pandas._libs.tslib import format_array_from_datetime
1515
from pandas._libs.tslibs import NaT, Timedelta, Timestamp, iNaT
16+
from pandas._libs.tslibs.nattype import is_np_nat
1617
from pandas.compat import StringIO, lzip, map, u, zip
1718

1819
from pandas.core.dtypes.common import (
@@ -942,20 +943,11 @@ def _format_strings(self):
942943
self.formatter if self.formatter is not None else
943944
(lambda x: pprint_thing(x, escape_chars=('\t', '\r', '\n'))))
944945

945-
def is_nat(x):
946-
# Compat for numpy 1.12. Replace with np.isnat once min dep is 1.13
947-
try:
948-
return np.isnat(x)
949-
except AttributeError:
950-
return str(x) == 'NaT'
951-
except TypeError:
952-
return False
953-
954946
def _format(x):
955947
if self.na_rep is not None and is_scalar(x) and isna(x):
956948
if x is None:
957949
return 'None'
958-
elif x is NaT or is_nat(x):
950+
elif x is NaT or is_np_nat(x):
959951
return 'NaT'
960952
return self.na_rep
961953
elif isinstance(x, PandasObject):

pandas/tests/frame/test_repr_info.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,3 +521,11 @@ def test_repr_categorical_dates_periods(self):
521521

522522
df = DataFrame({'dt': Categorical(dt), 'p': Categorical(p)})
523523
assert repr(df) == exp
524+
525+
@pytest.mark.parametrize('arg', [np.datetime64, np.timedelta64])
526+
@pytest.mark.parametrize('box, expected', [
527+
[Series, '0 NaT\ndtype: object'],
528+
[DataFrame, ' 0\n0 NaT']])
529+
def test_repr_np_nat_with_object(self, arg, box, expected):
530+
result = repr(box([arg('NaT')], dtype=object))
531+
assert result == expected

0 commit comments

Comments
 (0)