Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the time of a Pandas datetime column to midnight?

Tags:

python

pandas

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
    ...
like image 847
Javide Avatar asked Jan 31 '20 22:01

Javide


People also ask

How do I change Timestamp on pandas?

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.


2 Answers

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
like image 103
Andy L. Avatar answered Oct 10 '22 10:10

Andy L.


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
like image 43
Grzegorz Skibinski Avatar answered Oct 10 '22 09:10

Grzegorz Skibinski