'Remove microseconds and make seconds zero in datetime column?
I have a dataframe df with a column "date", I want to go from this format:
2018-12-11 15:26:04.877
Into a datetime, where there are no microseconds and all seconds are zero:
2018-12-11 15:26:00
What I have done so far is:
import pandas as pd
import datetime
dfdf=pd.read_excel('datefix.xlsx')
print(df.dtypes)
df.date =pd.to_datetime(df.date, errors='coerce')
print(df.date)
df["date"].datetime.replace(microsecond=0)
But I get the following error
AttributeError: 'Series' object has no attribute 'datetime'
Is there another way to get to the outcome desired?
Solution 1:[1]
Instead of .replace use .strftime('%Y-%m-%d %H:%M:%S') on the datetime object.
If you have a column of datetime objects, apply the function using df['column'].apply(mapper_function)
df["date"] basically gives your a pandas Series object, not an individual objects in the column.
Solution 2:[2]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pratik Shivarkar |
| Solution 2 | Hakan Serce |
