パンダで統計情報を生成するために、受信トレイからタイムスタンプを抽出しようとしています。私のコードは最大1000の電子メールを取得し、タイムスタンプをリストに格納します。私はそのリストをpd.DataFrameに渡します。これは私に "time"型の列を持つデータフレームを与えます。Pandas TimeGrouperの問題 - 「時間」インデックスのタイプエラー
平日、時間帯などの電子メールの数をプロットするためにgroupbyとTimeGrouperを使用したいので、タイムスタンプの列をインデックスとして設定しますが、TypeErrorが表示されます: "DatetimeIndex 、TimedeltaIndex、PeriodIndexのいずれかであるが、 'Index'のインスタンスがある。私はto_datetimeを使用しようとしましたが、別のTypeErrorを生成します。タイプ 'time'のオブジェクトにlen()がありません。私が知ることから、df [0]はすでにdatetimeオブジェクトなので、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を追加する()
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
'df.info()'と 'df.head()'の出力を共有してもよろしいですか? – Stefan
もちろん、私はそれを含めるために自分の投稿を編集しました。ありがとう – thobru
'Index:150 entries'は' index'カラムを 'pd.to_datetime()'を使って 'datetime'に変換する必要があることを示唆しています。 'df [0]'は 'datetime'のように見えるかもしれませんが、' df [0] = pd.to_datetime(df [0]、format = '%m-%d-%Y%H:%M: %S ') 'を実行してインデックスに設定します。 – Stefan