Skip to content

Commit

Permalink
Merge pull request #3379 from y-p/timestamp_repr
Browse files Browse the repository at this point in the history
ENH/CLN: make Timestamp repr valid python code, like datetime does.
  • Loading branch information
y-p committed Apr 23, 2013
2 parents 3396dca + b7b36b1 commit c28d2cd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 8 additions & 0 deletions pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 8 additions & 3 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<Timestamp: %s>' % 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):
Expand Down

0 comments on commit c28d2cd

Please sign in to comment.