我正在尝试使用重采样对数据帧进行采样,但是每当尝试这样做时,都会收到TypeError。错误出现在代码的最后两行。
我对这一切仍然陌生,因此对此有些困惑。我不断收到“ TypeError:仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但有一个'Index'实例”
import pandas as pd
import mailbox
import datetime
import pytz
MBOX = 'enron.mbox'
mbox = mailbox.mbox(MBOX)
mbox_dict = {}
for i, msg in enumerate(mbox):
mbox_dict[i] = {}
for header in msg.keys():
mbox_dict[i][header] = msg[header]
mbox_dict[i]['Body'] = msg.get_payload().replace('\n', ' ').replace('\t', ' ').replace('\r', ' ').strip()
df = pd.DataFrame.from_dict(mbox_dict, orient='index')
df.index = df['Date'].apply(pd.to_datetime)
utc=pytz.UTC
start_date = '2002-1-1'
stop_date = '2002-5-1'
date_time_obj = datetime.datetime.strptime(start_date, '%Y-%m-%d')
date_time_obj = utc.localize(date_time_obj)
start_time = date_time_obj.replace(tzinfo=utc)
date_time_obj = datetime.datetime.strptime(stop_date, '%Y-%m-%d')
date_time_obj = utc.localize(date_time_obj)
stop_time = date_time_obj.replace(tzinfo=utc)
datemask = ( df.index > start_time) & (df.index <= stop_time)
#*************here's when things go wrong *******************
vol_by_month = df.loc[datemask].resample('1M').count()['To']
print(vol_by_month)