Python For Data Analysis bookを使用してネットワーク分析用の python で分析を学び始めたところですが、いくつかの groupby を実行しているときに例外が発生して混乱しています... これが私の状況です。
パンダにインポートした NetFlow データの CSV があります。データは次のようになります。
dt, srcIP, srcPort, dstIP, dstPort, bytes
2013-06-06 00:00:01.123, 123.123.1.1, 12345, 234.234.1.1, 80, 75
次のようにデータをインポートしてインデックスを作成しました。
df = pd.read_csv('mycsv.csv')
df.index = pd.to_datetime(full_set.pop('dt'))
私が欲しいのは、期間ごとにサーバーにアクセスする一意の srcIP の数です (数日にわたるデータがあり、日付、時間ごとの期間が必要です)。次のようにグループ化してプロットすることで、全体的なトラフィック グラフを取得できます。
df.groupby([lambda t: t.date(), lambda t: t.hour]).srcIP.nunique().plot()
ただし、全体的なトラフィックがサーバー間でどのように分割されるかを知りたいです。私の直感では、「dstIP」列 (一意の値が 5 つしかありません) でさらにグループ化することでしたが、srcIP で集計しようとするとエラーが発生します。
grouped = df.groupby([lambda t: t.date(), lambda t: t.hour, 'dstIP'])
grouped.sip.nunique()
...
Exception: Reindexing only valid with uniquely valued Index objects
したがって、私の具体的な質問は次のとおりです。トラフィックが 1 時間ブロックにわたって集計され、サーバーごとに異なるシリーズがあるプロットを作成するために、この例外を回避するにはどうすればよいですか。
より一般的には、私が犯している newb エラーを教えてください。また、データには定期的な頻度のタイムスタンプがなく、回答に違いが生じる場合に備えて、サンプリングされたデータは必要ありません。
EDIT 1 これは私のipythonセッションであり、入力とまったく同じです。エラーの最も深いいくつかの呼び出しを除いて、出力は省略されました。
EDIT 2 パンダを0.8.0から0.12.0にアップグレードすると、以下に示すより説明的な例外が発生しました
import numpy as np
import pandas as pd
import time
import datetime
full_set = pd.read_csv('june.csv', parse_dates=True, index_col=0)
full_set.sort_index(inplace=True)
gp = full_set.groupby(lambda t: (t.date(), t.hour, full_set['dip'][t]))
gp['sip'].nunique()
...
/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.pyc in _make_labels(self)
1239 raise Exception('Should not call this method grouping by level')
1240 else:
-> 1241 labs, uniques = algos.factorize(self.grouper, sort=self.sort)
1242 uniques = Index(uniques, name=self.name)
1243 self._labels = labs
/usr/local/lib/python2.7/dist-packages/pandas/core/algorithms.pyc in factorize(values, sort, order, na_sentinel)
123 table = hash_klass(len(vals))
124 uniques = vec_klass()
--> 125 labels = table.get_labels(vals, uniques, 0, na_sentinel)
126
127 labels = com._ensure_platform_int(labels)
/usr/local/lib/python2.7/dist-packages/pandas/hashtable.so in pandas.hashtable.PyObjectHashTable.get_labels (pandas/hashtable.c:12229)()
/usr/local/lib/python2.7/dist-packages/pandas/core/generic.pyc in __hash__(self)
52 def __hash__(self):
53 raise TypeError('{0!r} objects are mutable, thus they cannot be'
---> 54 ' hashed'.format(self.__class__.__name__))
55
56 def __unicode__(self):
TypeError: 'TimeSeries' objects are mutable, thus they cannot be hashed