[Solved] Rounding datetime based on time of day


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))

1

solved Rounding datetime based on time of day