4
I believe the simplest way is to recreate all column values with a datetime.time object that copies the time, minute and seconds and ignores the microseconds of the original column.
I like to put code that other people can run from scratch, without having to think about how to create a dataframe where it can be applied - the example would be:
import datetime as dt, pandas as pd
z = pd.DataFrame({"hora": [dt.time(23,59,37, 159245)]})
at the ipython terminal::
In [566]: print(z)
hora
0 23:59:37.159245
There, to recreate the column "time":
z["hora"] = z["hora"].apply(lambda t:dt.time(t.hour, t.minute, t.second))
And the result:
In [568]: print(z)
hora
0 23:59:37
What has been done: the method apply
of Series and Dataframes takes as a first argument a function that takes the row or column (in the case of a dataframe) or the value of the cell (in the case of a series) and returns a result. This result is used as the value of a new cell. In this case, set a "lambda" function that receives a value "t", which is assumed to be a datetime.time
, and generates another datetime.time
, but using only the information of hour, minute and seconds.
The "time" column in the original dataframe is replaced by the new one, with the sign of "=".
(In your example date frame, of course, just do the same for the column "Original Time")
Another important detail: this method effectively changes the values in the column, not just the visualization of it. If you want to keep the original values and change only the presentation, the approach has to be different.
Thank you very much, it worked. Abs.
– Heber Araujo