【问题标题】:Change Int64Index to Index and dtype=int64 to dtype=object将 Int64Index 更改为 Index 并将 dtype=int64 更改为 dtype=object
【发布时间】:2013-01-06 10:12:00
【问题描述】:

我正在尝试将 pandas DataFrame 和 Series 写入 xlwt Worksheet 对象。一切顺利,除非我尝试写入 numpy.int64 数据,在这种情况下 xlwt 喘不过气来。在我的数据和单级索引中将 int64 更改为 float64 很简单,但对于 MultiIndexes,正确的做法是什么?

t = pd.DataFrame(np.array(np.mat('0 1 0 1; 1 0 2 3; 1 1 2 4')))
arrays = [[1,2,3,4],[5,6,7,8]]
tuples = zip(*arrays)
index = pd.MultiIndex.from_tuples(tuples, names=['First','Second'])
t.columns = index
wb = xlwt.Workbook()
ws_1 = wb.add_sheet('simple index', cell_overwrite_ok=True)

In [137]: t
Out[137]: 
First   1  2  3  4
Second  5  6  7  8
0       0  1  0  1
1       1  0  2  3
2       1  1  2  4

In [157]: t.ix[0][1][5]
Out[157]: 0

In [158]: ws_1.row(0).write(0, t.ix[0][1][5])
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\xlwt\Row.py", line 259, in write
    raise Exception("Unexpected data type %r" % type(label))
Exception: Unexpected data type <type 'numpy.int64'>

In [159]: t.dtypes
Out[159]: 
First  Second
1      5         int64
2      6         int64
3      7         int64
4      8         int64

In [160]: idx = t.dtypes[t.dtypes == np.int64].index

In [161]: idx
Out[161]: 
MultiIndex
[(1, 5), (2, 6), (3, 7), (4, 8)]

In [163]: for i in idx:
   .....:             t[i] = t[i].astype(np.float64)
   .....: 

In [164]: t.dtypes
Out[164]: 
First  Second
1      5         float64
2      6         float64
3      7         float64
4      8         float64

In [165]: ws_1.row(0).write(0, t.ix[0][1][5])

In [167]: t.columns.levels
Out[167]: [Int64Index([1, 2, 3, 4], dtype=int64), Int64Index([5, 6, 7, 8], dtype=int64)]

In [168]: t.columns
Out[168]: 
MultiIndex
[(1, 5), (2, 6), (3, 7), (4, 8)]

In [169]: t.columns[0][0]
Out[169]: 1

In [170]: ws_1.row(0).write(0, t.columns[0][0])
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\xlwt\Row.py", line 259, in write
    raise Exception("Unexpected data type %r" % type(label))
Exception: Unexpected data type <type 'numpy.int64'>

【问题讨论】:

  • 我认为当前处理多索引的最佳方法是使用to_csv 并导入,to_excel 有效,但会删除列级别名称/索引级别名称。
  • @AndyHayden,我的意图是将此代码包含在可以处理用户定义的 xlwt 样式的类中,to_excel 不允许并且 to_csv 不支持。我还打算按预期处理列级别。见my blog
  • xlwt 喘不过气来,因为isinstance(numpy.int64(1234), int) 返回False。注意numpy.int32numpy.float64没有问题,因为isinstance(numpy.int32(1234), int)返回Trueisinstance(numpy.float64(1234), float)返回Truegithub.com/python-excel/xlwt/issues/15

标签: python pandas xlwt


【解决方案1】:
for i in range(len(t.columns.levels)):
    if t.columns.levels[i].dtype == np.int64:
        t.columns.levels[i] = t.columns.levels[i].astype(np.float64)

【讨论】:

  • 这会抛出一个错误:TypeError: 'FrozenList' does not support mutable operations.
猜你喜欢
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 2020-05-10
相关资源
最近更新 更多