次のような単純な DataFrame を作成するとします。
import pandas as pd
import datetime as dt
import heapq
a = [1371215933513120, 1371215933513121]
b = [1,2]
d = ['h','h']
df = pd.DataFrame({'a':a, 'b':b, 'c':[dt.datetime.fromtimestamp(t/1000000.) for t in a], 'd':d})
df.index=pd.DatetimeIndex(df['c'])
d = OrderedDict()
d['x'] = df
p = pd.Panel(d)
p['x']['b'] = p['x']['b'].astype(int)
counter = 0
for dt in p.major_axis:
print "a", counter, p['x'].dtypes
df_s = p.major_xs(dt)
print "b", counter, p['x'].dtypes
print "-------------"
counter += 1
これは 3 つの列で構成され、そのうちの 1 つが索引になります。長軸値の反復を開始すると、最初の反復後にint
列のデータ型が変更さobject
れます。
a 0 a object
b int64
c object
d object
dtype: object
b 0 a object
b object
c object
d object
dtype: object
-------------
a 1 a object
b object
c object
d object
dtype: object
b 1 a object
b object
c object
d object
dtype: object
-------------
反復中に列が型を保持するように、これを回避する方法はありますか?