A Tuesday 29 July 2008, Francesc Alted escrigué:
[snip]
> > >   In [12]: t[0]
> > >   Out[12]: 24       # representation as an int64
> >
> > why not a "pretty" representation of timedelta64 too? I'd like that
> > better (at least for __str__, perhaps __repr__ should be the raw
> > numbers.
>
> That could be an interesting feature.  Here it is what the
> ``datetime``
>
> module does:
> >>> delta = datetime.datetime(1980,2,1)-datetime.datetime(1970,1,1)
> >>> delta.__str__()
>
> '3683 days, 0:00:00'
>
> >>> delta.__repr__()
>
> 'datetime.timedelta(3683)'
>
> For the NumPy ``timedelta64`` with a time unit of days, it could be
>
> something like:
> >>> delta_days.__str__()
>
> '3683 days'
>
> >>> delta_days.__repr__()
>
> 3683
>
> while for a ``timedelta64`` with a time unit of microseconds it could
>
> be:
> >>> delta_us.__str__()
>
> '3683 days, 3:04:05.000064'
>
> >>> delta_us.__repr__()
>
> 318222245000064
>
> But I'm open to other suggestions, of course.

Sorry, but I've been a bit inconsistent here as this is documented in 
the proposal already.  Just to clarify things, here it goes the 
str/repr suggestions (just a bit more populated with examples) in the 
second version of the second proposal.

For absolute times:

In [5]: numpy.datetime64(42, 'us')
Out[5]: datetime64(42, 'us')

In [6]: print numpy.datetime64(42)
1970-01-01T00:00:00.000042  # representation in ISO 8601 format

In [7]: print numpy.datetime64(367.7, 'D')  # decimal part is lost
1971-01-02  # still ISO 8601 format

In [8]: numpy.datetime('2008-07-18T12:23:18', 'm')  # from ISO 8601
Out[8]: datetime64(20273063, 'm')

In [9]: print numpy.datetime('2008-07-18T12:23:18', 'm')
Out[9]: 2008-07-18T12:23

In [10]: t = numpy.zeros(5, dtype="datetime64[D]")

In [11]: print t
[1970-01-01  1970-01-01  1970-01-01  1970-01-01  1970-01-01]

In [12]: repr(t)
Out[12]: array([0, 0, 0, 0, 0], dtype="datetime64[D]")

In [13]: print t[0]
1970-01-01

In [14]: t[0]
Out[14]: datetime64(0, unit='D')

In [15]: t[0].item()     # getter in action
Out[15]: datetime.datetime(1970, 1, 1, 0, 0)


For relative times:

In [5]: numpy.timedelta64(10, 'us')
Out[5]: timedelta64(10, 'us')

In [6]: print numpy.timedelta64(10, 'ms')
0:00:00.010

In [7]: print numpy.timedelta64(3600.2, 'm')  # decimal part is lost
2 days, 12:00

In [8]: t0 = numpy.zeros(5, dtype="datetime64[ms]")

In [9]: t1 = numpy.ones(5, dtype="datetime64[ms]")

In [10]: t = t1 - t1

In [11]: t[0] = datetime.timedelta(0, 24)  # setter in action

In [12]: print t
[0:00:24.000  0:00:01.000  0:00:01.000  0:00:01.000  0:00:01.000]

In [13]: repr(t)
Out[13]: array([24000, 1, 1, 1, 1], dtype="timedelta64[ms]")

In [14]: print t[0]
0:00:24.000

In [15]: t[0]
Out[15]: timedelta(24000, unit='ms')

In [16]: t[0].item()     # getter in action
Out[16]: datetime.timedelta(0, 24)


Cheers,

-- 
Francesc Alted
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to