Skip to content

BUG: GH12622 where pprint of Timestamp in nested structure fails #12629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,5 @@ Bug Fixes


- Bug in ``pivot_table`` when ``margins=True`` and ``dropna=True`` where nulls still contributed to margin count (:issue:`12577`)

- Bug in ``Timestamp.__repr__`` that caused ``pprint`` to fail in nested structures (:issue:`12622`)
19 changes: 19 additions & 0 deletions pandas/tseries/tests/test_tslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,25 @@ def test_nat_fields(self):
self.assertTrue(np.isnan(ts.daysinmonth))
self.assertTrue(np.isnan(ts.days_in_month))

def test_pprint(self):
# GH12622
import pprint
nested_obj = {'foo': 1,
'bar': [{'w': {'a': Timestamp('2011-01-01')}}] * 10}
result = pprint.pformat(nested_obj, width=50)
expected = r'''{'bar': [{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
{'w': {'a': Timestamp('2011-01-01 00:00:00')}}],
'foo': 1}'''
self.assertEqual(result, expected)


class TestDatetimeParsingWrappers(tm.TestCase):
def test_does_not_convert_mixed_integer(self):
Expand Down
89 changes: 45 additions & 44 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -316,50 +316,6 @@ class Timestamp(_Timestamp):

return ts_base

def __repr__(self):
stamp = self._repr_base
zone = None

try:
stamp += self.strftime('%z')
if self.tzinfo:
zone = _get_zone(self.tzinfo)
except ValueError:
year2000 = self.replace(year=2000)
stamp += year2000.strftime('%z')
if self.tzinfo:
zone = _get_zone(self.tzinfo)

try:
stamp += zone.strftime(' %%Z')
except:
pass

tz = ", tz='{0}'".format(zone) if zone is not None else ""
offset = ", offset='{0}'".format(self.offset.freqstr) if self.offset is not None else ""

return "Timestamp('{stamp}'{tz}{offset})".format(stamp=stamp, tz=tz, offset=offset)

@property
def _date_repr(self):
# Ideal here would be self.strftime("%Y-%m-%d"), but
# the datetime strftime() methods require year >= 1900
return '%d-%.2d-%.2d' % (self.year, self.month, self.day)

@property
def _time_repr(self):
result = '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)

if self.nanosecond != 0:
result += '.%.9d' % (self.nanosecond + 1000 * self.microsecond)
elif self.microsecond != 0:
result += '.%.6d' % self.microsecond

return result

@property
def _repr_base(self):
return '%s %s' % (self._date_repr, self._time_repr)

def _round(self, freq, rounder):

Expand Down Expand Up @@ -977,6 +933,30 @@ cdef class _Timestamp(datetime):
self._assert_tzawareness_compat(other)
return _cmp_scalar(self.value, ots.value, op)

def __repr__(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so moving this to _Timestamp worked, interesting. Should move any referenced functions as well ,e.g. ._repr_base. _date_repr & _time_repr for consistency.

stamp = self._repr_base
zone = None

try:
stamp += self.strftime('%z')
if self.tzinfo:
zone = _get_zone(self.tzinfo)
except ValueError:
year2000 = self.replace(year=2000)
stamp += year2000.strftime('%z')
if self.tzinfo:
zone = _get_zone(self.tzinfo)

try:
stamp += zone.strftime(' %%Z')
except:
pass

tz = ", tz='{0}'".format(zone) if zone is not None else ""
offset = ", offset='{0}'".format(self.offset.freqstr) if self.offset is not None else ""

return "Timestamp('{stamp}'{tz}{offset})".format(stamp=stamp, tz=tz, offset=offset)

cdef bint _compare_outside_nanorange(_Timestamp self, datetime other,
int op) except -1:
cdef datetime dtval = self.to_datetime()
Expand Down Expand Up @@ -1098,6 +1078,27 @@ cdef class _Timestamp(datetime):
out = get_start_end_field(np.array([self.value], dtype=np.int64), field, freqstr, month_kw)
return out[0]

property _repr_base:
def __get__(self):
return '%s %s' % (self._date_repr, self._time_repr)

property _date_repr:
def __get__(self):
# Ideal here would be self.strftime("%Y-%m-%d"), but
# the datetime strftime() methods require year >= 1900
return '%d-%.2d-%.2d' % (self.year, self.month, self.day)

property _time_repr:
def __get__(self):
result = '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)

if self.nanosecond != 0:
result += '.%.9d' % (self.nanosecond + 1000 * self.microsecond)
elif self.microsecond != 0:
result += '.%.6d' % self.microsecond

return result

property asm8:
def __get__(self):
return np.datetime64(self.value, 'ns')
Expand Down