Reputation: 43
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".
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'
Edit: 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
Upvotes: 4
Views: 2887
Reputation: 42875
Index: 150 entries
suggests your index
column needs to be converted to datetime
using pd.to_datetime()
first.
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')
before setting to index.
Upvotes: 1