Moises Garcia
Moises Garcia

Reputation: 13

Count frequency of timestamp by date and hour using python pandas

I'm new using python and I have been looking for the answer, but nothing helps me.

I have a pandas data frame of the form

data

Out:

      CALL_TYPE   TIMESTAMP
 0           B    1408039037
 1           B    1408038611
 2           B    1408038568
 3           B    1408039090
 4           B    1408039177
 5           A    1408037146
 6           B    1408038846
 7           A    1408038948...

My TIMESTAMP is an int64 of the type pandas.core.series.Series

I would like to count the frequency of my TIMESTAMP by "day" and "hour".

How can I achieve this using Python pandas?

Thanks in advance :)

Upvotes: 0

Views: 2006

Answers (2)

DYZ
DYZ

Reputation: 57033

First, split them into hours and days:

data['DAY'], data['HOUR'] = data["TIMESTAMP"] // (24*3600), data["TIMESTAMP"] % (24*3600)

Now, do the count:

data.groupby(['DAY','HOUR']).count()

Upvotes: 1

panty
panty

Reputation: 3

Do not forget datetime.fromtimestamp(timestamp) can give your local time, as the time.localtime(timestamp) .

datetime.datetime.utcfromtimestamp(timestamp) would give you UTC time.

(e.g.

data['HOUR'] = str(datetime.utcfromtimestamp(data["TIMESTAMP"]))[11:13]
data['DAY'] = str(datetime.utcfromtimestamp(data["TIMESTAMP"]))[:10]

Then use groupby() for a correct time format.

Upvotes: 0

Related Questions