From 334b4db5136170c6e2167a1d624cac9f9366f9a3 Mon Sep 17 00:00:00 2001 From: y-p Date: Wed, 17 Apr 2013 03:29:26 +0300 Subject: [PATCH 1/2] TST: Timestamp repr is valid python code --- pandas/tseries/tests/test_period.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tseries/tests/test_period.py b/pandas/tseries/tests/test_period.py index f34a237b55dd4..10a5e039b9fc6 100644 --- a/pandas/tseries/tests/test_period.py +++ b/pandas/tseries/tests/test_period.py @@ -1298,6 +1298,14 @@ def test_to_timestamp_preserve_name(self): conv = index.to_timestamp('D') self.assertEquals(conv.name, 'foo') + def test_to_timestamp_repr_is_code(self): + zs=[Timestamp('99-04-17 00:00:00',tz='UTC'), + Timestamp('2001-04-17 00:00:00',tz='UTC'), + Timestamp('2001-04-17 00:00:00',tz='America/Los_Angeles'), + Timestamp('2001-04-17 00:00:00',tz=None)] + for z in zs: + self.assertEquals( eval(repr(z)), z) + def test_as_frame_columns(self): rng = period_range('1/1/2000', periods=5) df = DataFrame(randn(10, 5), columns=rng) From b7b36b1f3150d3908244516ae60353694475e4ab Mon Sep 17 00:00:00 2001 From: y-p Date: Thu, 18 Apr 2013 00:33:41 +0300 Subject: [PATCH 2/2] ENH/CLN: make Timestamp repr executable, like datetime PTF --- pandas/tslib.pyx | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pandas/tslib.pyx b/pandas/tslib.pyx index 4d15ec8c8ace9..f9c1b2329c16d 100644 --- a/pandas/tslib.pyx +++ b/pandas/tslib.pyx @@ -164,20 +164,25 @@ class Timestamp(_Timestamp): def __repr__(self): result = self._repr_base + zone = None try: result += self.strftime('%z') if self.tzinfo: zone = _get_zone(self.tzinfo) - result += _tz_format(self, zone) except ValueError: year2000 = self.replace(year=2000) result += year2000.strftime('%z') if self.tzinfo: zone = _get_zone(self.tzinfo) - result += _tz_format(year2000, zone) - return '' % result + try: + result += zone.strftime(' %%Z') + except: + pass + zone = "'%s'" % zone if zone else 'None' + + return "Timestamp('%s', tz=%s)" % (result,zone) @property def _repr_base(self):