Skip to content
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

Show microseconds in str() output, and suggestied change to repr() output #118

Closed
davidhassell opened this issue Sep 6, 2019 · 6 comments

Comments

@davidhassell
Copy link
Contributor

Hello,

Currently, fractions of seconds are not shown in the str() representation of a cftime date-time object:

In [5]: import cftime                                                                                                                                                             

In [6]: cftime.__version__                                                                                                                                                        
Out[6]: '1.0.3.4'

In [7]: dt = cftime.DatetimeGregorian(2000, 2, 13, 12, 34, 23, 123456)                                                                                                            

In [8]: repr(dt)                                                                                                                                                                  
Out[8]: 'cftime.DatetimeGregorian(2000, 2, 13, 12, 34, 23, 123456, 6, 44)'

In [9]: str(dt)                                                                                                                                                                   
Out[9]: '2000-02-13 12:34:23'

I would like to suggest that they are shown, in a ISOI 8601-compliant fashion (https://en.wikipedia.org/wiki/ISO_8601#General_principles), so that the str() output would look like:

>>> str(dt)
'2000-02-13 12:34:23.123456'

I am happy to provide a pull request if this idea is accepted.

On a related note, might it be possible to alter the repr() output to improve its readaibility? I fully appreciate that this is subjective, and the developers have the last word, but I'll describe how it could perhaps be changed in case you agree!

We currently have:

In [8]: repr(dt)                                                                                                                                                                  
Out[8]: 'cftime.DatetimeGregorian(2000, 2, 13, 12, 34, 23, 123456, 6, 44)'

I would like to suggest:

 >>> rep(dt)
'cftime.DatetimeGregorian(2000-02-13 12:34:23.123456)'

My use case for this is that I frequently inspect numpy arrays of cftime objects, and the current repr() output delivered by numpy is hard for a (this!) human to parse quickly. We currently have:

In [10]: dt2 = cftime.DatetimeGregorian(2000, 2, 14, 12, 34, 23, 123456)                                                                                                          

In [11]: dt3 = cftime.DatetimeGregorian(2000, 3, 13, 12, 34, 23)                                                                                                          

In [12]: import numpy                                                                                                                                                             

In [13]: numpy.array([dt, dt2, dt3])                                                                                                                                              
Out[13]: 
array([cftime.DatetimeGregorian(2000, 2, 13, 12, 34, 23, 123456, 6, 44),
       cftime.DatetimeGregorian(2000, 2, 14, 12, 34, 23, 123456, 0, 45),
       cftime.DatetimeGregorian(2000, 3, 13, 12, 34, 23, 0, 0, 73)],
      dtype=object)

... but I think the following is easier to read:

>>> numpy.array([dt, dt2, dt3])
array([cftime.DatetimeGregorian(2000-02-13 12:34:23.123456),
       cftime.DatetimeGregorian(2000-02-14 12:34:23.123456),
       cftime.DatetimeGregorian(2000-03-13 12:34:23)],
      dtype=object)

I think that this output is still true to that required by repr - i.e you can still recreate the original object from this information.

Again, I would happy to include this in a pull request.

Many thanks for considering this,
David

@jswhit
Copy link
Collaborator

jswhit commented Sep 6, 2019

A pull request for both of these would be welcome, thanks. One comment on changing the repr() output for Datetime objects, the proposed change would be inconsistent with python datetime behaviour (which is similar to the current cftime behavior). I don't really mind - but someone might.

@davidhassell
Copy link
Contributor Author

Thanks. I'll put together a pull request, and see if any further comments or objections arise.

davidhassell added a commit to davidhassell/cftime that referenced this issue Sep 10, 2019
@jswhit
Copy link
Collaborator

jswhit commented Sep 11, 2019

thinking about it some more, it seems like

 >>> repr(dt)
'cftime.DatetimeGregorian(2000-02-13 12:34:23.123456)'

implies that the DatetimeGregorian constructor accepts an ISO 8601 string. I think I'd prefer we leave the repr() behavior as it is, but keep your improvement to str().

@davidhassell
Copy link
Contributor Author

Fair enough.

I'm thinking of writing a pull request that would allow the cftime.datetime object to accept ISO 8601-like strings for initialization (in addition to the existing integer arguments). Would that be OK?

@jswhit
Copy link
Collaborator

jswhit commented Sep 25, 2019

Sure, that would be fine.

jswhit added a commit that referenced this issue Oct 8, 2019
@jswhit
Copy link
Collaborator

jswhit commented Oct 8, 2019

PR merged, thansk!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants