Skip to content Skip to sidebar Skip to footer

Pandas: Counting Frequency Of Datetime Objects In A Column

I have a column (from my original data) that I have converted from a string to a datetime-object in Pandas. The column looks like this: 0 2012-01-15 11:10:12 1 2012-01-15 1

Solution 1:

You can first get the date part of the datetime, and then use value_counts:

s.dt.date.value_counts()

Small example:

In [12]: s = pd.Series(pd.date_range('2012-01-01', freq='11H', periods=6))

In [13]: s
Out[13]:
0   2012-01-01 00:00:00
1   2012-01-01 11:00:00
2   2012-01-01 22:00:00
3   2012-01-02 09:00:00
4   2012-01-02 20:00:00
5   2012-01-03 07:00:00
dtype: datetime64[ns]

In [14]: s.dt.date
Out[14]:
0    2012-01-01
1    2012-01-01
2    2012-01-01
3    2012-01-02
4    2012-01-02
5    2012-01-03
dtype: object

In [15]: s.dt.date.value_counts()
Out[15]:
2012-01-01    3
2012-01-02    2
2012-01-03    1
dtype: int64

Solution 2:

Late to the party, but nowadays it is dataframe.date_time_column.resample('1D').count()


Solution 3:

you can try this:

df.groupby(level=0).count()

this requires your date to be index.


Post a Comment for "Pandas: Counting Frequency Of Datetime Objects In A Column"