Python Can';t按月份将列中的值分组

Python Can';t按月份将列中的值分组,python,pandas,pandas-groupby,Python,Pandas,Pandas Groupby,我想计算timelog中的实例数,按月分组。我有以下专栏: print df['date_unconditional'][:5] 0 2018-10-15T07:00:00 1 2018-06-12T07:00:00 2 2018-08-28T07:00:00 3 2018-08-29T07:00:00 4 2018-10-29T07:00:00 Name: date_unconditional, dtype: object 然后我将其转换为datetime格式

我想计算timelog中的实例数,按月分组。我有以下专栏:

print df['date_unconditional'][:5]

0    2018-10-15T07:00:00
1    2018-06-12T07:00:00
2    2018-08-28T07:00:00
3    2018-08-29T07:00:00
4    2018-10-29T07:00:00
Name: date_unconditional, dtype: object
然后我将其转换为datetime格式

df['date_unconditional'] = pd.to_datetime(df['date_unconditional'].dt.strftime('%m/%d/%Y'))
print df['date_unconditional'][:5]


0   2018-10-15
1   2018-06-12
2   2018-08-28
3   2018-08-29
4   2018-10-29
Name: date_unconditional, dtype: datetime64[ns]
然后我试着数一数,但我总是出错

df['date_unconditional'] = pd.to_datetime(df['date_unconditional'], errors='coerce')
print df['date_unconditional'].groupby(pd.Grouper(freq='M')).count()

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'

该格式不是RangeIndex,我尝试用不同的方式更改它,但这个错误不断出现。

在以下位置使用参数

或者创建
DatetimeIndex
by,然后可以使用-两者之间的区别是
count
排除缺失值,
size

df['date_unconditional'] = pd.to_datetime(df['date_unconditional'], errors='coerce')
print (df.set_index('date_unconditional').groupby(pd.Grouper(freq='M')).size())
2018-06-30    1
2018-07-31    0
2018-08-31    2
2018-09-30    0
2018-10-31    2
Freq: M, dtype: int64
df['date_unconditional'] = pd.to_datetime(df['date_unconditional'], errors='coerce')
print (df.set_index('date_unconditional').groupby(pd.Grouper(freq='M')).size())
2018-06-30    1
2018-07-31    0
2018-08-31    2
2018-09-30    0
2018-10-31    2
Freq: M, dtype: int64