pandas TimeGrouper问题-“时间"上出现Typeerror指数 [英] Pandas TimeGrouper issue - Typeerror on "time" index
问题描述
我正在尝试从收件箱中提取时间戳,以便生成有关Pandas的一些统计信息.我的代码最多可以抓取1000封电子邮件,并将时间戳存储在列表中.然后,我将该列表传递给pd.DataFrame,这给了我一个带有时间"类型列的数据框.
I am trying to extract the timestamps from my inbox in order to generate some statistics with Pandas. My code grabs up to 1000 emails, and stores the timestamps in a list. I then pass the list to pd.DataFrame, which gives me a dataframe with a column of type "time".
我想使用groupby和TimeGrouper来按工作日,一天中的时间等方式绘制电子邮件数量,因此我将timestamp列设置为索引,但出现TypeError:仅对DatetimeIndex有效, TimedeltaIndex或PeriodIndex,但获得了"Index"的实例".我尝试使用to_datetime,但是会生成另一个TypeError:类型为"time"的对象没有len().据我所知,df [0]已经是一个日期时间对象,那么为什么在尝试使用TimeGrouper时会抛出错误?
I want to use groupby and TimeGrouper in order to plot the number of emails by weekday, time of day, etc., so I set my timestamp column as the index, but I get a TypeError: "Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'". I have tried using to_datetime, but that generates another TypeError: object of type 'time' has no len(). From what I can tell, df[0] is already a datetime object, so why does it throw an error when trying to use TimeGrouper?
import win32com.client
import pandas as pd
import numpy as np
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
message = messages.GetLast()
timesReceived = [message.SentOn]
for i in range(1000):
try:
message = messages.GetPrevious()
timesReceived.append(message.SentOn)
except(AttributeError):
break
df = pd.DataFrame(timesReceived);
df.set_index(df[0],inplace=True)
grouped = df.groupby(pd.TimeGrouper('M'))
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'
添加df.info()和df.head()
Adding df.info() and df.head()
df.info()
<class 'pandas.core.frame.DataFrame'>
Index: 150 entries, 04/01/16 09:37:07 to 02/11/16 17:40:56
Data columns (total 1 columns):
0 150 non-null object
dtypes: object(1)
memory usage: 2.3+ KB
df.head()
0
0
04/01/16 09:37:07 04/01/16 09:37:07
04/01/16 04:34:30 04/01/16 04:34:30
04/01/16 03:02:14 04/01/16 03:02:14
04/01/16 02:15:12 04/01/16 02:15:12
04/01/16 00:16:27 04/01/16 00:16:27
推荐答案
Index: 150 entries
建议将您的index
列首先需要使用pd.to_datetime()
转换为datetime
.
Index: 150 entries
suggests your index
column needs to be converted to datetime
using pd.to_datetime()
first.
df[0]
可能类似于datetime
,但需要类型转换,请尝试
df[0]
may look like datetime
but needs type conversion, try
df[0] = pd.to_datetime(df[0], format='%m/%d/%Y %H:%M:%S')
设置为索引之前.
这篇关于 pandas TimeGrouper问题-“时间"上出现Typeerror指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!