Pandas Problem with TimeGrouper - Default I / O Index

I am trying to extract timestamps from my mailbox in order to generate statistics using Pandas. My code captures up to 1000 letters and stores timestamps in a list. Then I pass the list to pd.DataFrame, which gives me a framework with a column like "time".

I want to use groupby and TimeGrouper to build the number of letters on weekdays, time of day, etc., so I set the timestamp column as an index, but I get TypeError: "Valid only with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of Index ". I tried to use to_datetime, but it raises another TypeError object: the type of type time does not have len (). From what I can say, 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 
+4
source share
1 answer

Index: 150 entries suggests that your index column be converted to datetime using pd.to_datetime() .

df[0] may look like datetime , but type conversion is required, try

 df[0] = pd.to_datetime(df[0], format='%m/%d/%Y %H:%M:%S') 

before installing in the index.

+1
source

Source: https://habr.com/ru/post/1263725/


All Articles