Reputation: 143
Here we have a dataframe with Pandas
. I am struggling to round the time precision.
Title 1 Title 2 Title 3 Title 4 ... midprice t0 t1 tEvent
2015-07-15 09:30:00+00:00 26730 26844 26851 26870 ... 267.020 09:29:59.500 09:30:00.000 20150715 09:30:00.002
2015-07-15 09:30:00.499500032+00:00 26726 26830 26840 26844 ... 267.000 09:30:00.000 09:30:00.500 20150715 09:30:00.599
2015-07-15 09:30:01+00:00 26730 26844 26851 26870 ... 267.020 09:30:00.500 09:30:01.000 20150715 09:30:01.012
2015-07-15 09:30:01.499500032+00:00 26694 26768 26780 26800 ... 266.770 09:30:01.000 09:30:01.500 20150715 09:30:01.557
2015-07-15 09:30:02+00:00 26695 26750 26768 26780 ... 266.775 09:30:01.500 09:30:02.000 20150715 09:30:02.086
2015-07-15 09:30:02.499500032+00:00 26694 26750 26768 26780 ... 266.770 09:30:02.000 09:30:02.500 20150715 09:30:02.584
2015-07-15 09:30:03+00:00 26694 26745 26750 26751 ... 266.770 09:30:02.500 09:30:03.000 20150715 09:30:03.106
2015-07-15 09:30:03.499500032+00:00 26693 26745 26750 26751 ... 266.765 09:30:03.000 09:30:03.500 20150715 09:30:03.638
2015-07-15 09:30:04+00:00 26693 26745 26750 26751 ... 266.765 09:30:03.500 09:30:04.000 20150715 09:30:04.419
2015-07-15 09:30:04.499500032+00:00 26693 26740 26745 26750 ... 266.765 09:30:04.000 09:30:04.500 20150715 09:30:04.636
2015-07-15 09:30:05+00:00 26693 26745 26750 26751 ... 266.765 09:30:04.500 09:30:05.000 20150715 09:30:06.817
2015-07-15 09:30:05.499500032+00:00 26693 26745 26750 26751 ... 266.765 09:30:05.000 09:30:05.500 20150715 09:30:06.817
2015-07-15 09:30:06+00:00 26693 26745 26750 26751 ... 266.765 09:30:05.500 09:30:06.000 20150715 09:30:06.817
The output I am interested is simply
Title 1 Title 2 Title 3 Title 4 ... midprice t0 t1 tEvent
2015-07-15 09:30:00+00:00 26730 26844 26851 26870 ... 267.020 09:29:59.500 09:30:00.000 20150715 09:30:00.002
2015-07-15 09:30:00.5:00 26726 26830 26840 26844 ... 267.000 09:30:00.000 09:30:00.500 20150715 09:30:00.599
2015-07-15 09:30:01+00:00 26730 26844 26851 26870 ... 267.020 09:30:00.500 09:30:01.000 20150715 09:30:01.012
2015-07-15 09:30:01.5:00 26694 26768 26780 26800 ... 266.770 09:30:01.000 09:30:01.500 20150715 09:30:01.557
2015-07-15 09:30:02+00:00 26695 26750 26768 26780 ... 266.775 09:30:01.500 09:30:02.000 20150715 09:30:02.086
2015-07-15 09:30:02.5:00 26694 26750 26768 26780 ... 266.770 09:30:02.000 09:30:02.500 20150715 09:30:02.584
2015-07-15 09:30:03+00:00 26694 26745 26750 26751 ... 266.770 09:30:02.500 09:30:03.000 20150715 09:30:03.106
2015-07-15 09:30:03.5:00 26693 26745 26750 26751 ... 266.765 09:30:03.000 09:30:03.500 20150715 09:30:03.638
2015-07-15 09:30:04+00:00 26693 26745 26750 26751 ... 266.765 09:30:03.500 09:30:04.000 20150715 09:30:04.419
2015-07-15 09:30:04.5:00 26693 26740 26745 26750 ... 266.765 09:30:04.000 09:30:04.500 20150715 09:30:04.636
2015-07-15 09:30:05+00:00 26693 26745 26750 26751 ... 266.765 09:30:04.500 09:30:05.000 20150715 09:30:06.817
2015-07-15 09:30:05.5:00 26693 26745 26750 26751 ... 266.765 09:30:05.000 09:30:05.500 20150715 09:30:06.817
2015-07-15 09:30:06+00:00 26693 26745 26750 26751 ... 266.765 09:30:05.500 09:30:06.000 20150715 09:30:06.817
How can I change the time column precision?
Upvotes: 1
Views: 46
Reputation: 38415
You can round and then trim and convert back to datetime
df.index = df.index.round('ms').strftime('%Y-%m-%d %H:%M:%S.%f').str[:22]
df.index = pd.to_datetime(df.index)
Title 1 Title 2 Title 3 Title 4
2015-07-15 09:30:00.00 26730 26844 26851 26870
2015-07-15 09:30:00.50 26726 26830 26840 26844
2015-07-15 09:30:01.00 26730 26844 26851 26870
2015-07-15 09:30:01.50 26694 26768 26780 26800
Upvotes: 1