Skip to content

Commit eb7a751

Browse files
committed
BUG: repr of pre-1900 datetime64 values in a DataFrame column close #1518
1 parent 3453d44 commit eb7a751

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

pandas/core/format.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -594,19 +594,7 @@ def _format_datetime64(x, tz=None):
594594
return 'NaT'
595595

596596
stamp = lib.Timestamp(x, tz=tz)
597-
base = stamp.strftime('%Y-%m-%d %H:%M:%S')
598-
599-
fraction = stamp.microsecond * 1000 + stamp.nanosecond
600-
digits = 9
601-
602-
if fraction == 0:
603-
return base
604-
605-
while (fraction % 10) == 0:
606-
fraction /= 10
607-
digits -= 1
608-
609-
return base + ('.%%.%id' % digits) % fraction
597+
return stamp._repr_base
610598

611599

612600
def _make_fixed_width(strings, justify='right'):

pandas/src/datetime.pyx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,7 @@ class Timestamp(_Timestamp):
102102
return ts_base
103103

104104
def __repr__(self):
105-
result = '%d-%.2d-%.2d %.2d:%.2d:%.2d' % (self.year, self.month,
106-
self.day, self.hour,
107-
self.minute, self.second)
108-
109-
if self.nanosecond != 0:
110-
nanos = self.nanosecond + 1000 * self.microsecond
111-
result += '.%.9d' % nanos
112-
elif self.microsecond != 0:
113-
result += '.%.6d' % self.microsecond
105+
result = self._repr_base
114106

115107
try:
116108
result += self.strftime('%z')
@@ -124,6 +116,20 @@ class Timestamp(_Timestamp):
124116

125117
return '<Timestamp: %s>' % result
126118

119+
@property
120+
def _repr_base(self):
121+
result = '%d-%.2d-%.2d %.2d:%.2d:%.2d' % (self.year, self.month,
122+
self.day, self.hour,
123+
self.minute, self.second)
124+
125+
if self.nanosecond != 0:
126+
nanos = self.nanosecond + 1000 * self.microsecond
127+
result += '.%.9d' % nanos
128+
elif self.microsecond != 0:
129+
result += '.%.6d' % self.microsecond
130+
131+
return result
132+
127133
@property
128134
def tz(self):
129135
"""

pandas/tests/test_reshape.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,3 @@ def test_pairs(self):
126126
if __name__ == '__main__':
127127
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
128128
exit=False)
129-

pandas/tseries/tests/test_timeseries.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,12 @@ def test_frame_add_datetime64_column(self):
384384
df['A'] = rng
385385
self.assert_(np.issubdtype(df['A'].dtype, np.dtype('M8[ns]')))
386386

387+
def test_frame_datetime64_pre1900_repr(self):
388+
df = DataFrame({'year': date_range('1/1/1700', periods=50,
389+
freq='A-DEC')})
390+
# it works!
391+
repr(df)
392+
387393
def test_frame_add_datetime64_col_other_units(self):
388394
n = 100
389395

0 commit comments

Comments
 (0)