Dark Apostle
Dark Apostle

Reputation: 197

Python Pandas sizeof times

I am working in a dataframe in Pandas that looks like this.

 Identifier                   datetime
0   AL011851                     00:00:00
1   AL011851                     06:00:00             
2   Al011851                     12:00:00

This is my code so far:

import pandas as pd

hurricane_df = pd.read_csv("hurdat2.csv",parse_dates=['datetime'])
hurricane_df['datetime'] = pd.to_timedelta(hurricane_df['datetime'].dt.strftime('%H:%M:%S'))
hurricane_df
grouped = hurricane_df.groupby('datetime').size()
grouped

What I did was convert the datetime column to a timedelta to get the hours. I want to get the size of the datetime column but I want just hours like 1:00, 2:00, 3:00, etc. but I get minute intervals as well like 1:15 and 2:45.

Any way to just display the hour? Thank you.

Upvotes: 1

Views: 95

Answers (2)

Pavel
Pavel

Reputation: 5876

You can use pandas.Timestamp.round with Series.dt shortcut:

df['datetime'] = df['datetime'].dt.round('h')

So

... datetime
    01:15:00
    02:45:00

becomes

... datetime
    01:00:00
    03:00:00

Upvotes: 1

cph_sto
cph_sto

Reputation: 7597

df = pd.DataFrame({'Identifier':['AL011851','AL011851','AL011851'],'datetime': ["2018-12-08 16:35:23","2018-12-08 14:20:45", "2018-12-08 11:45:00"]})
df['datetime'] = pd.to_datetime(df['datetime'])
df
  Identifier            datetime
0   AL011851 2018-12-08 16:35:23
1   AL011851 2018-12-08 14:20:45
2   AL011851 2018-12-08 11:45:00

# Rounds to nearest hour
def roundHour(t):
        return (t.replace(second=0, microsecond=0, minute=0, hour=t.hour)
               +timedelta(hours=t.minute//30))

df.datetime=df.datetime.map(lambda t: roundHour(t)) # Step 1: Round to nearest hour
df.datetime=df.datetime.map(lambda t: t.strftime('%H:%M')) # Step 2: Remove seconds
df
  Identifier datetime
0   AL011851    17:00
1   AL011851    14:00
2   AL011851    12:00

Upvotes: 0

Related Questions