Home > Mobile >  Problems rounding datetime to nearest hour
Problems rounding datetime to nearest hour

Time:11-02

I have a 4 dataframes with the objective of merging them around the date column, by hour, to make inferences based on interactions with tweets.

In order for the dataframe to be properly manicured, I need to round the date column to the nearest hour, and then merge dataframes around the hour, as mentioned.

My problem has been with rounding the datetime objects to the nearest hour. Below is my code, and I cannot figure out how to get it to round correctly.

import datetime
tweet_and_price_df['date'] = tweet_and_price_df['date'].apply(lambda dt: 
    datetime.datetime(dt.year, dt.month, dt.day, 
    round(dt.hour),round(dt.minute)))

The output is correctly rounding the minute, but not to the hour. Here is a snippet from the output:

Screenshot of output

If anyone has any ideas why it's not rounding the hour up/down, please let me know!

Thank you.

CodePudding user response:

You can just do

tweet_and_price_df['date'] = pd.to_datetime(tweet_and_price_df['date']).round('H')
  • Related