Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

python - Rounding datetime based on time of day

I have a pandas dataframe with timestamps shown below:

6/30/2019 3:45:00 PM

I would like to round the date based on time. Anything before 6AM will be counted as the day before.

6/30/2019  5:45:00 AM -> 6/29/2019
6/30/2019  6:30:00 AM -> 6/30/2019

What I have considered doing is splitting date and time into 2 different columns then using an if statement to shift the date (if time >= 06:00 etc). Just wondering there is a built in function in pandas to do this. Ive seen posts of people rounding up and down based on the closest hour but never a specific time threshold (6AM).

Thank you for the help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

there could be a better way to do this.. But this is one way of doing it.

import pandas as pd    

def checkDates(d):
    if d.time().hour < 6:
        return d - pd.Timedelta(days=1)
    else:
        return d

ls = ["12/31/2019  3:45:00 AM", "6/30/2019  9:45:00 PM", "6/30/2019  10:45:00 PM", "1/1/2019  4:45:00 AM"]
df = pd.DataFrame(ls, columns=["dates"])
df["dates"] = df["dates"].apply(lambda d: checkDates(pd.to_datetime(d)))
print (df)
                dates
0 2019-12-30 03:45:00
1 2019-06-30 21:45:00
2 2019-06-30 22:45:00
3 2018-12-31 04:45:00

Also note i am not taking into consideration of the time. when giving back the result.. if you just want the date at the end of it you can just get that out of the datetime object doing something like this

print ((pd.to_datetime("12/31/2019 3:45:00 AM")).date()) >>> 2019-12-31

if understand python well and dont want anyone else(in the future) to understand what your are doing one liner to the above is.

df["dates"] = df["dates"].apply(lambda d: pd.to_datetime(d) - pd.Timedelta(days=1) if pd.to_datetime(d).time().hour < 6 else pd.to_datetime(d))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...