Rounding datetime based on time of day

2024/7/5 11:00:15

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!

Answer

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 dls = ["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))
https://en.xdnf.cn/q/120367.html

Related Q&A

Scraping Project Euler site with scrapy [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python date function bugs

I am trying to create a function in python which will display the date. So I can see the program run, I have set one day to five seconds, so every five seconds it will become the next day and it will p…

Retreiving data from a website [duplicate]

This question already has answers here:How to determine the IP address of the server after connecting with urllib2?(4 answers)Closed 9 years ago.Im terribly sorry if this is unacceptable or answered e…

How to comma separate an array of integers in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

Python 2.7.5 - Where is it installed on Windows Vista? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about a specific programming problem, a software algorithm, or s…

Python list of lists specific path combinations or permutations

I have a list of lists and am looking for something similar to combinations or permutations, but there are conditions that may result in good "Path" or "Dead_End". If "Dead_En…

Python packages.import sys vs from sys import argv

Im trying to use argv into my python script. For this im using following code:from sys import argv script, file_name = argv print(file_name)python3 e.py e.txt This code works fine.But when I use:import…

How to reorganize a list of tuples?

Say I had a list of tuples:[(98, studentA), (97, studentB), (98, studentC), (95,studentD)]And I wanted to organize it so that the students are grouped together by the first number in the tuple, what wo…

How to loop through json data with multiple objects

My json file data.json looks like this [ {"host" : "192.168.0.25", "username":"server2", "path":"/home/server/.ssh/01_id"}, {"host"…

python django only the first statement statement can be accessed

i can acccess only the first statement in my name appjavascript:<script type="text/javascript">function searched(){{% for names in name %}nameSearched = document.getElementById(name).va…