Skip to content

string-representation for Timestamp does not show nanoseconds if present #5912

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
cancan101 opened this issue Jan 12, 2014 · 3 comments · Fixed by #5701
Closed

string-representation for Timestamp does not show nanoseconds if present #5912

cancan101 opened this issue Jan 12, 2014 · 3 comments · Fixed by #5701
Labels
Output-Formatting __repr__ of pandas objects, to_string
Milestone

Comments

@cancan101
Copy link
Contributor

See:

In [27]: str(pd.Timestamp(200))
Out[27]: '1970-01-01 00:00:00'

In [28]: repr(pd.Timestamp(200))
Out[28]: "Timestamp('1970-01-01 00:00:00.000000200', tz=None)"

whereas microseconds work fine:

In [30]: str(pd.Timestamp(2000))
Out[30]: '1970-01-01 00:00:00.000002'
@cancan101
Copy link
Contributor Author

One way to fix this would be to implement isoformat in Timestamp. Another would be to implement __str__ and have that method check if nanos !=0.

@cancan101
Copy link
Contributor Author

    def isoformat(self, sep='T'):
        base = super(_Timestamp, self).isoformat(sep=sep)
        if self.nanosecond == 0:
            return base

        if self.tzinfo is not None:
            base1, base2 = base[:-6], base[-6:]
        else:
            base1, base2 = base, ""

        if self.microsecond != 0:
            base1 += "%.3d" % self.nanosecond
        else:
            base1 += "%.9d" % self.nanosecond

        return base1 + base2

@cancan101
Copy link
Contributor Author

I intend to fix this (and add some test showing issue) in #5701

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant