Using Pandas 1.0.0, how can I change the time of a datetime dataframe column to midnight in one line of code?
e.g.: from
START_DATETIME
2017-02-13 09:13:33
2017-03-11 23:11:35
2017-03-12 00:44:32
...
to
START_DATETIME
2017-02-13 00:00:00
2017-03-11 00:00:00
2017-03-12 00:00:00
...
My attempt:
df['START_DATETIME'] = df['START_DATETIME'].apply(lambda x: pd.Timestamp(x).replace(hour=0, minute=0, second=0))
but this produces
START_DATETIME
2017-02-13
2017-03-11
2017-03-12
...
replace() function is used to replace the member values of the given Timestamp. The function implements datetime. replace, and it also handles nanoseconds. Example #1: Use Timestamp.
Your method already converted datetime values correctly to midnight. I.e., their time are 00:00:00
. Pandas just intelligently doesn't show the time part because it is redundant to show all same time of 00:00:00
. After you assigning result back to START_DATETIME
, print a cell will show
print(df.loc[0, START_DATETIME])
Output:
2017-02-13 00:00:00
Besides, to convert time to 00:00:00
, you should use dt.normalize
or dt.floor
df['START_DATETIME'] = pd.to_datetime(df['START_DATETIME']).dt.normalize()
or
df['START_DATETIME'] = pd.to_datetime(df['START_DATETIME']).dt.floor('D')
If you want to force pandas to show 00:00:00
in the series output, you need convert START_DATETIME
to str
after converting
pd.to_datetime(df['START_DATETIME']).dt.floor('D').dt.strftime('%Y-%m-%d %H:%M:%S')
Out[513]:
0 2017-02-13 00:00:00
1 2017-03-11 00:00:00
2 2017-03-12 00:00:00
Name: START_DATETIME, dtype: object
You can do:
import pandas as pd
df=pd.DataFrame({"START_DATETIME":
["2017-02-13 09:13:33","2017-03-11 23:11:35","2017-03-12 00:44:32"]})
#you should convert it to date time first
#in case if it's not already:
df["START_DATETIME"]=pd.to_datetime(df["START_DATETIME"])
df["START_DATETIME_DT"]=df["START_DATETIME"].dt.strftime("%Y-%m-%d 00:00:00")
Outputs:
START_DATETIME START_DATETIME_DT
0 2017-02-13 09:13:33 2017-02-13 00:00:00
1 2017-03-11 23:11:35 2017-03-11 00:00:00
2 2017-03-12 00:44:32 2017-03-12 00:00:00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With