Saturday, 15 September 2012

Getting Python to round Timestamp with NS versus Truncate -



Getting Python to round Timestamp with NS versus Truncate -

tried couple different methods , both didn't homecoming desired result. i'm trying .349566 show in microseconds decimal place versus .349565 showing, want round versus truncate.

in [113]: dateutil.parser.parse('2014-06-18 13:06:00.349565827') out[113]: datetime.datetime(2014, 6, 18, 13, 6, 0, 349565) (pandas) in [114]: pd.timestamp('2014-06-18 13:06:00.349565827') out[114]: timestamp('2014-06-18 13:06:00.349565', tz=none)

thoughts on quick way this? know of lot of options using string manipulation , such not goal.

you'll need round manually, unfortunately. on plus side, that's relatively easy in utilize case. string formatting floats can round natively, that's how i'd go it:

date_str = '2014-06-18 13:06:00.349565827' # split off seconds & microseconds beginning, sec = date_str.rsplit(':', 1) # utilize string formatting recreate rounded timestamp date = dateutil.parser.parse('{}:{:02.6f}'.format(beginning, float(sec)))

python

No comments:

Post a Comment